Last active
December 1, 2015 17:29
-
-
Save drbrain/d5b8449e1c43e8eaefc4 to your computer and use it in GitHub Desktop.
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
input = 1.upto 10 | |
lazy = input.lazy.map { |v| | |
result = v * 2 | |
p times_2: result | |
result | |
}.each_slice(2).map { |v1, v2| | |
result = v1 + v2 | |
p add_pairs: result | |
result | |
} | |
p lazy.to_a |
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
input = 1.upto 10 | |
times_2 = Enumerator.new do |out| | |
loop do | |
v = input.next | |
result = v * 2 | |
p times_2: result | |
out << result | |
end | |
end | |
add_pairs = Enumerator.new do |out| | |
loop do | |
v1 = times_2.next | |
v2 = times_2.next | |
result = v1 + v2 | |
p add_pairs: result | |
out << result | |
end | |
end | |
p add_pairs.to_a |
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
{:times_2=>2} | |
{:times_2=>4} | |
{:add_pairs=>6} | |
{:times_2=>6} | |
{:times_2=>8} | |
{:add_pairs=>14} | |
{:times_2=>10} | |
{:times_2=>12} | |
{:add_pairs=>22} | |
{:times_2=>14} | |
{:times_2=>16} | |
{:add_pairs=>30} | |
{:times_2=>18} | |
{:times_2=>20} | |
{:add_pairs=>38} | |
[6, 14, 22, 30, 38] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment