deferred_enum 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.homepage = "https://github.com/take-five/deferred_enum"
11
11
  s.summary = %q{Introduces lazy computations to Enumerable}
12
12
  s.description = File.read(File.expand_path('../README.rdoc', __FILE__))
13
- s.date = Time.now.strftime('%Y-%m-%d')
13
+ s.date = "2011-12-15"
14
14
 
15
15
  s.rubyforge_project = "deferred_enum"
16
16
 
@@ -0,0 +1,12 @@
1
+ class DeferredEnumerator::Map < DeferredEnumerator #:nodoc:all
2
+ def initialize(obj, map = nil)
3
+ @_map = map
4
+ super(obj)
5
+ end
6
+
7
+ def each
8
+ return super unless block_given?
9
+
10
+ super { |e| yield(@_map ? @_map.call(e) : e) }
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class DeferredEnumerator::Select < DeferredEnumerator #:nodoc:all
2
+ def initialize(obj, filter = nil)
3
+ @_filter = filter
4
+ super(obj)
5
+ end
6
+
7
+ def each
8
+ return super unless block_given?
9
+
10
+ super { |e| yield(e) if !@_filter || @_filter.call(e) }
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module DeferredEnum
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -27,32 +27,26 @@ module DeferredEnumerable
27
27
  end
28
28
 
29
29
  # Returns a new enumerator with the results of running block once for every element in enum.
30
- def collect # :yields: obj
30
+ def collect(&block) # :yields: obj
31
31
  return self unless block_given?
32
32
 
33
- DeferredEnumerator.new do |yielder|
34
- each { |entry| yielder << yield(entry) }
35
- end
33
+ DeferredEnumerator::Map.new(self, block)
36
34
  end
37
35
  alias map collect
38
36
 
39
37
  # Returns a enumerator containing all elements of enum for which block is not false
40
- def select # :yields: obj
38
+ def select(&block) # :yields: obj
41
39
  return self unless block_given?
42
40
 
43
- DeferredEnumerator.new do |yielder|
44
- each { |entry| yielder << entry if yield(entry) }
45
- end
41
+ DeferredEnumerator::Select.new(self, block)
46
42
  end
47
43
  alias find_all select
48
44
 
49
45
  # Returns a enumerator containing all elements of enum for which block is false
50
- def reject # :yields: obj
46
+ def reject(&block) # :yields: obj
51
47
  return self unless block_given?
52
48
 
53
- DeferredEnumerator.new do |yielder|
54
- each { |entry| yielder << entry unless yield(entry) }
55
- end
49
+ select { |e| !block.call(e) }
56
50
  end
57
51
 
58
52
  # Returns a new enumerator that is a one-dimensional flattening of this enumerator (recursively).
@@ -125,7 +119,7 @@ module DeferredEnumerable
125
119
  raise TypeError, 'Integer (> 0) expected' unless n.is_a?(Fixnum) && n > 0
126
120
 
127
121
  DeferredEnumerator.new do |yielder|
128
- each { |entry| yielder << entry if (n -= 1) >= 0}
122
+ each { |entry| yielder << entry if (n -= 1) >= 0 }
129
123
  end
130
124
  end
131
125
 
@@ -3,4 +3,7 @@ require "deferred_enum/enumerable"
3
3
 
4
4
  class DeferredEnumerator < Enumerator
5
5
  include ::DeferredEnumerable
6
+
7
+ require "deferred_enum/map"
8
+ require "deferred_enum/select"
6
9
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alexey Mikhaylov
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-12-11 00:00:00 +06:00
17
+ date: 2011-12-15 00:00:00 +06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -82,6 +82,8 @@ files:
82
82
  - deferred_enum.gemspec
83
83
  - lib/deferred_enum.rb
84
84
  - lib/deferred_enum/enumerable.rb
85
+ - lib/deferred_enum/map.rb
86
+ - lib/deferred_enum/select.rb
85
87
  - lib/deferred_enum/version.rb
86
88
  - lib/deferred_enumerable.rb
87
89
  - lib/deferred_enumerator.rb