If you want your functions to have access to a shared environment
read-only, then the Reader monad is what you want. This works with
scalaz:
scala> val g: Int => String = _.toString
g: (Int) => String = <function1>
scala> val f: Int => Int = _ + 1
f: (Int) => Int = <function1>
scala> val e: Int => Boolean = _ % 2 == 0
e: (Int) => Boolean = <function1>
scala> for {
| x <- e
| y <- f
| z <- g
| }
| yield (x, y, z)
res2: (Int) => (Boolean, Int, String) = <function1>
scala> res2(3)
res3: (Boolean, Int, String) = (false,4,3)
If you want the functions to be able to read and write the
environment, then the monad you want is State:
scala> val fib = state((s:(Int, Int)) => ((s._2, s._1 + s._2), s._2))
fib: scalaz.State[(Int, Int),Int] = scalaz.States$$anon$1@4e1abb41
scala> Stream.continually(fib).take(10).sequence[({type
lam[x]=State[(Int, Int),x]})#lam, Int]
res30: scalaz.State[(Int, Int),scala.collection.immutable.Stream[Int]]
= scalaz.States$$anon$1@426a7e13
scala> res30 ! (0,1) toList
res31: List[Int] = List(1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
This is equivalent to:
for { a1 <- fib; a2 <- fib; a3 <- fib; ... } yield List(a1, a2, a3, ...)
As you can see, the fib function both reads the state and modifies it
before it's fed to the next call to fib.
Runar
> --
> You received this message because you are subscribed to the Google Groups
> "scalaz" group.
> To post to this group, send email to sca...@googlegroups.com.
> To unsubscribe from this group, send email to
> scalaz+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/scalaz?hl=en.
>