elixir - How do I map any given function over a list of values? -
i splitting string on character , trim items in resulting split. expect following work string.trim/1
exists:
iex> "my delimited ! string of doom" |> string.split("!") |> enum.map(string.trim) ** (undefinedfunctionerror) function string.trim/0 undefined or private. did mean 1 of: * trim/1 * trim/2 (elixir) string.trim()
i receive undefinedfunctionerror
indicating function string.trim/0
not exist. want accomplished anonymous function passed enum.map
:
iex> "my delimited ! string of doom" |> string.split("!") |> enum.map(fn (word) -> string.trim(word) end) ["my delimited", "string of doom"]
does enum.map/2
require anonymous function second parameter? possible give desired function parameter?
you need use & operator
. capture operator
try this:
iex()> "my delimited ! string of doom" |> string.split("!") |> enum.map(&string.trim/1) ["my delimited", "string of doom"]
Comments
Post a Comment