I have a list like:
val arr = Array(\"a\", \"\", \"\", \"b\", \"c\", \"\")
I am looking for a way to create:
Array(\"a\", \"a\"
I think Mohitt's answer is a clear solution ... but Pablo Francisco Pérez Hidalgo is right that there are side effects
Maybe we could include the variable inside of a function to avoid changing the temp variable by some other developers
(x: Array[String]) => { var last = ""; x.map { v => if (v != "") last = v; last } }
to be used like this:
val fillEmpty = (x: Array[String]) => { var last = ""; x.map { v => if (v != "") last = v; last } }
fillEmpty(arr)