Created
March 4, 2012 19:17
-
-
Save globulon/1974419 to your computer and use it in GitHub Desktop.
Traversable list
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
| 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