Memo‎ > ‎

Scala の Yコンビネータ

実際のコーディングでYコンビネータを利用しやすいように、実引数を前に置くようにし、多引数に対応できるようにしました。
  #10変数で飽きました。どちらにしても引数が多くなったら何らかの構造にまとめるべきですよね。

使い方として、Int と Double の引数をとり、 String を返す例を示します。

   Y(1, 1., (i:Int, result:Double, f:(Int, Double) => String) => i match{
  case _ if i > 9 =>
              result.toString
          case _ =>
      f(i+1, result / i)
  })


ソースコード

package object combinator {
def Y[B](f:(Unit=>B)=>B):B = f(Unit=>Y(f))
def Y[A, B](x:A, f:(A, (A=>B))=>B):B = f(x, (y:A) => Y(y,f))
def Y[A0, A1, B](x0:A0, x1:A1, f:(A0, A1, ((A0,A1)=>B))=>B):B = f(x0, x1,(y0:A0, y1:A1) => Y(y0,y1,f))
def Y[A0, A1, A2, B](x0:A0, x1:A1, x2:A2, f:(A0, A1, A2, ((A0,A1,A2)=>B))=>B):B = f(x0, x1, x2,(y0:A0, y1:A1, y2:A2) => Y(y0,y1,y2,f))
def Y[A0, A1, A2, A3, B](x0:A0, x1:A1, x2:A2, x3:A3, f:(A0, A1, A2, A3, ((A0,A1,A2,A3)=>B))=>B):B = f(x0, x1, x2, x3, (y0:A0, y1:A1, y2:A2, y3:A3) => Y(y0,y1,y2,y3,f))
def Y[A0, A1, A2, A3, A4, B](x0:A0, x1:A1, x2:A2, x3:A3, x4:A4, f:(A0, A1, A2, A3, A4, ((A0,A1,A2,A3,A4)=>B))=>B):B = f(x0, x1, x2, x3, x4, (y0:A0, y1:A1, y2:A2, y3:A3, y4:A4) => Y(y0,y1,y2,y3,y4,f))
def Y[A0, A1, A2, A3, A4, A5, B](x0:A0, x1:A1, x2:A2, x3:A3, x4:A4, x5:A5, f:(A0, A1, A2, A3, A4, A5, ((A0,A1,A2,A3,A4,A5)=>B))=>B):B = f(x0, x1, x2, x3, x4, x5, (y0:A0, y1:A1, y2:A2, y3:A3, y4:A4, y5:A5) => Y(y0,y1,y2,y3,y4,y5,f))
def Y[A0, A1, A2, A3, A4, A5, A6, B](x0:A0, x1:A1, x2:A2, x3:A3, x4:A4, x5:A5, x6:A6, f:(A0, A1, A2, A3, A4, A5, A6, ((A0,A1,A2,A3,A4,A5,A6)=>B))=>B):B = f(x0, x1, x2, x3, x4, x5, x6, (y0:A0, y1:A1, y2:A2, y3:A3, y4:A4, y5:A5, y6:A6) => Y(y0,y1,y2,y3,y4,y5,y6,f))
def Y[A0, A1, A2, A3, A4, A5, A6, A7, B](x0:A0, x1:A1, x2:A2, x3:A3, x4:A4, x5:A5, x6:A6, x7:A7, f:(A0, A1, A2, A3, A4, A5, A6, A7, ((A0,A1,A2,A3,A4,A5,A6,A7)=>B))=>B):B = f(x0, x1, x2, x3, x4, x5, x6, x7, (y0:A0, y1:A1, y2:A2, y3:A3, y4:A4, y5:A5, y6:A6, y7:A7) => Y(y0,y1,y2,y3,y4,y5,y6,y7,f))
def Y[A0, A1, A2, A3, A4, A5, A6, A7, A8, B](x0:A0, x1:A1, x2:A2, x3:A3, x4:A4, x5:A5, x6:A6, x7:A7, x8:A8, f:(A0, A1, A2, A3, A4, A5, A6, A7, A8, ((A0,A1,A2,A3,A4,A5,A6,A7,A8)=>B))=>B):B = f(x0, x1, x2, x3, x4, x5, x6, x7, x8, (y0:A0, y1:A1, y2:A2, y3:A3, y4:A4, y5:A5, y6:A6, y7:A7, y8:A8) => Y(y0,y1,y2,y3,y4,y5,y6,y7,y8,f))
def Y[A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, B](x0:A0, x1:A1, x2:A2, x3:A3, x4:A4, x5:A5, x6:A6, x7:A7, x8:A8, x9:A9, f:(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, ((A0,A1,A2,A3,A4,A5,A6,A7,A8,A9)=>B))=>B):B = f(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, (y0:A0, y1:A1, y2:A2, y3:A3, y4:A4, y5:A5, y6:A6, y7:A7, y8:A8, y9:A9) => Y(y0,y1,y2,y3,y4,y5,y6,y7,y8,y9,f))
}


Comments