--
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.
Since Foldable[Stream].foldRight isn't tail recursive that might be
a problem when dealing with 'big' streams (those of mine have 10k
elements). For me this isn't a blocking issue since I've got several
workarounds (but traverse seems to be more elegant)
Traversing a big strict list won't get you a SOE if you are using the
last snapshot.
Traversing a 'huge' lazy list causes a SOE on Haskell too
On Dec 2, 8:44 am, etorreborre <etorrebo...@gmail.com> wrote:
> I also suspect that the solution is described here<http://apocalisp.wordpress.com/2011/10/26/tail-call-elimination-in-sc...>
--
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.
--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To view this discussion on the web visit https://groups.google.com/d/msg/scalaz/-/Srypc61Agg0J.
--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To view this discussion on the web visit https://groups.google.com/d/msg/scalaz/-/WwMFHjw5oTcJ.
None of that actually requires reifying the stream. You should never
have a Stream or a Seq in the first place. Here's something with
Iteratees:
val noop = ().pure[IO]
def processLines: Iteratee[IO, String, Unit] = {
def go(n: Int) = ContM((x: Input[String]) => Iteratee(step(n, x))).pure[IO]
def step(n: Int = 0, i: Input[String]): IO[IterVM[IO, String, Unit]]
= i match {
case El(e) => for {
_ <- putStrLn(e) // simulate database call
_ <- if (n % 10 == 0)
putStrLn("Processed " + n + " lines")
else noop
next <- go(n + 1)
} yield next
case Empty() => go(n)
case e@EOF() => DoneM((), e).pure[IO]
}
Iteratee(go(0))
}
type ProcessLines[A] = Iteratee[IO, String, A] => Iteratee[IO, String, A]
def processReaderLines[A](r: => BufferedReader): ProcessLines[A] = it => {
def loop(i: IterVM[IO, String, A]): IO[IterVM[IO, String, A]] = i.fold(
done = (_,_) => io { i },
cont = k => for {
s <- rReadLn(r)
a <- s.traverse(l => for {
v <- k(El(l)).value
x <- loop(v)
} yield x)
b <- a.map(_.pure[IO]).getOrElse(k(EOF[String]).value)
} yield b)
Iteratee(it.value >>= loop)
}
def processFileLines(f: File): Iteratee[IO] = it =>
Iteratee(bufferFile(f).bracket(closeReader)(processReaderLines(_)(it)))
Then you can say:
processFileLines(new File("myFile.txt"))(processLines).unsafePerformIO
On Tue, Dec 6, 2011 at 2:44 AM, etorreborre <etorr...@gmail.com> wrote:
> --
> You received this message because you are subscribed to the Google Groups
> "scalaz" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/scalaz/-/WwMFHjw5oTcJ.
One more question: is that possible to write a tail-recursive version of StreamT[M, A].toStream when M is a general monad (not Id)?E.
--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To view this discussion on the web visit https://groups.google.com/d/msg/scalaz/-/LhPXX5xlrVsJ.
Can you elaborate? I thought it might be possible via trampoline. What is the tail recursive implementation for the free monad?
Runar