Lazy queries

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 ...

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.