Stream.new

68 views
Skip to first unread message

Robert Dober

unread,
Jul 23, 2024, 5:00:20 PM7/23/24
to elixir-lang-core
Hi there

for eternity (there was no life before Elixir, right?) I was creating streams from enumerables with

-  Stream.map(enum, &(&1))

trying to find something better I found, somethoing worse, I believe

- Stream.concat(enum, [])

and, please forgive me, I wont do it again...

- %Stream{enum: enum}


I would gladly provide a PR for implementing Stream.new/1 if so desired.

KR
Robert

José Valim

unread,
Jul 23, 2024, 5:17:34 PM7/23/24
to elixir-l...@googlegroups.com
Is there any benefit in calling Stream.new(enum)? All streams are enums and not all streams are Stream, so nowhere you should be checking for Stream in particular.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/7b9cab1c-37fe-4cce-90f7-cd81c3835b65n%40googlegroups.com.

Generic Jam

unread,
Jul 23, 2024, 5:45:49 PM7/23/24
to elixir-lang-core
I recently wrote an internal lib that for many functions it takes a list or a stream and returns a stream. It's the responsibility of the user to run the stream. For my use case I used a guard which only permitted a list or a struct (Stream) as I couldn't figure out a better system. For my tests I just used a list with Stream.take to create a stream.

On that note I'd like it if there was a Stream.t() akin to String.t() for type definitions.

José Valim

unread,
Jul 23, 2024, 6:17:22 PM7/23/24
to elixir-l...@googlegroups.com
While some functions in the Stream module may return the `Stream` struct,
you must never explicitly check for the `Stream` struct, as streams
may come in several shapes, such as `IO.Stream`, `File.Stream`, or
even `Range`s.

The functions in the Stream module only guarantee to return enumerables
and their implementation (structs, anonymous functions, etc) may
change at any time. For example, a function that returns an anonymous
function today may return a struct in future releases.

Instead of checking for a particular type, you must instead write
assertive code that assumes you have an enumerable, using the functions
in the `Enume` or `Stream` module accordingly.

Robert Dober

unread,
Jul 24, 2024, 3:22:45 AM7/24/24
to elixir-lang-core
 I am afraid I do not get the message José, did you mean that `Stream.map(enum, &(&1))` is an antipattern?
Well my use case is very simple, I have a list and I want to make it lazy, why should I not use an idiom like the above?
IIUC I should not use Stream at all for this, right? But is there a different way? There is no Enum.to_stream for example.

Hmm maybe Stream and List do not play in the same class as Stream does not implement Collectable either?

Can you help me out with this, please
Robert

Robert Dober

unread,
Jul 24, 2024, 3:38:28 AM7/24/24
to elixir-lang-core
Oh I think I get it
Why create just a wrapper around an enum with `Stream.map(...;&(&1))` when you can call all the transformations on the enum anyway.
In other words instead of `x|>Stream.new|>Stream.something|>...  I just get rid of the Stream.new

Correct?

Wojtek Mach

unread,
Jul 24, 2024, 3:43:21 AM7/24/24
to elixir-l...@googlegroups.com
Yeah the key part is not whether something is a _stream_ (functions in Stream module tend to return %Stream{} or fun/2) but whether something is an _enumerable_. There’s no benefit turning, say, list into a stream, list is already _enumerable_!

-- 
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Robert Dober

unread,
Jul 24, 2024, 6:37:35 AM7/24/24
to elixir-lang-core
Understood
sorry for the noise, but thanx to your replies I learnt something.
Reply all
Reply to author
Forward
0 new messages