lib/external/enumerable.rb in external-0.1.0 vs lib/external/enumerable.rb in external-0.3.0
- old
+ new
@@ -1,28 +1,65 @@
require 'enumerator'
module External
+
+ # An externalized implementation of Enumerable. External::Enumerable
+ # requires several methods with the following functionality:
+ #
+ # each:: iterates over items in self
+ # another:: provide a another instance of self
+ # to_a:: converts self to an Array
+ #
module Enumerable
- # def all? # :yield: obj
- # not_implemented
- # end
+ # Flag indicating whether to enumerate (ie collect,
+ # select, etc) into an array or into an instance
+ # of self. In most cases enumerating to an array
+ # performs better, but enumerating to another
+ # instance of self may be desired for especially
+ # large collections.
+ attr_accessor :enumerate_to_a
- # def any? # :yield: obj
- # not_implemented
- # end
+ def all? # :yield: obj
+ # WARN -- no tests for this in test_array
+ each do |obj|
+ return false unless yield(obj)
+ end
+ true
+ end
- # def collect # :yield: item
- # not_implemented
- # end
+ def any? # :yield: obj
+ # WARN -- no tests for this in test_array
+ each do |obj|
+ return true if yield(obj)
+ end
+ false
+ end
+ def collect # :yield: item
+ if block_given?
+ another = enumerate_to_a ? [] : self.another
+ each do |item|
+ another << yield(item)
+ end
+ another
+ else
+ # Not sure if Enumerator works right for large externals...
+ Object::Enumerable::Enumerator.new(self)
+ end
+ end
+
# def collect! # :yield: item
# not_implemented
# end
- # def detect(ifnone=nil) # :yield: obj
- # not_implemented
- # end
+ def detect(ifnone=nil) # :yield: obj
+ # WARN -- no tests for this in test_array
+ each do |obj|
+ return obj if yield(obj)
+ end
+ nil
+ end
# def each_cons(n) # :yield:
# not_implemented
# end
@@ -36,13 +73,13 @@
yield(item, i + offset)
end
end
end
- # def entries
- # to_a
- # end
+ def entries
+ to_a
+ end
# def enum_cons(n)
# not_implemented
# end
@@ -52,45 +89,53 @@
# def enum_with_index
# not_implemented
# end
- # def find(ifnone=nil, &block) # :yield: obj
- # detect(ifnone, &block)
- # end
+ def find(ifnone=nil, &block) # :yield: obj
+ # WARN -- no tests for this in test_array
+ detect(ifnone, &block)
+ end
- # def find_all # :yield: obj
- # not_implemented
- # end
+ def find_all # :yield: obj
+ another = enumerate_to_a ? [] : self.another
+ each do |item|
+ another << item if yield(item)
+ end
+ another
+ end
# def grep(pattern) # :yield: obj
# not_implemented
# end
- # def include?(obj)
- # not_implemented
- # end
+ def include?(obj)
+ each do |current|
+ return true if current == obj
+ end
+ false
+ end
# def inject(init) # :yield: memo, obj
# not_implemented
# end
- # def map(&block) # :yield: item
- # collect(&block)
- # end
+ def map(&block) # :yield: item
+ collect(&block)
+ end
# def map!(&block) # :yield: item
# collect!(&block)
# end
# def max # :yield: a,b
# not_implemented
# end
- # def member?(obj)
- # include?(obj)
- # end
+ def member?(obj)
+ include?(obj)
+ end
# def min # :yield: a,b
# not_implemented
# end
@@ -104,12 +149,12 @@
# def reject! # :yield: item
# not_implemented
# end
- # def select(&block) # :yield: obj
- # find_all(&block)
- # end
+ def select(&block) # :yield: obj
+ find_all(&block)
+ end
# def sort # :yield: a,b
# not_implemented
# end
\ No newline at end of file