July 2018
Intermediate to advanced
574 pages
14h 7m
English
The first thing we want is to use Flow to count how many files each user has currently stored on ElixirDrip. In practice, we want to reduce our media_set, by incrementing each user's counter for each file. The naive approach could be something like the following:
$ cat examples/search.exsimport_file("./examples/sample_data.exs")# ...files_by_user_v1 = media_set |> Flow.from_enumerable(max_demand: 1) |> Flow.reduce(fn -> %{} end, fn media, accum -> Map.update(accum, media.user_id, 1, &(&1 + 1)) end)
However, if we run the script and check the outcome of the files_by_user_v1 flow, we will see a weird result:
# 'search.exs' already evaluatediex> files_by_user_v1 |> Enum.to_list()[{1, 1}, {3, 1}, {2, 1}, {3, 2}, {4, 1}]
The explanation ...