There seems to be no way to split an Enum like String.split
does. Actually those functions have very different semantics.
String.split("123045067809", "0") #=> ["123", "45", "678", "9"]
function I’d like to have:
l = [1,2,3,0,4,5,0,6,7,8,0,9]
Enum.split(l, &(&1 == 0)) #=>[[1, 2, 3], [4, 5], [6, 7, 8], [9]]
this is there, but not what I want:
Enum.split_with(l, &(&1 == 0)) #=> {[0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8, 9]}
close:
Enum.chunk_by(l, &(&1 == 0)) #=> [[1, 2, 3], [0], [4, 5], [0], [6, 7, 8], [0], '\t']
I think this is the first time, I miss a funciton in stdlib, that I expected to be there.
String.split(string, pattern, options \\ [])
Divides a string into parts based on a pattern. [split]
Enum.split_with(enumerable, fun)
Splits the
enumerable
in two lists according to the given functionfun
. [split_with]