I'm trying to split an incoming stream of strings into cumulative tokens per line item using a function below,
def cumulativeTokenise(string: String): Array[String] = {
val array = string.split(" +")
var result: Array[String] = Array()
array.map { i => (
result = result :+ (
if (result.lastOption == None) i
else result.lastOption.getOrElse("")+ " " + i
)
)
}
result
}
Ex: output of cumulativeTokenise("TEST VALUE DESCRIPTION . AS") would be => Array(TEST, TEST VALUE, TEST VALUE DESCRIPTION, TEST VALUE DESCRIPTION ., TEST VALUE DESCRIPTION . AS)
Trying to figure out if there's another efficient in-built method in Scala or better ways of doing it with FP, without any mutable array. Any help is much appreciated.