Skip to content

Instantly share code, notes, and snippets.

@ceedubs
Last active August 29, 2015 14:07
Show Gist options
  • Save ceedubs/f8273ede78f86df7df7f to your computer and use it in GitHub Desktop.
Save ceedubs/f8273ede78f86df7df7f to your computer and use it in GitHub Desktop.
Turning List[Int => Int] into Int => List[Int] with sequenceU
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