scala 函数定义使用

Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。


C:\Users\Lenovo>
C:\Users\Lenovo>scala
Welcome to Scala version 2.10.6 (Java HotSpot(TM) Client VM, Java 1.7.0_72).
Type in expressions to have them evaluated.
Type :help for more information.


scala> def  ttt(f:Int=>Int){}
ttt: (f: Int => Int)Unit


scala> def  ttt(f:Int=>Int){ }
ttt: (f: Int => Int)Unit


scala> def  ttt(f:Int=>Int){ println(f(100))}
ttt: (f: Int => Int)Unit


scala>


scala> def  ttt(f:Int=>Int){ println(f(100))}
ttt: (f: Int => Int)Unit


scala> val f0 =(x:Int) => x*3
f0: Int => Int = <function1>


scala> ttt(f0)
300


scala> def  m1(x:Int) = x * x
m1: (x: Int)Int


scala> ttt(m1(20))
<console>:10: error: type mismatch;
 found   : Int
 required: Int => Int
              ttt(m1(20))
                    ^


scala> m1(20)
res2: Int = 400


scala> ttt(m1(20))
<console>:10: error: type mismatch;
 found   : Int
 required: Int => Int
              ttt(m1(20))
                    ^


scala> ttt(m1)
10000


scala> def  ttt(f:Int=>Int){ println(f(100))}
ttt: (f: Int => Int)Unit


scala> ttt(m1)
10000


scala> m1(3)
res6: Int = 9


scala> m1  _
res7: Int => Int = <function1>


scala> ttt(m1)
10000


scala> ttt(m1(50))
<console>:10: error: type mismatch;
 found   : Int
 required: Int => Int
              ttt(m1(50))
                    ^


scala> val  f1 = m1 _
f1: Int => Int = <function1>


scala> ttt(f1(50))
<console>:11: error: type mismatch;
 found   : Int
 required: Int => Int
              ttt(f1(50))
                    ^


scala> ttt(f1)
10000


scala> ttt(m1 _ )
10000


scala> ttt(x => m1(x) )
10000


scala> ttt(m1)
10000


scala> def m1(x:Int ):Int =x
m1: (x: Int)Int


scala> m1(sss)
<console>:9: error: not found: value sss
              m1(sss)
                 ^


scala> m1(555)
res16: Int = 555


scala> def m2(y:Int ):Int =y*y
m2: (y: Int)Int


scala> m2(50)
res17: Int = 2500


scala> def m2(y:Int ) =y*y
m2: (y: Int)Int


scala> def m3y:Int ) =y*y
<console>:1: error: '=' expected but ')' found.
       def m3y:Int ) =y*y
                   ^


scala> def m3(y:Int)=y*y
m3: (y: Int)Int


scala> m3(2)
res18: Int = 4


scala> m3(8
     | m3(8)
     |
     |
You typed two blank lines.  Starting a new command.


scala>


scala>


scala> m3(8)
res19: Int = 64


scala> val f1 =(x:Int ,y:Int) => x+y
f1: (Int, Int) => Int = <function2>


scala> f1(4,5)
res20: Int = 9


scala> val   f2 =(x:Int ,y:Int) =>x*y
f2: (Int, Int) => Int = <function2>


scala> f2(5,5)
res21: Int = 25


scala> def m4(y:Int) = y*y
m4: (y: Int)Int


scala> m4(66)
res22: Int = 4356


scala> val f =m4(y:Int) => y*y
<console>:1: error: not a legal formal parameter
       val f =m4(y:Int) => y*y
                ^


scala> val f =(y:Int) => y*y
f: Int => Int = <function1>


scala> val f4=(y:Int) => y*y
f4: Int => Int = <function1>


scala> f4(5
     | f4(5)
     |
     |
You typed two blank lines.  Starting a new command.


scala>


scala>


scala> f4(5)
res23: Int = 25


scala> f4(7
     | f4(7)
     |
     |
You typed two blank lines.  Starting a new command.


scala>


scala>


scala>


scala>


scala> f4(7)
res24: Int = 49


scala> f4(8)
res25: Int = 64


scala> (x) =>x
<console>:8: error: missing parameter type
              (x) =>x
               ^


scala> (x:Int) =>x
res27: Int => Int = <function1>


scala> res27(5
     |
     |
You typed two blank lines.  Starting a new command.


scala>


scala>


scala> res27(5)
res28: Int = 5


scala> val arr  =Array(1,2,6)
arr: Array[Int] = Array(1, 2, 6)


scala> arr.map(_*10)
res29: Array[Int] = Array(10, 20, 60)


scala> arr.map((x:Int) => x*2)
res30: Array[Int] = Array(2, 4, 12)


scala> (x) =>x
<console>:8: error: missing parameter type
              (x) =>x
               ^


scala> arr.map((x:Int) => x*2)
res32: Array[Int] = Array(2, 4, 12)


scala> arr.map((x) => x*2)
res33: Array[Int] = Array(2, 4, 12)


scala> arr.map((y)=> x*2)
<console>:9: error: not found: value x
              arr.map((y)=> x*2)
                            ^


scala>

函数式编程(FP)是一种软件开发风格,它注重不依赖于编程状态的函数函数式代码易于测试和复用,容易实现并发,且不容易受到bug的攻击。Scala是一种能很好支持函数式编程的新兴JVM语言。《Scala函数式编程》是针对希望学习FP并将它应用于日常编码中的程序员而写的,内容包括:函数式编程的概念;函数式编程相关的各种“为什么”和“怎么做”;如何编写多核程序;练习和检测。 从OOP到FP,思路的转化 我是使用scala做完一个项目之后,开始阅读本书。 介绍下背景: 1 程序员 2 前C程序员,linux平台,没有很深的java背景 3 用scala做过一个2年期的项目 在使用scala的过程中,碰到的问题主要体现在: 1 scala的很多语法糖不理解,不知道为啥要这么写,有种为了这么写的简洁而这么写的感觉 2 scala很多库在设计的时候,不理解原因,包括Option,Collection的很多看似有冗余的地方 3 很多scala的默认写法,不理解 4 多态的具体化,尤其是协变的意义所在 5 各种重载的符号使用 之前读过 programming in scala,对语言的整体还停留在: 1 scala用起来比java更灵活 2 强大的collection,可以更加方便的处理collection类的数据 3 不同于java的并行处理方法,有点像c的逻辑思路 4 开发成本比java小,但是语言学习成本比java高很多 正在阅读这本书的过程中,只能一点一点说。 第一部分快要读完了,习题也快要做完了。 1 第一部分主要着墨点正是回答我上述问题的1,2,3的。很大篇幅都放在,使用scala实现scala默认库文件的API中,通过对简单的函数式编程逻辑的介绍和实践,主要是实践,建立起来一个比较明晰的scala思维模式,或者叫函数式编程的思维模式。 2 无副作用的函数式编程,同时也解释了为什么在scala中,val和var的区分为什么那么重要。 3 在做习题的过程中,尤其是在做类型推导的过程中,对原来oop,命令式编程向函数式编程转变有很大作用;而且简洁的语法,确实让人有享受编程的感觉。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值