How can I combine two Function0 val's into one Function0
I imagine something like this:
def combine[A, B, C](f: (A, B) => C): (M[A], M[B]) => M[C]
while M
would be Function0
. Is this possi开发者_运维技巧ble in scalaz?
import scalaz._; import Scalaz._
def combine[A, B, C, M: Applicative](f: (A, B) => C) =
(ma: M[A], mb: M[B]) => (ma |@| mb)(f)
In standard scala it is not that hard, simply stick to your signature:
def combine[A,B,C]( f: (A,B) => C )
= ( fA:(()=>A), fB:(()=>B) ) => f( fA(), fB() )
Here is a small example:
scala> val isProdPositive = combine( (i:Int,d:Double) => i*d > 0.0 )
isProdPositive: (() => Int, () => Double) => Boolean = <function2>
scala> val f1 = () => 2
f1: () => Int = <function0>
scala> val f2 = () => -1.5
f2: () => Double = <function0>
scala> isProdPositive(f1,f2)
res1: Boolean = false
精彩评论