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: https://hexdocs.pm/elixir/Stream.html#concat/2 -
Stream.zip/2: https://hexdocs.pm/elixir/Stream.html#zip/2 -
Stream.filter/2: https://hexdocs.pm/elixir/Stream.html#filter/2
2
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
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







