scala独学ノート高次関数

4357 ワード

バリュー関数として
         
import scala.math._
val num = 3.14   // Double
val fun = ceil _ // (Double) => Double,  fun  ceil  , _           ,         。
     ,_  ceil       , scala ,        ,         。

fun(num) // 4.0    
Array(3.14,1.42,2.0).map(fun) // Array(4.0,2.0,2.0)    

匿名関数
(x: Double) => 3* x

val triple = (x: Double) => 3 * x
    def  
def triple(x: Double) => 3 * x

Array(3.14, 1.42, 2.0).map((x:Double)=>3*x) //                 

     ,                  
Array(3.14, 1.42, 2.0) map {(x:Double) => 3*x} //     ,        

関数パラメータ付き関数
def valueAtOneQuarter(f: (Double) => Double) = f(0.25)
valueAtOneQuarter(ceil _)    //1.0
valueAtOneQuarter(sqrt _)   //0.5
valueAtOneQuarter     ( (Double) =>Double) => Double
            ,         。

               ,
def mulBy( factor: Double) = (x : Double) => factor * x
val quintuple = mulBy(5)
quintuple(20) // 100
mulBy    (Double) = > ( (Double) => Double )

パラメータ推定
valueAtOneQuarter((x:Double) => 3 * x) // 0.75
  valueAtOneQuarter          (Double) = > Double   ,      :
valueAtOneQuarter((x) = > 3 * x)
valueAtOneQuarter( x = > 3 *x) //           ,         ()
valueAtOneQuarter(3 * _) //     =>       ,    _    

                     :
val fun = 3 * _ //  ,        
val fun = 3 * _(_:Double) // OK
val fun:(Double) => Double = 3* _ //OK,     fun   

一般的な高次関数
(1 to 9).map("*" * _).foreach(prinlnt _)
(1 to 9).filter(_ % 2 ==0) // 2,4,6,8

reduceLeft           ,            ,              ,    
(1 to 9).reduceLeft( _ * _)
   
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9
      
(...((1 *2) *3) * ... * 9 )

  
"Marry has a little lamb".split(" ").sortWith(_.length < _.length)
             :Array("a","had","Mary","lamb","little")

クローズドパッケージ
def mulBy(factor :Double) = (x: Double) => factor * x

val triple = mulBy(3)
val half = mulBy(0.5)
triple(14) + " " + half(14) // 42 17
triple   half      factor  
           ,                       。

SAM変換
 Java ,           ,              ,                    ,                  
    ,             。 Java    SAM  
var counter = 0
val button = new JButton("Increment")
button.addActionListener(new ActionListener{
  override def actionPerformed(event: ActionEvent){
    counter +=1
  }
})
       ,        counter+=1。
            ,           addActionListener, :
button.addActionListener((event: ActionEvent) => counter +=1 )

       :
implicit def makAction(action:(ActionEvent) => Unit) = 
  new ActionListener{
    override def actionPerformed(event: ActionEvent){ action(event)}
}
                ,        ActionListener         (ActionEvent) => Unit    .

コリー化
def mulOneAtATime(x:Int) = (y:Int) => x * y
mulOneAtATime(6)(7)
Scala               :
def mulOneAtATime(x:Int)(y:Int) = x * y。
            ,    
def mulOneAtATime(x:Int, y:Int) = x * y。
  Scala        ,   ,                   ,              。
val a = Array("Hello","World")
val b = Array("Hello","World")
a.corresponds(b)(_.equalsIgnoreCase(_))
  , API 
def corresponds[B](that: Seq[B])(p:(A,B) = >Boolean) : Boolean
  ,            B   that   ,             p     。
 ,that   String     ,        p     (String, String) => Boolean。
          _.equalsIgnoreCase(_)   (a:String, b:String)=>a.equalsIgnoreCase(b)    

パラメータとしての無パラメトリック関数の簡略化書き方(きれいな書き方で、これらの機能がなくても書けるのは、醜いだけです)
def runInThread(block : () => Unit){
  new Thread{
    override def run(){ block()}
  }.start()
}
runInThread { () => println("Hi")} //    

  ,             ()
def runInThread(block : => Unit){ //   (),         。
  new Thread{
    override def run(){ block} //   ()
  }.start()
}
runInThread { println("Hi")} //      。

        :
def until(condition: =>Boolean)(block: => Unit){
 if(condition){
     block
     until(condition)(block)
  }
}
  :
var x = 10
until (x==0){
  x -=1
  println(x)
}//        whili       。
         :
until ( x==0,{...}) ,