Skip to Main Content
Mastering Elixir
book

Mastering Elixir

by André Albuquerque, Daniel Caixinha
July 2018
Intermediate to advanced content levelIntermediate to advanced
574 pages
14h 7m
English
Packt Publishing
Content preview from Mastering Elixir

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Testing Elixir

Testing Elixir

Andrea Leopardi, Jeffrey Matthias

Publisher Resources

ISBN: 9781788472678Supplemental Content