scala 函数2

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


C:\Users\Lenovo>
C:\Users\Lenovo>
C:\Users\Lenovo>
C:\Users\Lenovo>
C:\Users\Lenovo>
C:\Users\Lenovo>
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>


scala>


scala>


scala>


scala>


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


scala> m
<console>:9: error: missing arguments for method m;
follow this method with `_' if you want to treat it as a partially applied function
              m
              ^


scala> m(2)
res1: Int = 4


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


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


scala> res3(5)
res4: Int = 5


scala> s(x:Int)=>x
<console>:1: error: not a legal formal parameter
       s(x:Int)=>x
        ^


scala> ms(x:Int)=>x
<console>:1: error: not a legal formal parameter
       ms(x:Int)=>x
         ^


scala> ms (x:Int)=>x
<console>:1: error: not a legal formal parameter
       ms (x:Int)=>x
          ^


scala> ms (x:Int) => x
<console>:1: error: not a legal formal parameter
       ms (x:Int) => x
          ^


scala> md (x:Int) => x
<console>:1: error: not a legal formal parameter
       md (x:Int) => x
          ^


scala> md (x:Int) => x
<console>:1: error: not a legal formal parameter
       md (x:Int) => x
          ^


scala> md (x:Int) => x*x
<console>:1: error: not a legal formal parameter
       md (x:Int) => x*x
          ^


scala> md (x:Int) => x * x
<console>:1: error: not a legal formal parameter
       md (x:Int) => x * x
          ^


scala> md(x:Int) => x*x
<console>:1: error: not a legal formal parameter
       md(x:Int) => x*x
         ^


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


scala> md(5)
res5: Int = 25


scala> md(55)
res6: Int = 3025


scala> arr.map(md)
<console>:9: error: not found: value arr
              arr.map(md)
              ^


scala> arr.map(md)
<console>:9: error: not found: value arr
              arr.map(md)
              ^


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


scala> arr.map(md(3))
<console>:10: error: type mismatch;
 found   : Int
 required: Int => ?
              arr.map(md(3))
                        ^


scala> arr.map(md)
res10: Array[Int] = Array(1, 4)


scala> arr.filter(_ % 2==0)
res11: Array[Int] = Array(2)


scala> arr.filter(_ % 2==0).map(_*5)
res12: Array[Int] = Array(10)


scala>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值