Skip to content

Instantly share code, notes, and snippets.

@achempion
Created January 27, 2015 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achempion/498921382bc90cd3ba7e to your computer and use it in GitHub Desktop.
Save achempion/498921382bc90cd3ba7e to your computer and use it in GitHub Desktop.
module Enumerable
class Lazy < Enumerator
def initialize(obj)
super() do |yielder|
obj.each do |val|
if block_given?
yield(yielder, val)
else
yielder << val
end
end
end
end
def map
Lazy.new(self) do |yielder, val|
yielder << yield(val)
end
end
end
end
a = Enumerable::Lazy.new([1,2,3])
a = a.map { |x| x * 10 }.map { |x| x - 1 }
puts a.next #=> 9
puts a.next #=> 19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment