I would like to figure out the most pragmatic way to accept an array (or list) and append to the data structure. Then finally return the new data structure.
Something like this:
def template(array: Array[String]): Array[Nothing] = {
val staging_path = "s3//clone-staging/"
var path_list = Array()
//iterate through each of the items in the array and append to the new string.
for(outputString <- array){
var new_path = staging_path.toString + outputString
println(new_path)
//path_list I thought would add these new staging_path to the array
path_list +: new_path
}
path_list(4)
}
However, calling a single index of the data structure as a shanty way of checking existence, path_list(4) returns an Out of Bounds.
Thanks.
I think you just want to use map
here:
val staging_path = "s3//clone-staging/"
val dirs = Array("one", "two", "three", "four", "five")
val paths = dirs.map(dir => staging_path + dir)
println(paths)
// result: paths: Array[String] = Array(s3//clone-staging/one, s3//clone-staging/two, s3//clone-staging/three, s3//clone-staging/four, s3//clone-staging/five)
println(paths.length)
// result: 5
In functional programming land you are generally trying to avoid mutations. Instead, think of it as transforming your input array into a new array.