scalaの関数と閉パッケージ

5322 ワード

関数と閉パッケージ
方法
//method: The most common way to define a function is as a member of some object;
//such a function is called a method.

def processFile(filename: File, width: Int): Unit = {
  val source = scala.io.Source.fromFile(filename)
  for (line  width)
    println(filename + ":" + line.trim)
}

ローカル関数:関数のネストされた関数
def processFile(filename: File, width: Int): Unit = {

  def processLine(filename: File, line: String, width: Int): Unit = {
    if (line.length > width)
      println(filename + ":" + line.trim)
  }
  

  val source = scala.io.Source.fromFile(filename)
  for (line 

ローカル関数は、それらを含む関数のパラメータにアクセスできるため、上記の関数は次の形式に変更できます.
def processFile(filename: File, width: Int): Unit = {

  def processLine(line: String): Unit = {
    if (line.length > width)
      println(filename + ":" + line.trim)
  }


  val source = scala.io.Source.fromFile(filename)
  for (line 

いっとうかんすう
//    
val fun = (x:Int) => x+1;
println(fun(3))

val fun02 = (x:Int)=>{
  println("We")
  println("are")
  println("here!")
  x+100
}
println(fun02(20))


//     
val someNumbers = List(-11, -10, -5, 0, 5, 10)
someNumbers.foreach((x: Int) => println(x) )
val b = someNumbers.filter((x: Int) => x>0)
println(b) //List(5, 10)

//     ,  
val b = someNumbers.filter(x => x>0)

//     
val someNumbers = List(-11, -10, -5, 0, 5, 10)
someNumbers.foreach(println _)  //   _         
val b = someNumbers.filter(_ >0)

//        :
//             
scala> val f = (_: Int) + (_: Int)
f: (Int, Int) => Int = $$Lambda$1078/1672784022@3feeab43

scala> println(f(1,2))
3

scala> println(f(1,_:Int))
$line18.$read$$iw$$iw$$$Lambda$1081/2090086723@705e24fb

partially applied functions
scala> def sum(a: Int, b: Int, c: Int) = a + b + c
sum: (a: Int, b: Int, c: Int)Int

scala> sum(1,2,3)
res0: Int = 6

scala> val a = sum _
a: (Int, Int, Int) => Int = $$Lambda$1057/1239710170@2d24cdd9

scala> a(1,2,3)
res1: Int = 6

scala> a.apply(2,3,4)
res2: Int = 9

説明1:
object FunctionDemo {
  def main(args: Array[String]): Unit = {

    /**
      *       :PAF ( PARTIALLY APPLIED FUNCTIONS)
      *
      *          ,  a             ,
      *          Scala       sum _    PAF          
      *                      apply  ,             Function3     
      *     apply           ,       sum _          3
      *
      * Scala         a(1,2,3)          apply    ,
      *    a(1,2,3)   a.apply(1,2,3)      
      */
    val a = sum _
    println(a(1,2,3))
    println(a.apply(1,2,3))
  }

  def sum(a: Int, b: Int, c: Int) = a + b + c

}

説明2:
scala> def sum(a: Int, b: Int, c: Int) = a + b + c
sum: (a: Int, b: Int, c: Int)Int

scala> val a = sum _
a: (Int, Int, Int) => Int = $$Lambda$1055/1568638055@5c234920

scala> val b = a(1,(_ : Int),3) //   
b: Int => Int = $$Lambda$1058/497507806@5c5f0edc

scala> b(1)
res0: Int = 5

scala> b(3)
res1: Int = 7
//         b.apply(3)

クローズドパッケージ
簡単な理解は、関数内部の変数がその役割ドメインにない場合でも、外部からアクセスでき、抽象的に聞こえることです.
package scala
object Closures {

  def main(args: Array[String]): Unit = {
    val addOne = makeAdd(1)
    val addTwo = makeAdd(2)

    println(addOne) //scala.Closures$$$Lambda$1/1789447862@1810399e
    println(addTwo) //scala.Closures$$$Lambda$1/1789447862@32d992b2

    println(addOne(1))
    println(addTwo(1))
  }
  //  
  def makeAdd(more: Int) = (x: Int) => x + more

  def normalAdd(a: Int, b: Int) = a + b
}

      :
scala.Closures$$$Lambda$1/1789447862@1810399e
scala.Closures$$$Lambda$1/1789447862@32d992b2
2
3



閉じたパケットがキャプチャした変数に対する修正も、閉じたパケットの外で見ることができる.次のようになります.
scala> val someNumbers = List(-11, -10, -5, 0, 5, 10)
someNumbers: List[Int] = List(-11, -10, -5, 0, 5, 10)

scala> var sum = 0
sum: Int = 0

scala> someNumbers.foreach(sum +=  _)

scala> sum
res1: Int = -11

繰り返しパラメータ
//     args: String*,      ,    
scala> def echo(args: String*) =
     |            for (arg  echo("hello", "world!")
hello
world!

scala>  val arr = Array("What's", "up", "doc?")
arr: Array[String] = Array(What's, up, doc?)

//          
scala> echo(arr)
:14: error: type mismatch;
 found   : Array[String]
 required: String
       echo(arr)
            ^
//          ,         echo  
scala> echo(arr: _*)
What's
up
doc?

名前付きパラメータ
scala> def speed(distance: Float, time: Float): Float =
     |            distance / time
speed: (distance: Float, time: Float)Float

scala> speed(100,10)
res5: Float = 10.0

scala> speed(time = 10, distance=100)
res6: Float = 10.0

デフォルトのパラメータ
//    :         
scala>   def printTime(out: java.io.PrintStream = System.out): Unit ={
     |     out.println("time:"+System.currentTimeMillis())
     |   }
printTime: (out: java.io.PrintStream)Unit

//                 
scala> printTime()
time:1553658060529

scala> printTime(System.err)
time:1553658067245