I am iterating a list with indexes (Enum.with_index), and based on the first matching condition ( against the value in a map for list element), I wish to split the list into 2 lists at that index.
For example: map= ['a': false, 'b':'false, 'c':true, 'd':false]
my list: [:d, :a, :c, :b]
Expected Output: [:d, :a, :c], [:b]
My approach: Iterate through the list and if the value (first occurrence) is true in map, return the index, and then split the original list. If no value from the list is true in the map, then I wish to return [ ], [d, a, c, b]
However, I am struggling to return the index at all from Enum.each
.
splitter = fn(list) ->
list
|> Enum.with_index
|> Enum.each(fn({elem, i}) ->
if map[elem] do
i
end
end)
end
IO.puts "#{splitter.(list)}"