Last active
February 1, 2017 14:47
-
-
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
This file contains 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
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