Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Created December 19, 2011 13:02
Show Gist options
  • Save halcat0x15a/1497125 to your computer and use it in GitHub Desktop.
Save halcat0x15a/1497125 to your computer and use it in GitHub Desktop.
import IterV._
implicit lazy val StreamEnumerator: Enumerator[Stream] = new Enumerator[Stream] {
def apply[E, A](e: Stream[E], i: IterV[E, A]): IterV[E, A] = e match {
case Stream() => i
case x #:: xs => i.fold(done = (_, _) => i, cont = k => apply(xs, k(El(x))))
}
}
head(Stream(3, 4, 6)).run assert_=== Some(3)
head(Stream(1, 2, 3)).run assert_=== Some(1)
peek(Stream(1, 2, 3)).run assert_=== Some(1)
length(Stream(1, 2, 3)).run assert_=== 3
drop(2)(Stream(1, 2, 3)).run assert_=== ()
collect[Int, List].apply(Stream(1, 2, 3)).run assert_=== List(1, 2, 3)
groupBy[Int, List](_ === _).apply(Stream(1, 1, 2, 1, 1, 2)).run assert_=== List(1, 1)
takeWhile[Int, List](_ % 2 === 0).apply(Stream(2, 4, 5, 6)).run assert_=== List(2, 4)
reversed[Int, List](ListReducer)(Stream(1, 2, 3)).run assert_=== List(3, 2, 1)
repeat[Int, List[Int], List](groupBy[Int, List](_ === _)).apply(Stream(1, 1, 2, 1, 1, 2)).run assert_=== List(List(1, 1), List(2), List(1, 1), List(2))
peekDoneOr[Int, List[Int]](5.pure[List], i => takeWhile(_ lt 3))(Stream(1, 2, 3)).run assert_=== List(1, 2)
(drop[Int](2) >|> length).apply(Stream(1, 2, 3)).run assert_=== 1
(drop[Int](1) >|> collect[Int, List]).apply(Stream(1, 2, 3)).run assert_=== List(2, 3)
(head[Int] >>= (o => head.map(_ |+| o))).apply(Stream(1, 5, 3)).run assert_=== Some(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment