Skip to content

Instantly share code, notes, and snippets.

@globulon
Created March 4, 2012 19:17
Show Gist options
  • Select an option

  • Save globulon/1974419 to your computer and use it in GitHub Desktop.

Select an option

Save globulon/1974419 to your computer and use it in GitHub Desktop.
Traversable list
trait ListLike[C[_]] {
def cons[T](x: T, xs: C[T]): C[T]
def empty[T](): C[T]
def first[T](source: C[T]): Option[T]
def rest [T](source: C[T]): C[T]
}
trait TraverseListLike[C[_]] extends Traverse[C]{
self : ListLike[C] =>
override def traverse[M[_], T, U](source: C[T])(f: (T) ⇒ M[U])(implicit applicative: Applicative[M]): M[C[U]] = {
first(source) match {
case Some(value) ⇒
cons[U] _ :@:f(value):*:traverse(rest(source))(f)
case _ ⇒ applicative(empty[U]())
}
}
}
implicit object traversableList extends TraverseListLike[List] with ListLike[List]{
def empty[T]() = Nil
def cons[T](x: T, xs: List[T]) = x::xs
def first[T](source: List[T]) = source.headOption
def rest[T](source: List[T]) = source.tail
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment