enumerable_lz 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +4 -0
- data/README.md +0 -4
- data/lib/enumerable_lz/filter.rb +18 -17
- data/lib/enumerable_lz/filter_18.rb +27 -18
- data/lib/enumerable_lz.rb +1 -1
- metadata +3 -3
data/ChangeLog
CHANGED
data/README.md
CHANGED
data/lib/enumerable_lz/filter.rb
CHANGED
@@ -16,11 +16,16 @@ module Enumerable
|
|
16
16
|
@init_block = init_block unless init_block.nil?
|
17
17
|
super() do |y|
|
18
18
|
@init_block.call unless @init_block.nil?
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
the_enum = (@filter||[]).inject(@org_enum) do |r,f|
|
20
|
+
Enumerator.new do |iy|
|
21
|
+
r.each do |el|
|
22
|
+
iy.yield el if f.call(el)
|
23
|
+
end
|
22
24
|
end
|
23
25
|
end
|
26
|
+
catch :do_break do
|
27
|
+
the_enum.each {|el| y.yield el}
|
28
|
+
end
|
24
29
|
end
|
25
30
|
filter! the_filter if the_filter
|
26
31
|
end
|
@@ -28,9 +33,9 @@ module Enumerable
|
|
28
33
|
def filter! pattern=nil, &block
|
29
34
|
@filter||=[]
|
30
35
|
if pattern.is_a? Array
|
31
|
-
@filter
|
36
|
+
pattern.each{|el| @filter << conv_proc(el)}
|
32
37
|
else
|
33
|
-
@filter<<(pattern || block)
|
38
|
+
@filter << conv_proc(pattern || block)
|
34
39
|
end
|
35
40
|
self
|
36
41
|
end
|
@@ -49,18 +54,14 @@ module Enumerable
|
|
49
54
|
end
|
50
55
|
|
51
56
|
private
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
filter[*args]
|
61
|
-
else
|
62
|
-
filter===obj
|
63
|
-
end
|
57
|
+
def conv_proc pattern
|
58
|
+
case pattern
|
59
|
+
when nil
|
60
|
+
Proc.new{true}
|
61
|
+
when Proc
|
62
|
+
pattern
|
63
|
+
else
|
64
|
+
pattern.respond_to?(:to_proc) ? pattern.to_proc : Proc.new{|el|pattern===el}
|
64
65
|
end
|
65
66
|
end
|
66
67
|
end
|
@@ -9,7 +9,6 @@ module Enumerable
|
|
9
9
|
end
|
10
10
|
|
11
11
|
class Filter < Enumerator
|
12
|
-
require 'generator'
|
13
12
|
def initialize obj, *args
|
14
13
|
the_filter = args.shift
|
15
14
|
init_block, the_filter = [the_filter, args.shift] unless args.empty?
|
@@ -20,11 +19,12 @@ module Enumerable
|
|
20
19
|
|
21
20
|
def each &block
|
22
21
|
return self unless block_given?
|
22
|
+
@init_block.call unless @init_block.nil?
|
23
|
+
the_enum = (@filter||[]).inject(@org_enum) do |r,f|
|
24
|
+
CompliedFilter.new r, f
|
25
|
+
end
|
23
26
|
catch :do_break do
|
24
|
-
|
25
|
-
@org_enum.each do |*el|
|
26
|
-
block.call(*el) if matches_filter? *el
|
27
|
-
end
|
27
|
+
the_enum.each{|el| block.call(el)}
|
28
28
|
end
|
29
29
|
self
|
30
30
|
end
|
@@ -32,9 +32,9 @@ module Enumerable
|
|
32
32
|
def filter! pattern=nil, &block
|
33
33
|
@filter||=[]
|
34
34
|
if pattern.is_a? Array
|
35
|
-
@filter
|
35
|
+
pattern.each{|el| @filter << conv_proc(el)}
|
36
36
|
else
|
37
|
-
@filter<<(pattern || block)
|
37
|
+
@filter << conv_proc(pattern || block)
|
38
38
|
end
|
39
39
|
self
|
40
40
|
end
|
@@ -53,17 +53,26 @@ module Enumerable
|
|
53
53
|
end
|
54
54
|
|
55
55
|
private
|
56
|
-
def
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
56
|
+
def conv_proc pattern
|
57
|
+
case pattern
|
58
|
+
when nil
|
59
|
+
Proc.new{true}
|
60
|
+
when Proc
|
61
|
+
pattern
|
62
|
+
else
|
63
|
+
pattern.respond_to?(:to_proc) ? pattern.to_proc : Proc.new{|el|pattern===el}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class CompliedFilter
|
68
|
+
def initialize org_enum, filter
|
69
|
+
@org_enum = org_enum
|
70
|
+
@filter = filter
|
71
|
+
end
|
72
|
+
|
73
|
+
def each &block
|
74
|
+
@org_enum.each do |el|
|
75
|
+
block.call(el) if @filter.call(el)
|
67
76
|
end
|
68
77
|
end
|
69
78
|
end
|
data/lib/enumerable_lz.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- antimon2
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-02-23 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|