Last active
July 23, 2017 04:34
-
-
Save gam0022/da20cbd5af5dcf1c7e22 to your computer and use it in GitHub Desktop.
EnumeratorとEnumerator::Lazyの違い ref: http://qiita.com/gam0022/items/8acfc0c674b96060c03f
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
でも無限リストを処理できる | |
Enumerator.new{|y| | |
(1..Float::INFINITY).each{|n| | |
y << n*2 | |
} | |
}.first(5) # => [2, 4, 6, 8, 10] |
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
[2] pry(main)> (1..Float::INFINITY).lazy.map{|n| n*2}.first(5) | |
# => [2, 4, 6, 8, 10] |
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
Enumerator.new{|y| | |
(1..Float::INFINITY).each{|n| | |
y << n*2 | |
} | |
}.first(5) # => [2, 4, 6, 8, 10] |
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
File.open("log.txt") do |f| | |
puts f.each_line.first(10) | |
end |
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
[1] pry(main)> (1..Float::INFINITY).map{|n| n*2}.first(5) | |
# => (実行が終わらない...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment