-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Improve Stream.interval/1 documentation #5480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Stream.interval/1 documentation #5480
Conversation
lib/elixir/lib/stream.ex
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is actually much more cheaper to use Stream.interval(0) than Stream.iterate(0, & &1 + 1):
iex> interval = Stream.interval(0)
iex> :timer.tc(Enum, :take, [interval, 100])
{36, ...}
iex> iterate = Stream.iterate(0, &(&1 + 1))
iex> :timer.tc(Enum, :take, [iterate, 100])
{184, ...}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because you are using IEx so the anonymous function is being evaluated. Never benchmark in the shell. Doing the one below in a file gives me a slightly faster iterate:
interval = Stream.interval(0)
IO.inspect :timer.tc(Enum, :take, [interval, 1000])
iterate = Stream.iterate(0, &(&1 + 1))
IO.inspect :timer.tc(Enum, :take, [interval, 1000])There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Riiight, though with compiled code there is no clear winner:
{103, ...}
{106, ...}
======
{103, ...}
{98, ...}
======
{103, ...}
{124, ...}
======
{103, ...}
{98, ...}
I believe there is no meaningful difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. So let's go with the one that is better semantically. :)
lib/elixir/lib/stream.ex
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please remove the bold and say "instead use ..." or "use ... instead". I believe the "use instead" is not idiomatic. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use use X instead. in every other place.
|
I am 👍 because it gives a nudge in the proper direction. I actually have no use cases for Stream.interval and Stream.timer besides possibly testing. They were added in a time we thought Stream would have async functionality but it never became true. It is in my plans to evaluate deprecating them so pushing users away from it is a good idea. |
f5ac129 to
480179f
Compare
|
@britto @josevalim Mind there's a |
|
With this addition we have conflicting usage example: we generate sequence of numbers. |
|
@gusaiani thanks! fixed. |
480179f to
69267c7
Compare
lib/elixir/lib/stream.ex
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we should have parens here, I think better to be closer to what Macro.to_string/1 produce.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it much more readable without the parenthesis. Maybe we should make Macro.to_string/1 smart enough to handle those cases. It has a tendency to add too many parens. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Just applied your suggestions 👍.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh noes. "parens" is the answer to life, the universe, and everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amount of people who confused by dangling & requires parens. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had this discussion in the past, so no need to revisit it here. :) It is part of the language, it is here to stay, folks may as well get acquainted with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I am not folks! I don't want to get acquainted! 😄
|
I aggree that the current documentation is pretty self-explanatory. My point here is to warn beginners who might overlook the |
69267c7 to
c54048f
Compare
By warning about its blocking behavior and suggesting the use of `Stream.iterate/2` for generating number sequences.
c54048f to
b00f06b
Compare
|
❤️ 💚 💙 💛 💜 |
By warning about its blocking behavior and suggesting the use of
Stream.iterate/2for generating number sequences.