Created
March 16, 2017 08:10
-
-
Save YanhaoYang/85f4fc8048bd0e190a757dfc725dbb47 to your computer and use it in GitHub Desktop.
lazy ruby calls each block sequentially
This file contains hidden or 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
irb(main):001:0> 1.upto(Float::INFINITY).lazy. | |
irb(main):002:0* map { |x| puts "1 #{x}"; x * x }. | |
irb(main):003:0* map { |x| puts "2 #{x}"; x + 1 }. | |
irb(main):004:0* take(10).to_a | |
1 1 | |
2 1 | |
1 2 | |
2 4 | |
1 3 | |
2 9 | |
1 4 | |
2 16 | |
1 5 | |
2 25 | |
1 6 | |
2 36 | |
1 7 | |
2 49 | |
1 8 | |
2 64 | |
1 9 | |
2 81 | |
1 10 | |
2 100 | |
=> [2, 5, 10, 17, 26, 37, 50, 65, 82, 101] | |
irb(main):005:0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment