Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created September 28, 2009 13:01
Show Gist options
  • Select an option

  • Save chrislewis/195410 to your computer and use it in GitHub Desktop.

Select an option

Save chrislewis/195410 to your computer and use it in GitHub Desktop.
/*
* A curried emulation of Iterable#map.
*
* map(List(1,2,3)) { _ + 2} //List[Int] = List(3, 4, 5)
*/
def map[T](l: List[T])(f: (T) => T): List[T] = {
def doMap[T](l: List[T], f: (T) => T, acc: List[T]): List[T] = l match {
case Nil => acc
case x :: tail => doMap(tail, f, acc ::: List(f(x)))
}
doMap(l, f, Nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment