Skip to content

Commit

Permalink
Add section about Enumerable#lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
ydakuka committed Dec 15, 2023
1 parent 5dcdd37 commit cd1789d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4950,6 +4950,28 @@ LOREM
"when an unknown printer took a galley of type and scrambled it to make a type specimen book."
----

== Enumerable

=== lazy [[lazy]]

Use `Enumerable#lazy` in combination with methods like `.first` or `.take`, especially for operations on large collections or datasets, because it avoids the need to process the entire collection.

[source,ruby]
----
SIZE = 10
MAX = 10_000_000
# bad
(0..MAX).uniq { |x| x + 2 }.first(SIZE)
(0..MAX).map { |x| x + 2 }.first(SIZE)
(0..MAX).select { |x| x + 2 }.take(SIZE)
# good
(0..MAX).lazy.uniq { |x| x + 2 }.first(SIZE)
(0..MAX).lazy.map { |x| x + 2 }.first(SIZE)
(0..MAX).lazy.select { |x| x + 2 }.take(SIZE)
----

== Heredocs

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

0 comments on commit cd1789d

Please sign in to comment.