vishal-h
Stream composition in Elixir
Let’s say we’ve a stream S1 that generates a random number between 1..100.
There is also another stream S2 that also generates a random number between 1..100.
Is it possible to compose S1 and S2 into another Stream S3 such that S3 emits if s1 + s2 is even? Functionality that I am looking for is similar to the one that is in RxJS.
I know the example is made up, but would like to know if streams in Elixir are composable.
Thanks!
Marked As Solved
Marcus
iex(63)> s1 = Stream.repeatedly(fn -> :rand.uniform(100) end)
#Function<54.33009823/2 in Stream.repeatedly/1>
iex(64)> s2 = Stream.repeatedly(fn -> :rand.uniform(100) end)
#Function<54.33009823/2 in Stream.repeatedly/1>
iex(65)> s3 = s1 |> Stream.zip(s2)
#Function<69.33009823/2 in Stream.zip/1>
iex(66)> s4 = Stream.map(s3, fn {a, b} -> a + b end)
#Stream<[
enum: #Function<69.33009823/2 in Stream.zip/1>,
funs: [#Function<49.33009823/1 in Stream.map/2>]
]>
iex(67)> s5 = Stream.filter(s4, fn x -> rem(x, 2) == 0 end)
#Stream<[
enum: #Function<69.33009823/2 in Stream.zip/1>,
funs: [#Function<49.33009823/1 in Stream.map/2>,
#Function<41.33009823/1 in Stream.filter/2>]
]>
iex(68)> Enum.take(s2, 5)
[31, 48, 21, 15, 7]
iex(69)> Enum.take(s3, 5)
[{97, 90}, {22, 37}, {11, 100}, {12, 44}, {24, 83}]
iex(71)> Enum.take(s4, 5)
[109, 62, 79, 105, 178]
iex(72)> Enum.take(s5, 5)
[144, 68, 34, 104, 50]
6
Also Liked
NobbZ
There are 2 means of composing streams, you can zip them or concatenate them.
Only of those makes sense in your case.
You can filter the resulting stream into another Stream.
Please take a look at
Stream.concat/2: Stream — Elixir v1.20.2Stream.zip/2: Stream — Elixir v1.20.2Stream.filter/2: Stream — Elixir v1.20.2
2
Popular in Questions
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)?
Would
mix ecto.rollback -v 200809061...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
I would like to know what is the best IDE for elixir development?
New
Other popular topics
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
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
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
I would like to know what is the best IDE for elixir development?
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New








