Created
October 21, 2021 11:34
-
-
Save wowremywang/9d5786a1a33adee7aae1c39e7505d22e to your computer and use it in GitHub Desktop.
Avoid endless loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bad | |
range = 1..Float::INFINITY | |
p range.collect { |x| x * x }.first(10) | |
# Good | |
range = 1..Float::INFINITY | |
p range.lazy.collect { |x| x * x }.first(10) | |
Reference: http://patshaughnessy.net/2013/4/3/ruby-2-0-works-hard-so-you-can-be-lazy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment