Ok, here’s the problem:
Given a map of variable keys and values, select a set of those keys which may be either nil
, false
, or true
. Only return true if there is at least one true value and no false values.
Here is an example map I have, where I would be extracting the 3 matches_
keys:
%{
random_key: 123,
another: 456,
matches_1: nil,
matches_2: true,
matches_3: false
}
In the above, the check would fail because the matches include false. Here is another false (as no true values):
%{
random_key: 123,
another: 456,
matches_1: nil,
matches_2: nil,
matches_3: nil
}
Here is a pass:
%{
random_key: 123,
another: 456,
matches_1: nil,
matches_2: true,
matches_3: nil
}
My attempt at this:
def pass?(map) do
Map.take(map, [:matches_1, :matches_2, :matches_3])
|> Map.values
|> Enum.reject(&is_nil/1)
|> (fn(values) -> length(values) > 0 && Enum.all?(values, fn x -> x == true end) end).()
end
Would love to see if this can be more elegantly expressed. The crux of the conditions and what I’m not that happy with is the last line:
(fn(values) -> length(values) > 0 && Enum.all?(values, fn x -> x == true end) end).()