Consider the list of string values,
lines = ["foo: 1", "bar: 2", "baz: 3"]
That one might transform into the Elixir map,
%{ "foo": "1", "bar": "2", "baz": "3"}
using a simple Enum.reduce
,
Enum.reduce(lines, %{}, fn(line, result) ->
[key, value] = String.split(line, ": ")
Map.put(result, key, value)
end)
How might one refactor this into something more succinct, specifically with the third argument passed to reduce
? For example, my instinct wants to do something like,
Enum.reduce(lines, %{}, fn(line, result) ->
String.split(line, ": ") |> Map.put(result)
end)
but the Elixir pipe operator doesn't allow piping multiple values nor piping them to anything other than the first argument.