Skip to content

@BFalkner /lazy.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A sample partial implementation of a lazy enumerable. See http://gist.github.com/441028 for example usage.
module Lazy
module LazyEnumerable; end
class Enumerator
include LazyEnumerable
def initialize(enumerable=nil, &block)
if enumerable
@enumeration_block = proc {|send| enumerable.each(&send) }
else
@enumeration_block = block
end
end
def each(&block)
@enumeration_block.call(block)
end
end
module LazyEnumerable
def to_a
arr = []
self.each {|element| arr << element }
arr
end
def map(&block)
Enumerator.new do |send|
self.each do |element|
send.call block.call(element)
end
end
end
alias_method :collect, :map
def find_all(&block)
Enumerator.new do |send|
self.each do |element|
send.call(element) if block.call(element)
end
end
end
alias_method :select, :find_all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.