@CommandoSev it is a list acting as an accumulator. Here are some examples:
- Find out if something exists in a list:
list = [1,2,3,4]
Enum.any?(list, fn item -> item == 3 end)
true
- If you want to get the item if it exists:
list = [1,2,3,4]
Enum.find(list, fn item -> item == 3 end)
3
- If you want to find all of the matches and have them returned in a list:
list = [1, 2, 3, 4, 3]
# The second arg is the accumulator which I have named acc in the anonymous function
Enum.reduce(list, [], fn item, acc ->
# If true add the item to the acc list, otherwise return the acc as is
if item == 3, do: [item | acc], else: acc
end)
[3,3]
You can replicate the Enum.reduce function by writing your own recursive function:
list = [1, 2, 3, 4, 3]
# match on the empty list and return the accumulator
def my_reduce([], acc, _item), do: acc
# match on the head and tail of the list and test if item exists
def my_reduce([h | t], acc, item) do
acc =
if h == item do
[h | acc] # add the matching item to the accumulator
else
acc # no match so return acc as is
end
my_reduce(t, acc, item) # recursivly call my_reduce with the tail of the list and the acc
end
# start the recursive function by calling it
my_reduce(list, [], 3)
Hope this helps. If you tell us exactly what you are trying to do I am sure we can help work it out.
Andrew


















