Can somebody give an example how to use ScalaZ Free monad ?
For example, if I have a simple State function and want to apply it 10,000 times, I'd get StackOverflowError:
def setS(i: Int) :State[List[Int], Unit] = State { l => ( i::l, () ) }
val state = (1 to 10000).foldLeft( put(Nil :List[Int]) ) {
case (st, i) => st.flatMap(_ => setS(i))
}
state(Nil)
As I understand, Free monad can help avoid this. How would I re-write this piece of code using Free monad to not cause stack overflow ?