Applying an argument list to curried function using foldLeft in Scala

独自空忆成欢 提交于 2019-11-27 08:19:02

This turns out to be quite a bit simpler than I initially expected.

First we need to define a simple HList,

sealed trait HList

final case class HCons[H, T <: HList](head : H, tail : T) extends HList {
  def ::[H1](h : H1) = HCons(h, this)
  override def toString = head+" :: "+tail.toString
}

trait HNil extends HList {
  def ::[H1](h : H1) = HCons(h, this)
  override def toString = "HNil"
}

case object HNil extends HNil
type ::[H, T <: HList] = HCons[H, T]

Then we can define our fold-like function inductively with the aid of a type class,

trait FoldCurry[L <: HList, F, Out] {
  def apply(l : L, f : F) : Out
}

// Base case for HLists of length one
implicit def foldCurry1[H, Out] = new FoldCurry[H :: HNil, H => Out, Out] {
  def apply(l : H :: HNil, f : H => Out) = f(l.head)
}

// Case for HLists of length n+1
implicit def foldCurry2[H, T <: HList, FT, Out]
  (implicit fct : FoldCurry[T, FT, Out]) = new FoldCurry[H :: T, H => FT, Out] {
    def apply(l : H :: T, f : H => FT) = fct(l.tail, f(l.head))
}

// Public interface ... implemented in terms of type class and instances above
def foldCurry[L <: HList, F, Out](l : L, f : F)
  (implicit fc : FoldCurry[L, F, Out]) : Out = fc(l, f)

We can use it like this, first for your original example,

val f1 = (i : Int, j : Int, k : Int, l : Int) => i+j+k+l
val f1c = f1.curried

val l1 = 1 :: 2 :: 3 :: 4 :: HNil

// In the REPL ... note the inferred result type
scala> foldCurry(l1, f1c)
res0: Int = 10

And we can also use the same unmodified foldCurry for functions with different arity's and non-uniform argument types,

val f2 = (i : Int, s : String, d : Double) => (i+1, s.length, d*2)
val f2c = f2.curried

val l2 = 23 :: "foo" :: 2.0 :: HNil

// In the REPL ... again, note the inferred result type
scala> foldCurry(l2, f2c)
res1: (Int, Int, Double) = (24,3,4.0)

Your function expects exactly 4 Int arguments. foldLeft is a function that applies to an arbitrary number of elements. You mention List(1,2,3,4) but what if you have List(1,2,3,4,5) or List()?

List.foldLeft[B] also expects a function to return the same type B, but in your case Int and some Function1[Int, _] is not the same type.

Whatever solution you come up with would not be general either. For instance what if your function is of type (Int, Float, Int, String) => Int? You would then need a List[Any]

So it's definitely not a job for List.foldLeft.

With that in mind (warning very un-scala code):

class Acc[T](f: Function1[T, _]) {
  private[this] var ff: Any = f
  def apply(t: T): this.type = {
    ff = ff.asInstanceOf[Function1[T,_]](t)
    this
  }
  def get = ff match { 
    case _: Function1[_,_] => sys.error("not enough arguments")
    case res => res.asInstanceOf[T]
  }
}

List(1,2,3,4).foldLeft(new Acc(f.curried))((acc, i) => acc(i)).get
// res10: Int = 10

Ok no scalaz and no solution but an explanation. If you use your f.curried.apply with 1 and then with 2 arguments in the REPL observe the return-result types DO actually differ each time! FoldLeft is quite simple. It's fixed in it's type with your starting argument which is f.curried and since that has not the same signature as f.curried.apply(1) it doesn't work. So the starting argument and the result HAVE to be of the same type. The type hast to be consistent for the starting-sum element of foldLeft. And your result would be even Int so that would absolutely not work. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!