apoorv-2204
The issue:
I would like to consume stream one by one.But its not possible look below examples.
iex(1)> s = 1..1000 |> Stream.map(& "Consumed #{&1}")
#Stream<[enum: 1..1000, funs: [#Function<49.82544474/1 in Stream.map/2>]]>
iex(2)> s |> Enum.take(1)
["Consumed 1"]
iex(3)> s |> Enum.take(1)
["Consumed 1"]
iex(4)> s |> Enum.take(2)
["Consumed 1", "Consumed 2"]
iex(5)> s |> Enum.take(2)
["Consumed 1", "Consumed 2"]
iex(6)> s |> Enum.take(1)
["Consumed 1"]
iex(7)> s |> Enum.take(1)
["Consumed 1"]
iex(8)> s |> Enum.take(1)
["Consumed 1"]
iex(9)> s |> Enum.take(1)
["Consumed 1"]
Expectations: When I do Enum.take(1) , I expect it to consume one element, and When I do I expect it to return the next element.
s |> Enum.take(1) =>["Consumed 1"]
s |> Enum.take(1) =>["Consumed 2"]
s |> Enum.take(1) =>["Consumed 3"]
s |> Enum.take(1) =>["Consumed 4"]
s |> Enum.take(1) =>["Consumed 5"]
s |> Enum.take(1) =>["Consumed 6"]
What is that I am doing wrong?
How to achieve this behaviour.?
I impl this behaviour using processes, but I am not sure how valid it is.See below
defmodule StatfulStream do
@moduledoc false
def start(stream, fx) do
spawn(fn ->
stream
|> Stream.map(fn chunk ->
fx.(chunk)
IO.inspect(chunk, label: :inside)
wait()
chunk
end)
|> Enum.to_list()
|> IO.inspect(label: :end)
end)
end
def wait() do
receive do
:process ->
:ok
end
end
def ge_str() do
Stream.resource(
fn -> 1 end,
fn
10 ->
{:halt, 10}
1 ->
{[1], 2}
v ->
{[v], v + 1}
end,
fn v -> IO.inspect(v) end
)
end
def r() do
str = ge_str()
fx = fn r ->
IO.inspect(r, label: :fx)
end
start(str, fx)
end
def send(p) do
Process.send(p, :process, [])
end
end
This is how ran it.
iex(1)> import StatfulStream
StatfulStream
iex(2)> pid= r()
fx: 1
#PID<0.237.0>
inside: 1
iex(3)> send(pid)
fx: 2
:ok
inside: 2
iex(4)> send(pid)
fx: 3
:ok
inside: 3
iex(5)> send(pid)
fx: 4
:ok
inside: 4
iex(6)> send(pid)
fx: 5
:ok
inside: 5
iex(7)> send(pid)
fx: 6
:ok
inside: 6
iex(8)> send(pid)
fx: 7
:ok
inside: 7
iex(9)> send(pid)
fx: 8
:ok
inside: 8
iex(10)> send(pid)
fx: 9
:ok
inside: 9
iex(11)> send(pid)
10
:ok
end: [1, 2, 3, 4, 5, 6, 7, 8, 9]
iex(12)> send(pid)
:ok
iex(13)>
What you think ? whats valid? , and How to impl it?
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
That’s not what should happen. Elixir doesn’t differenciate “streams” – lazily producing enumerables – from other enumerables like lists or maps – all of them are Enumerables. Hence
Enumworks exactly the same if you provide either of those – each individual call ofEnum.take(enumerable, 1)gives you a list with the first item of the enumerable, which means the same result given the enumerable stayed the same. If there are sideffects they’re hidden behind the enumerable interface. Iterating over an enumerable doesn’t change the input enumerable.You could probably build an api over
Enumerable.reducefor stateful iteration of an enumerable, but the intermediate states wouldn’t necessarily themselves be enumerable. So an enumerable could just be the input to such a new api.apoorv-2204
But the issue is I am passing ref here and there b/w functions I(.e in genserver handle info )and want to resume it where i left it.
LostKobrakai
That’s not how the
Enumerableabstraction works though. I don’t know what you’re streaming there, but I’d consider using other apis instead of an enumerable.apoorv-2204
I am streaming here a S3 object, or a file. And I am sending it to 3310 port to clamd for scanning
Ecclesiastes
Start a GenServer to handle the streaming logic.
Initialize GenServer state with an offset (e.g.,
0) ininit/1.store the stream object in an ETS table.
krasenyp
Or, use the process state as storage.