Skip to content
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

Add section about Enumerable#lazy #938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4954,6 +4954,27 @@ LOREM
"when an unknown printer took a galley of type and scrambled it to make a type specimen book."
----

== Enumerable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to check if something else can't be moved under this section as well, as it's kind of weird to have a section with a single entry in it.


=== Leverage `#lazy` to Optimize Memory Usage [[leverage-lazy-to-optimize-memory-usage]]

Using `Enumerable#lazy` in combination with methods like `Enumerable#first` or `Enumerable#take` can be very efficient, especially for operations on large collections, datasets and infinite sequences seamlessly.

Enumerator::Lazy can be constructed from any Enumerable with the `Enumerable#lazy` method without evaluating them immediately, and evaluating values on as-needed basis.

The real enumeration is performed when any non-redefined Enumerable method is called.

[source,ruby]
----
# bad
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might also want to mention that this will instantiate a huge collection first, so it's clearer what the problem and value proposition is.

(0..).uniq { |x| x + 2 }.first(10)
(0..10000000).select { |x| x % 1000 }.first(5)

# good
(0..).lazy.uniq { |x| x + 2 }.first(10)
(0..10000000).lazy.select { |x| x % 1000 }.first(5)
----

== Heredocs

=== Squiggly Heredocs [[squiggly-heredocs]]
Expand Down