Suppose I have a list of numbers and list of functions:
val xs: List[Int] = List(1, 2, 3)
val fs: List[Int => Int] = List(f1, f2, f3)
Now I would like to use an Applicative
to apply f1
to 1
, f2
to 2
, etc.
val ys: List[Int] = xs <*> fs // expect List(f1(1), f2(2), f3(3))
How can I do it with Scalaz
?
pure
for zip lists repeats the value forever, so it's not possible to define a zippy applicative instance for Scala'sList
(or for anything like lists). Scalaz does provide aZip
tag forStream
and the appropriate zippy applicative instance, but as far as I know it's still pretty broken. For example, this won't work (but should):You can use the applicative instance directly (as in the other answer), but it's nice to have the syntax, and it's not too hard to write a "real" (i.e. not tagged) wrapper. Here's the workaround I've used, for example:
And then:
For what it's worth, it looks like this has been broken for at least a couple of years.