ca1989
First time trying Flow, some novice questions
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 in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Marked As Solved
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.Also Liked
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:
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
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!