Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created March 15, 2012 11:30
Show Gist options
  • Save rirakkumya/2043771 to your computer and use it in GitHub Desktop.
Save rirakkumya/2043771 to your computer and use it in GitHub Desktop.
どう書けばいいのかな
この問題を解決するには、
scala> val f:Int => Int = x => x
f: (Int) => Int = <function1>
この関数に数値を代入して、同じ関数を返す事が出来ないとダメなので解決出来ない。
scala> def a(x:String=>Int)(y:(String=>Int) => String) = y(x)
a: (x: (String) => Int)(y: ((String) => Int) => String)String
これはOK
scala> val w = 3
scala> a{x => x.toInt * w}{x => x("33") + "scala"}
res17: String = 99scala
で、もう一個関数増やして、
def b(x:String=>Int)(y:(String=>Int) => String)(z:((String=>Int)=>String)=>Int) =
xをyに代入してyを評価させないで、zに代入したい。
yを遅延評価させればいいんだろうけど、どう書けばいいんだろう。
def c(x:String=>Int)(y:(String=>Int) => String)(z:((String=>Int)=>String)=>Int) = y(x) //これはOK
def d(x:String=>Int)(y:(String=>Int) => String)(z:((String=>Int)=>String)=>Int) = z(y) //これもOK
== sample ==
def e(x:String=>Int)(y:(String=>Int) => String)(z:((String=>Int),((String=>Int)=>String))=>Int) = z(x,y)
@kmizu
Copy link

kmizu commented Mar 15, 2012

scala> def e(x:String=>Int)(y:(String=>Int) => String)(z: (String=>Int) => ((Str
ing => Int) => String) => Int) = z(x)(y)
e: (x: String => Int)(y: String => Int => String)(z: String => Int => String =>
Int => String => Int)Int

であってます?

@kmizu
Copy link

kmizu commented Mar 15, 2012

使い方は

scala> e{s => s.toInt * 3}{f => f("100").toString + "1"}{(x) => (y) => x("100")

  • y(s => s.toInt).toInt}
    res4: Int = 1301

な感じで。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment