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
| implicit def iterateesToApplicative[E]() = new Applicative[({type λ[α] = IterV[E, α]})#λ] { | |
| def apply[T](data: T) = { | |
| Done(data, EMPTY) | |
| } | |
| def flatten[T](source: IterV[E, IterV[E, T]]): IterV[E, T] = source match { | |
| case Done(Done(v, _), s) => Done(v, s) | |
| case Done(Cont(f), s) => f(s) | |
| case Cont(f) => Cont[E, T]{ | |
| s: StreamG[E] => flatten(f(s)) |
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
| println(run(enum(length, List(1,2,3)))) | |
| println(run(enum(head[Int], List[Int](1,2,3)))) | |
| println(enum(drop[Int](1), List[Int](1,2,3))) |
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
| def head[E]: IterV[E, Option[E]] = Cont[E, Option[E]] { | |
| s: StreamG[E] => | |
| s match { | |
| case Element(e) => Done(Some(e), EMPTY) | |
| case EMPTY => head[E] | |
| case EOF => Done(None, EOF) | |
| } | |
| } | |
| def drop[E](n: Int): IterV[E, Unit] = { |
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
| def run[E, A](iter: IterV[E, A]): Option[A] = { | |
| def iterRun(next: IterV[E, A]) = next match { | |
| case Done(a, _) => Some(a) | |
| case _ => None | |
| } | |
| iter match { | |
| case Done(a, _) => Some(a) | |
| case c@Cont(_) => iterRun(c(EOF)) | |
| } |
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
| @tailrec | |
| def enum[E, A](iter: IterV[E, A], el: Seq[E]): IterV[E, A] = { | |
| (iter, el) match { | |
| case _ if el.isEmpty => iter | |
| case (Done(_, _), _) => iter | |
| case (c@Cont(_), (e :: es)) => { | |
| enum(c(Element(e)), es) | |
| } | |
| } | |
| } |
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
| sealed trait IterV[-E, +A] | |
| case class Done[E, A](value: A, stream: StreamG[E]) extends IterV[E, A] | |
| case class Cont[E, A](f: StreamG[E] => IterV[E, A]) extends IterV[E, A] { | |
| def apply[F >: E](s: StreamG[E]) = f(s) | |
| } |
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
| sealed trait StreamG[+E] | |
| final case class Element[E](data: E) extends StreamG[E] | |
| case object EOF extends StreamG[Nothing] | |
| case object EMPTY extends StreamG[Nothing] | |
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
| def foldLeft[B](z: B)(l: Seq[A])(f: (B, A) => B): B |
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
| def toCollector[A, B, U](x: A, f: (A) ⇒ B, g: U ⇒ U) = State[B,U]((s: U) ⇒ (f(x), g(s))) | |
| def collect[A, B, T[_], U](source: T[A])(f: (A) ⇒ B, g: U ⇒ U)(implicit t: Traverse[T]): State[T[B], U] = { | |
| type Projection[X] = State[X, U] | |
| implicit val A = stateApplicative[U]() | |
| t.traverse[Projection, A, B](source)((x: A) ⇒ toCollector(x, f, g)) | |
| } | |
| println((collect(List(10, 20, 30, 40))((a: Int) ⇒ 2 * a, (i: Int) ⇒ i + 1)).apply(0)) |
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
| def reduce[A, T[_]](source: T[A])(implicit t: Traverse[T], monoid: Monoid[A])= | |
| accumulate(source)(identity).apply(monoid.unit) | |
| implicit object StringMonoid extends Monoid[String] { | |
| override def add(x: String, y: String) = x + y | |
| override def unit = "" | |
| } | |
| println(reduce(List("Thumper", "is", "a", "cute", "rabbit"))) |