加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

你能用scala中的varargs来讨论一个函数吗?

发布时间:2020-12-16 18:15:50 所属栏目:安全 来源:网络整理
导读:我在想如何用varargs来讨论一个方法,我意识到我甚至没有直觉知道如何去做.理想情况下,它会让您随时开始使用它,然后以可迭代结束它. def concat(strs: String*) = strs.mkStringval curriedConcat = concat.currycurriedConcat("OK")("hello","world")("welco
我在想如何用varargs来讨论一个方法,我意识到我甚至没有直觉知道如何去做.理想情况下,它会让您随时开始使用它,然后以可迭代结束它.

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

val curriedConcat = concat.curry

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

scala中是否支持此功能?我无法弄清楚如何做任何事情,而不是将它绑定到长度为N的函数,然后咖喱.

解决方法

使用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,N <: Nat](implicit curry : CurryAux[Out0,N]) = new Curry[T,N] {
    type Out = Out0
    def apply(as: Seq[T],f : Seq[T] => Res) = curry(as,f)
  }
}

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

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

  implicit def curryN[Out,N <: Nat](implicit c : CurryAux[Out,N]) =
    new CurryAux[T => Out,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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读