apoorv-2204

apoorv-2204

Is Stateful Stream possible?

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?

Most Liked

LostKobrakai

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 Enum works exactly the same if you provide either of those – each individual call of Enum.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.reduce for 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.

krasenyp

krasenyp

Or, use the process state as storage.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement