Skip to content

Commit cd1789d

Browse files
committed
Add section about Enumerable#lazy
1 parent 5dcdd37 commit cd1789d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,6 +4950,28 @@ LOREM
49504950
"when an unknown printer took a galley of type and scrambled it to make a type specimen book."
49514951
----
49524952

4953+
== Enumerable
4954+
4955+
=== lazy [[lazy]]
4956+
4957+
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.
4958+
4959+
[source,ruby]
4960+
----
4961+
SIZE = 10
4962+
MAX = 10_000_000
4963+
4964+
# bad
4965+
(0..MAX).uniq { |x| x + 2 }.first(SIZE)
4966+
(0..MAX).map { |x| x + 2 }.first(SIZE)
4967+
(0..MAX).select { |x| x + 2 }.take(SIZE)
4968+
4969+
# good
4970+
(0..MAX).lazy.uniq { |x| x + 2 }.first(SIZE)
4971+
(0..MAX).lazy.map { |x| x + 2 }.first(SIZE)
4972+
(0..MAX).lazy.select { |x| x + 2 }.take(SIZE)
4973+
----
4974+
49534975
== Heredocs
49544976

49554977
=== Squiggly Heredocs [[squiggly-heredocs]]

0 commit comments

Comments
 (0)