I’m failing to understand what Elixir is doing in this function. The objective is to create a map with each word as a key and how many times that word appears in the sentence as its value.
For example:
sentence = "one fish two fish red fish blue fish"
First the sentence is split into a list and then I get lost around the functionality. How does Elixir pass in arguments into increment/2
?
@spec count(String.t()) :: map
def count(sentence) do
sentence
|> String.split(" ")
|> Enum.reduce(%{}, &increment/2)
end
defp increment(word, map) do
Map.update(map, word, 1, &(&1 + 1))
end
end
Also, regarding the Map.update
function, I see nowhere in the docs where it says it loops and accumulates; so confused as to how that is happening.