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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值