Skip to content

Instantly share code, notes, and snippets.

@gam0022
Last active July 23, 2017 04:34
Show Gist options
  • Save gam0022/da20cbd5af5dcf1c7e22 to your computer and use it in GitHub Desktop.
Save gam0022/da20cbd5af5dcf1c7e22 to your computer and use it in GitHub Desktop.
EnumeratorとEnumerator::Lazyの違い ref: http://qiita.com/gam0022/items/8acfc0c674b96060c03f
でも無限リストを処理できる
Enumerator.new{|y|
(1..Float::INFINITY).each{|n|
y << n*2
}
}.first(5) # => [2, 4, 6, 8, 10]
[2] pry(main)> (1..Float::INFINITY).lazy.map{|n| n*2}.first(5)
# => [2, 4, 6, 8, 10]
Enumerator.new{|y|
(1..Float::INFINITY).each{|n|
y << n*2
}
}.first(5) # => [2, 4, 6, 8, 10]
File.open("log.txt") do |f|
puts f.each_line.first(10)
end
[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