Last active
May 8, 2017 23:31
-
-
Save padde/a759b18bca7851e8ffaa to your computer and use it in GitHub Desktop.
Filter a list by pattern matching in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyEnum do | |
defmacro filter_matching(collection, pattern) do | |
quote do | |
Enum.filter unquote(collection), fn | |
unquote(pattern) -> true | |
_ -> false | |
end | |
end | |
end | |
end | |
require MyEnum | |
MyEnum.filter_matching [a: 1, b: 2, a: 3], {:a, _} | |
#=> [a: 1, a: 3] |
One other approach is to just write two functions, one that matches the true case, and another that matches the false case and just pass that function. Especially helpful if you want to negate some condition such as nil in a parameter.
zip_rates
|> Enum.filter(&zip_nil/1)
def zip_nil { _zip, nil } do
false
end
def zip_nil { _zip, _price } do
true
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following should work:
Because of the way macros work, this is impossible to do without a macro.