Created
March 14, 2012 20:29
-
-
Save LeifW/2039286 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // sourceList from CL re-written how it is in haskell-conduit now. | |
| def sourceList[F[_], A](l: => Stream[A])(implicit M: Monad[F]): Source[F, A] = l match { | |
| case Stream.Empty => Closed[F, A] | |
| case x #:: xs => Open(sourceHandle(xs), M.point(()), x) | |
| } | |
| // sans IO monad: | |
| def sourceHandle(h: InputStream): Source[Id, Array[Byte]] = { | |
| val arrayBuf = new Array[Byte](4096) | |
| val r = h.read(arrayBuf) | |
| if (r == -1) | |
| Closed[Id, Array[Byte]] | |
| else | |
| Open(sourceHandle(h), ???, arrayBuf take r) | |
| } | |
| // With IO monad, ported straight from Haskell | |
| def sourceHandle[F[_]](h: InputStream)(implicit M: Monad[F]): Source[F, Array[Byte]] = { | |
| lazy val pull:F[Source[F, Array[Byte]]] = IO{ | |
| val arrayBuf = new Array[Byte](4096) | |
| val r = h.read(arrayBuf) | |
| if (r == -1) | |
| None | |
| else | |
| Some(arrayBuf take r) | |
| }.liftIO map { | |
| case None => Closed[F, Array[Byte]] | |
| case Some(bytes) => Open(src, close, bytes) | |
| } | |
| val close:F[Unit] = M.point(()) | |
| lazy val src = SourceM(pull, close) | |
| src | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment