ca1989
Today I started studying Flow, and I have some questions.
Please keep in mind that this is an example code, I want to use Flow.
So, it’s:
- Reading a text file
- Remove empty lines and newlines
- Count the words
- Generate a comma separated string line with word and count
This is what I’ve done so far:
def count_words(filepath) do
File.stream!(filepath)
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split(&1, " "))
|> Flow.reject(&(&1 == "\n"))
|> Flow.map(&String.replace(&1, "\n", ""))
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, acc ->
Map.update(acc, word, 1, &(&1 + 1))
end)
|> Enum.map(fn {word, count} -> "#{word},#{count}\n" end)
end
My questions:
- Is it ok to chain multiple Flow operations? (
reject,map…) or should I put everything in the reducer function? - IIUC The last
Enum.mapis not executed in parallel, but if I try to useFlow.mapI got this error:
** (ArgumentError) map/2 cannot be called after group_by/reduce/emit_and_reduce operation (use on_trigger/2 if you want to further emit events or the accumulated state)
which is quite informative, but still don’t get it
Cheers!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Yes.
Putting everything in the reducer would create a flow that slightly shuffles your elements and then effectively reduces serially.
Because there are no events anymore after the
reduce, you just have a single value that was reduced into.ca1989
Ok, thanks.
I am quite embarassed by that. For some reasons, I was thinking in Enumerables when Flow functions works on Flows (Flow — Flow v1.2.4).
(Sorry for the very silly questions, I’m studying Flow alone and I have no one to ask silly questions to…)
Cheers!
NobbZ
As with the
Enumfunctions, theFlowfunctions operate on flows, yes, but they don’t necessarily return a flow.ca1989
Now my question is:
Should I create another Flow from the result of the reduce?
Cheers
LostKobrakai
Flow.reducereturns a flow, so you should be able to chain furtherFlowfunctions.NobbZ
Hmmm… Seems as if I so far totally misunderstood the use of
Flow.reduce/3then…ca1989
Flow.reducedoes indeed return aFlow, but I can’t callFlow.mapon it, as I get the error I posted in the original post.Following the suggestion in the error message, I was able to use
on_triggeron the flow, to map over the result ofFlow.reduce:IIUC the
Enum.mapinside theon_triggerrun sequentially on the batches, while the trigger function itself is parallel.Thank you
fireside68
I’m here because I am working through the Concurrent Data Processing in Elixir book. There’s a point in the bit on Flows where there is an error in the code. On page 102, this is suggested:
The result of this code is a similar error:
To anyone else who may have arrived by the same path, making this change will allow you to continue unabated: