A marker trait that enables curried varargs.
A marker trait that enables curried varargs.
Given a function call f(a, b, c)
,
when f
is a subtype of Curried,
it should be rewritten to f.applyBegin.applyNext(a).applyNext(b).applyNext(c).applyEnd
.
Optionally, some arguments to a Curried call may be a sequence argument marked as _*
.
Given a function call f(p1, s1: _*, p2)
,
when translating it to the curried form,
the sequence argument will becomes a foldLeft
call.
f.applyBegin .applyNext(p1) .applyNextSeq(s1) .applyNext(p2) .applyEnd
Unlike traditional repeated parameters, which restrict the sequence argument at the last position, sequence arguments in a curried call are allowed at any position.
When a Curried is invoked with some type arguments,
those type arguments will be moved to the applyBegin
method.
Therefore, List[Int](1 to 3: _*)
should be translated to (1 to 3).foldLeft(List.applyBegin[Int])(_.applyNext(_)).applyEnd
.
Fast list builder
class PartiallyAppliedInitializer[A](builder: collection.mutable.Builder[A, List[A]]) { def applyEnd = builder.result def applyNextSeq(seq: Seq[A]) = { builder ++= seq this } def applyNext(a: A) = { builder += a this } } object FastListInitializer extends Curried { def applyBegin[A]:PartiallyAppliedInitializer[A] = new PartiallyAppliedInitializer(List.newBuilder[A]) } FastListInitializer(0, 4) should be(List(0, 4)) FastListInitializer[Int](0, 100 to 103: _*, 1) should be(List(0, 100, 101, 102, 103, 1))