"blah", ...">

Using a pipe with a filter and a card in an elixir

I am trying to filter some values ​​from a map in Elixir.

It:

params = %{"blah" => "blah", "vtha" => "blah"} params |> Enum.filter fn {k, v} -> k == v end |> Enum.map(fn {k, v} -> {k, v} end) 

The result of this error: ** (FunctionClauseError). Doesn't match function suggestion in Enumerable.Function.reduce / 3

But filter and card operations work in isolation.

 Enum.filter params, fn {k, v} -> k == v end Enum.map(params, fn {k, v} -> {k, v} end) 

They do not work on channel transmission.

I am sure that I am missing something obvious.

+7
functional-programming elixir
source share
1 answer

EDIT On the Elixir main branch, the compiler will warn if the function is passed without parentheses, if there are arguments.


You need an explicit bracket for Enum.filter , since a function call takes precedence over a pipe statement.

 params = %{"blah" => "blah", "vtha" => "blah"} params |> Enum.filter(fn {k, v} -> k == v end) |> Enum.map(fn {k, v} -> {k, v} end) 

Please see Why I can not bind String.replace? for a more detailed explanation.

+11
source share

All Articles