Reputation: 8461
I'm getting this error when trying to sum a list I'm getting back from a comprehension:
range = 1..999
multiple_of_3_or_5? = fn(n) -> (rem(n, 3) == 0 || rem(n, 5) == 0) end
IO.inspect for n <- range, multiple_of_3_or_5?.(n),
do: Enum.reduce n, 0, fn(x) -> x end
#=> ** (FunctionClauseError) no function clause matching in Enum.reduce/3
Why am I getting this error?
Upvotes: 0
Views: 1251
Reputation: 1081
The first and third param are wrong. You can try this
range = 1..999
multiple_of_3_or_5? = fn(n) -> (rem(n, 3) == 0 || rem(n, 5) == 0) end
for n <- range, multiple_of_3_or_5?.(n) do n end |> Enum.reduce(0,
fn(x, acc) -> x + acc end)
or
range = 1..999
multiple_of_3_or_5? = fn(n) -> (rem(n, 3) == 0 || rem(n, 5) == 0) end
Enum.reduce_while(range, 0, fn i, acc ->
if multiple_of_3_or_5?.(i), do: {:cont, acc + i}, else: {:cont, acc}
end)
Upvotes: 0
Reputation: 23955
The function in the third parameter of Enum.reduce
needs to have two parameters, the element from the enumerable and an accumulator. You currently only have one parameter, x
.
Upvotes: 1