5

我正在考虑如何使用可变参数来柯里化一种方法,我意识到我什至没有直觉知道如何去做。理想情况下,它可以让您随时开始使用它,然后以可迭代的方式结束它。

def concat(strs: String*) = strs.mkString

val curriedConcat = concat.curry

curriedConcat("OK")("hello", "world")("welcome")(Seq(): _*)

scala 对此有支持吗?除了将它绑定到长度为 N 的函数然后对它进行 curry 之外,我不知道如何做任何事情。

4

1 回答 1

6

使用 Scala 2.10 和无形:

import shapeless.Nat._
import shapeless.{Nat, Succ}

trait Curry[T, Res, N <: Nat] {
  type Out
  def apply(as: Seq[T], f : Seq[T] => Res) : Out
}

object Curry {
  implicit def curry[Out0, T, Res, N <: Nat](implicit curry : CurryAux[Out0, T, Res, N]) = new Curry[T, Res, N] {
    type Out = Out0
    def apply(as: Seq[T], f : Seq[T] => Res) = curry(as, f)
  }
}

trait CurryAux[Out, T, Res, N <: Nat] {
  def apply(as: Seq[T], f : Seq[T] => Res) : Out
}

object CurryAux {
  implicit def curry0[Res, T] = new CurryAux[Res, T, Res, _0] {
    def apply(as: Seq[T], f : Seq[T] => Res) : Res = f(as)
  }

  implicit def curryN[Out, T, Res, N <: Nat](implicit c : CurryAux[Out, T, Res, N]) =
    new CurryAux[T => Out, T, Res, Succ[N]] {
      def apply(as: Seq[T], f : Seq[T] => Res) : (T => Out) = (a: T) => c(as :+ a, f)
    }
}

implicit class CurryHelper[T, Res](f : Seq[T] => Res) {
  def curry[N <: Nat](implicit c : Curry[T, Res, N]): c.Out = c(IndexedSeq[T](), f)
}

用法:

scala> def concat(strs: String*) = strs.mkString
concat: (strs: String*)String

scala> val test = ( concat _ ).curry[_3]
test: String => (String => (String => String)) = <function1>

scala> test("1")("2")("3")
res0: String = 123

没有无形:

class CurryHelper[T, Res](f: Seq[T] => Res, as: Seq[T]) {
  def myCurry() = this
  def apply(ts: T*) = new CurryHelper(f, as ++ ts)
  def apply(ts: Seq[T]) = f(as ++ ts)
}

implicit def toCurryHelper[T, Res](f: Seq[T] => Res) = new CurryHelper(f, IndexedSeq[T]())

scala> def concat(strs: String*) = strs.mkString
concat: (strs: String*)String

scala> val test = ( concat _ ).myCurry
test: CurryHelper[String,String] = CurryHelper@4f48ed35

scala> test("1")("2")("3", "4")(Nil)
res0: String = 1234
于 2012-12-08T15:45:41.820 回答