To complete the list of possible alternatives, reduce
could be used to implement the behavior of flatten:
var a = ["a", "b", "c"]
var b = ["d", "e", "f"]
let res = [a, b].reduce([],combine:+)
The best alternative (performance/memory-wise) among the ones presented is simply flatten
, that just wrap the original arrays lazily without creating a new array structure.
But notice that flatten does not return a LazyCollection
, so that lazy behavior will not be propagated to the next operation along the chain (map, flatMap, filter, etc...).
If lazyness makes sense in your particular case, just remember to prepend or append a .lazy
to flatten()
, for example, modifying Tomasz sample this way:
let c = [a, b].lazy.flatten()