您可以在scala中使用varargs来函数化吗? [英] Can you curry a function with varargs in scala?

查看:101
本文介绍了您可以在scala中使用varargs来函数化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在考虑如何使用varargs来实现一种方法,但我意识到我什至对如何去做都没有直觉.理想情况下,它将使您可以在需要时开始使用它,然后以可迭代的方式结束它.

I was thinking about how to go about currying a method with varargs, and I realized that I don't even have an intuition for how one would go about doing it. Ideally, it would be something that would let you start using it whenever you liked, and then end it with an iterable.

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

val curriedConcat = concat.curry

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

scala中对此有支持吗?除了将其绑定到长度为N的函数,然后咖喱处理之外,我一无所知.

Is there support for this in scala? I couldn't figure out how to do anything more than bind it to a function of length N and then curry that.

推荐答案

使用Scala 2.10和无形:

Using Scala 2.10 and shapeless:

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

这篇关于您可以在scala中使用varargs来函数化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆