Skip to content
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
19 changes: 19 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4915,6 +4915,25 @@ Predicate methods without arguments can't replace `count` expressions when colle
----
--

=== 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 - instantiates the entire collection into memory first
(0..).uniq { |x| x + 2 }.first(10)
(0..10000000).select { |x| x % 1000 }.first(5)

# good - evaluates elements on an as-needed basis
(0..).lazy.uniq { |x| x + 2 }.first(10)
(0..10000000).lazy.select { |x| x % 1000 }.first(5)
----

== Numbers

=== Underscores in Numerics [[underscores-in-numerics]]
Expand Down