Last active
August 29, 2015 14:07
-
-
Save ceedubs/f8273ede78f86df7df7f to your computer and use it in GitHub Desktop.
Turning List[Int => Int] into Int => List[Int] with sequenceU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz.std.list._ | |
import scalaz.std.function._ | |
import scalaz.syntax.traverse._ | |
val add1: Int => Int = _ + 1 | |
val times2: Int => Int = _ * 2 | |
val squared: Int => Int = n => n * n | |
// sequenceU requires that List has a Traverse instance | |
// and that Function1 (for Int => Int in this case) has an Applicative instance | |
// luckily, scalaz provides both | |
val combined: Int => List[Int] = List(add1, times2, squared).sequenceU | |
combined(4) // List(5, 8, 16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment