Skip to content

Instantly share code, notes, and snippets.

View globulon's full-sized avatar
💭
I may be slow to respond.

patterngazer globulon

💭
I may be slow to respond.
View GitHub Profile
@globulon
globulon / AugmentedIteratee.scala
Created March 31, 2012 16:53
Iteratee on Steroids
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))
@globulon
globulon / justStuffIter.scala
Created March 31, 2012 16:24
No fluff just stuff with Iteratee
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)))
@globulon
globulon / SimpleIter.scala
Created March 31, 2012 15:46
Simple Iteratees
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] = {
@globulon
globulon / RunIter.scala
Created March 31, 2012 15:39
Run Iteratee
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))
}
@globulon
globulon / IterEnum.scala
Created March 31, 2012 15:16
Iteratee enumeration
@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)
}
}
}
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)
}
@globulon
globulon / StreamG.scala
Created March 31, 2012 15:00
Stream definition for iteratee
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]
@globulon
globulon / foldleft.scala
Created March 31, 2012 14:42
Fold Left method
def foldLeft[B](z: B)(l: Seq[A])(f: (B, A) => B): B
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))
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")))