Skip to content

Instantly share code, notes, and snippets.

@blancosj
Last active February 1, 2017 14:47
Show Gist options
  • Save blancosj/467934a21db255d1c01f2505e019741d to your computer and use it in GitHub Desktop.
Save blancosj/467934a21db255d1c01f2505e019741d to your computer and use it in GitHub Desktop.
Implementation of the Map (higher-order function) for generic List collections with tail recursion
def tMap[T](tail: List[T], f: T => T): List[T] = {
@tailrec
def _q(tail: List[T], acc: List[T] = List()): List[T] = {
tail match {
case Nil => rev(acc)
case x :: xs => {
_q(xs, f(x) :: acc)
}
}
}
_q(tail)
}
@tailrec
def rev[T](tail: List[T], acc: List[T] = List()): List[T] = {
tail match {
case Nil => acc
case x :: xs => rev(xs, x :: acc)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment