Stream.flat_map influences the behaviour of preceeding Stream operations in a pipe

70 views
Skip to first unread message

Paul Balomiri

unread,
Sep 10, 2016, 8:05:05 AM9/10/16
to elixir-lang-talk
Hi,

Today i banged my head against this one :

iex(13)> [[true,false],[false],[true]]|>Stream.filter(fn([x|tail])-> IO.inspect([filter: x]); x end) |>Stream.take(1) |> Stream.flat_map(fn(x)-> IO.inspect([flat_map: x]); x end)  |> Enum.to_list()
[filter: true]
[flat_map: [true, false]]
[filter: false]
[filter: true]
[true, false]


taking away the culprit `Stream.flat_map` prevents the execution of `Stream.filter`'s function after the first element:
[[true,false],[false],[true]]|>Stream.filter(fn([x|tail])-> IO.inspect([filter: x]); x end) |>Stream.take(1) |> Enum.to_list()
[filter: true]
[[true, false]]

It seems to me that this is a bug, but what i find more puzzling is that i do not know how `Stream.flat_map` has even any influence on the `Stream.filter` function.

Did i miss here how `Stream.flat_map` should work or shall file a bug request?

Paul

Ben Wilson

unread,
Sep 10, 2016, 9:13:09 AM9/10/16
to elixir-lang-talk
It would probably be best to post this on either the mailing list OR the forum, but not both. Many people use both and having the discussion happen in 2 places is not supre helpful.

José Valim

unread,
Sep 10, 2016, 10:43:02 AM9/10/16
to elixir-l...@googlegroups.com
Hi Paul,

You got the bug backwards. The issue is that flat_map continues even after take(1). Streams are by design meant to halt when items will no longer be consumed.

I will investigate it soon.

PS: I will send an email that retires this mailing list later today.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/ed614aa7-8c44-490d-90d4-7ff563dc9dc9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--


José Valim
Skype: jv.ptec
Founder and Director of R&D

José Valim

unread,
Sep 10, 2016, 6:25:50 PM9/10/16
to elixir-lang-talk, jose....@plataformatec.com.br
It has been fixed in master.
Reply all
Reply to author
Forward
0 new messages