Now, this has to have a built-in somewhere in
Scala, because it just seems too common. So, how to convert an Array to a List in Scala?
Why do I need this? I needed to drop to Java for some functionality, which in this case returns an Array. I wanted to get that Array into a List to practice my functional programming skillz.
**Update**: I figured out how to convert Arrays to Lists the Scala way. Turns out it's a piece of cake.
val myList = List.fromArray(Array("one", "two", "three"))
or
val myList = Array("one","two","three").elements.toList
The call to elements returns an Iterator, and from there you can convert to a List via toList. Nice.
Because my first version wasn't actually tail recursive, what follows is a true tail recursive solution, if I were to implement this by hand. The above, built in mechanism is much better, though.
object ArrayUtil {
def toList[a](array: Array[a]): List[a] = {
def convert(ar…