Scala 函数

2018-10-16  本文已影响0人  _fatef

闭包

scala> var more =1
more: Int = 1

scala> val addMore = (x:Int) => x + more
addMore: Int => Int = <function1>

scala> addMore (100)
res1: Int = 101

scala> more =  9999
more: Int = 9999

scala> addMore ( 10)
res2: Int = 10009

可变参数

scala> def echo (args: Any *) =
     |   for (arg <- args) println(arg)
echo: (args: Any*)Unit

scala> val arr = Array("java","scala","python")
arr: Array[String] = Array(java,scala,python)

scala> echo ("Hello",123,true,arr)
Hello
123
true
[Ljava.lang.String;@2c282004
scala> echo(arr: _*)

命名参数

scala> def add(one: Double, two: Double) :Double = one + tow
add: (one: Double, two: Double) Double

scala> add(two=10, one=2)
res1: Double = 12.0

高阶函数

  val arr = Array("javaisgood", "thisscala", " 123456")

  def strMatcher(matcher: (String) => Boolean) = {
    for(str <- arr; if matcher(str))
      yield str
  }

  def strEnding(str: String) = strMatcher(_.endsWith(str))

  def strContaining(str: String) = strMatcher(_.contains(str))

  def strRegex(str: String) = strMatcher(_.matches(str))

柯里化函数

  // common function
  def add(x: Int, y: Int) = {
    println("x: " + x)
    println("y: " + y)
    x + y
  }

  //curry function
  def _add(x: Int)(y:Int) = {
    println("_x: " + x)
    println("_y: " + y)
    x + y
  }

  // split
  def first(x: Int) = (y: Int) => {
    println("__x: " + x)
    println("__y: " + y)
    x + y
  }
first: (x: Int)Int => Int
scala> val second=first(1)
second: Int => Int = <function1>

scala> second(2)
res1: Int = 3

scala> val onePlus = _add(1)_
onePlus: Int => Int = <function1>

Call By Name & Call By Value

Call by value(传值调用)
使用 (变量 :类型)
Call by name(传名调用)
使用 (变量 : => 类型)
注意 ":" 和 "=>"之间一定要有空格

  def main(args: Array[String]) {
    // testOne
    testCallByNameAndValue(1,testLoop(6))
    // testTwo
    testCallByNameAndValue(testLoop(6),1)
  }
  // x为传值调用,y为传名调用
  def testCallByNameAndValue(x: Int,y: => Int) = {
    println("scala")
    1
  }
  def testLoop(a: Int):Int = {
    for (i <- (0 until a)) println("Iteration" + i)
    1
  }
// results of execution
// testOne
scala
// testTwo
Iteration0
Iteration1
Iteration2
Iteration3
Iteration4
Iteration5
scala

总结:

Call By Value :无论方法是否使用该参数都要先将该参数的值计算出来再传入
Call By Name :只有在该参数被方法使用时才计算该参数的值

定义新的的控制结构

scala> def twice (op:Double => Double, x:Double) =op(op(x))
twice: (op: Double => Double, x: Double)Double

scala> twice(_ + 1, 5)
res0: Double = 7.0

上一篇下一篇

猜你喜欢

热点阅读