Currying&Partial function初体験

2339 ワード

参考文献
1、王家林.scalaは実戦経典に深く入る。
2、http://www.cnblogs.com/nixil/archive/2012/05/16/2503722.html
3、http://spreadscala.iteye.com/blog/705466
シーン
scalaにおけるカリー化と偏関数の基本概念と応用初体験
実験
<pre name="code" class="java"> def <pre name="code" class="java">curring
(x:Int,y:Int)=x*y;
 package com.scode.scala/** * author: Ivy Peng * function: Curring & Partial Function & Partial applied Function 学习 * date:2016/03/22 22.34 * 1、Curring:把接收多个参数的函数变成接收单一参数的函数。 * 2、偏函数:偷懒,简化函数调用时的参数传递 * collectionA.zip(collectionB) */object Curring_Partial_Function{ def main(args: Array[String]): Unit = { /* * Currying 柯里化 */ def multiple(x:Int,y:Int)=x*y; 
 
    def curring(x:Int)= (y:Int)=> x*y;  // Curry
def curring 2(x:Int)(y:Int)=x*y;/Curry println(curring(3)(8)//Currying応用:u.equals IgnoreCase(u)val a=Aray(Hello)、World b=Alray(Hello)、Spark)println(a.oresponds(b)(u.equals IgnoreCase(u)//参照「問題」における関連解説/**偏関数*定義:種類PartalFunction[-A、+B]の関数があります。Aは、その受け入れられた関数の種類であり、Bはその戻りの結果のタイプ*の特徴である。パラメータ定義ドメインのサブセット*/def p 1:ParttialFunction[Int,Int]={case x if x>1=>1}def p 2=(x:Int)=>x match{case x if x>>1}println(p 2(0)//ここでは異常を投げます。Exception in thread"main"scala.MatErrror:0(of class javant.lant.Integer.*3)の関数を求めます。2ただ一つのパラメータ*/def sum(x:Int,y:Int,z:Int)=x+y+z val parFun_1=sum_printl(parFun_)1(1,2,3)println(parFun_1.appy(1,2,3)val parFun_2=sum(1,_:Int,3)println(parFun_)2(2)println(parFun_2(10)}
 
 
问题

林哥在解说Currying应用的时候以代码段

val a = Array("Hello","World")
    val b = Array("Hello","Spark")
    println(a.corresponds(b)(_.equalsIgnoreCase(_)))
を例にして、調べます。 
cores pondsメソッドのソースコード:
 def corresponds[B](that: GenSeq[B])(p: (A,B) => Boolean): Boolean = {
    val i = this.iterator
    val j = that.iterator
    while (i.hasNext && j.hasNext)
      if (!p(i.next(), j.next()))
        return false

    !i.hasNext && !j.hasNext
  }
は、Curryingがどのように言語レベルでscalaコードを簡略化するかを説明する。
a.co.rresponds(b)(u.equalsIgnoreCase(u) 【ここ:_.equals IgnoreCase(u) ,コンパイラはCurryIngを用いて_を推定することができる。Stringタイプを表す文字?Mr.Snailは少しモンゴルの輪があると言っていますが、今後さらに分析します。」