Created
July 11, 2012 17:13
-
-
Save mpilquist/3091801 to your computer and use it in GitHub Desktop.
scalaz-seven enumerator examples
This file contains 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
def enumLines[F[_]](r: => BufferedReader)(implicit MO: MonadPartialOrder[F, IO]): EnumeratorT[IoExceptionOr[String], F] = | |
new EnumeratorT[IoExceptionOr[String], F] { | |
import MO._ | |
import EnumeratorT._ | |
lazy val reader = r | |
def apply[A] = (s: StepT[IoExceptionOr[String], F, A]) => | |
s.mapContOr({ | |
k => { | |
val i = IoExceptionOr(reader.readLine) | |
if (i exists { _ != null }) k(Input(i)) >>== apply[A] | |
else s.pointI | |
} | |
}, { IoExceptionOr(reader.close()); s.pointI }) | |
} | |
def enumIgnoringIoExceptions[A, F[_]](e: EnumeratorT[IoExceptionOr[A], F])(implicit M: Monad[F]): EnumeratorT[A, F] = { | |
e flatMap { i => i.toOption.fold( | |
ii => ii.point[({type l[a] = EnumeratorT[a, F]})#l], | |
implicitly[Monoid[EnumeratorT[A, F]]].zero | |
) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment