You are viewing a single comment's thread. Return to all comments →
To avoid using math.abs:
math.abs
def f(arr: List[Int]): List[Int] = arr map (x => x * x.signum)
And if you wanna do that with tail recursion:
def f(arr: List[Int]): List[Int] = { @annotation.tailrec def run(i: Int, res: List[Int]): List[Int] = arr.length match { case `i` => res case _ => run(i + 1, res :+ arr(i) * arr(i).signum) } run(0, List.empty[Int]) }
Seems like cookies are disabled on this browser, please enable them to open this website
Update List
You are viewing a single comment's thread. Return to all comments →
To avoid using
math.abs
:And if you wanna do that with tail recursion: