Let's say I have a method that turns (a function on two elements) into (a function on two sequences):
def seqed[T](f: (T,T) => T): (Seq[T], Seq[T]) => Seq[T] = (_,_).zipped map f
In words, the resulting function takes two sequences xsand yscreates a new sequence consisting of (xs(0) f ys(0), xs(1) f ys(1), ...)
So, for example, if xss- Seq(Seq(1,2),Seq(3,4))and f- (a: Int, b: Int) => a + b, we can call it this way:
xss reduceLeft seqed(f) // Seq(4, 6)
or with an anonymous function:
xss reduceLeft seqed[Int](_+_)
This is pretty good; it would be nice to get rid of the type argument [Int], but I don't see how (any ideas?).
To make it tupledlook a bit like a method , I also tried the my-library enrichment pattern:
class SeqFunction[T](f: (T,T) => T) {
def seqed: (Seq[T], Seq[T]) => Seq[T] = (_,_).zipped map f
}
implicit def seqFunction[T](f: (T,T) => T) = new SeqFunction(f)
For a predefined function, this works fine, but ugly with anonymous
xss reduceLeft f.seqed
xss reduceLeft ((_:Int) + (_:Int)).seqed
, , , :
xss reduceLeft (_+_).seqed
xss reduceLeft (_+_).seqed[Int]
? ?