I'm trying to increase a variable only during certain conditions
Enum.reduce(items, 0, fn item, acc ->
if item.condition do
acc = acc+1
Logger.info acc
end
end)
But i get
** (ArithmeticError) bad argument in arithmetic expression
I'm trying to increase a variable only during certain conditions
Enum.reduce(items, 0, fn item, acc ->
if item.condition do
acc = acc+1
Logger.info acc
end
end)
But i get
** (ArithmeticError) bad argument in arithmetic expression
The result returned by the function is used as the accumulator for the next iteration, recursively.
Logger.info returns :ok so you probably don't want that to be your last line.
You must also return acc if the condition doesn't meets.
Try with:
Enum.reduce(items, 0, fn item, acc ->
if item.condition, do: acc + 1, else: acc
end)