facets 2.0.0 → 2.0.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.
@@ -4,6 +4,7 @@
4
4
  Core:
5
5
  chdir: lib/core
6
6
  files:
7
+ ../../README
7
8
  facets/1stclassmethod.rb facets/array facets/binding facets/boolean.rb facets/class
8
9
  facets/comparable facets/continuation facets/conversion.rb facets/dir facets/enumerable
9
10
  facets/file facets/filetest facets/float facets/functor.rb facets/hash
data/task/test CHANGED
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ratch
2
2
 
3
+ # Run unit tests
4
+
3
5
  live = ARGV.delete('--live')
4
6
 
5
7
  unless live
@@ -11,10 +13,10 @@ end
11
13
  main :test do
12
14
  if find = argv[0]
13
15
  unless file?(find)
14
- find = File.join(find, '**', '*.rb')
16
+ find = File.join(find, '**', 'test_*.rb')
15
17
  end
16
18
  else
17
- find = 'test/**/*.rb'
19
+ find = 'test/**/test_*.rb'
18
20
  end
19
21
 
20
22
  Dir.glob(find).each do |file|
@@ -1,52 +1,175 @@
1
- # _____ _
2
- # |_ _|__ ___| |_
3
- # | |/ _ \/ __| __|
4
- # | | __/\__ \ |
5
- # |_|\___||___/\__|
6
- #
7
- # for lib/facets/enumerable/collect.rb
8
- #
9
- # Extracted Mon Sep 03 16:23:07 -0700 2007
10
- # w/ Test Extraction Ratchet
11
- #
1
+ # _____ _
2
+ # |_ _|__ ___| |_
3
+ # | |/ _ \/ __| __|
4
+ # | | __/\__ \ |
5
+ # |_|\___||___/\__|
6
+ #
7
+ # for lib/facets/enumerable/partition.rb
8
+ #
9
+ # Extracted Mon Sep 03 16:23:07 -0700 2007
10
+ # w/ Test Extraction Ratchet
11
+ #
12
12
 
13
- require 'facets/enumerable/collect.rb'
13
+ require 'facets/enumerable/collect.rb'
14
14
 
15
+ require 'test/unit'
16
+ require 'set'
15
17
 
18
+ class TestEnumerable < Test::Unit::TestCase
16
19
 
17
- require 'test/unit'
20
+ def test_filter_collect
21
+ e = [3,4]
22
+ a = [1,2,3,4].filter_collect { |n|
23
+ throw(:skip) if n < 3
24
+ n
25
+ }
26
+ assert_equal( e, a )
27
+ end
28
+
29
+ def test_compact_collect
30
+ a = [1,2,nil,4].compact_collect { |e| e }
31
+ assert_equal( [1,2,4], a )
32
+ end
33
+
34
+ def test_filter_collect
35
+ e = [3,4]
36
+ a = [1,2,3,4].filter_collect { |n|
37
+ throw(:skip) if n < 3
38
+ n
39
+ }
40
+ assert_equal( e, a )
41
+ end
42
+
43
+ def test_compact_collect
44
+ a = [1,2,nil,4].compact_collect { |e| e }
45
+ assert_equal( [1,2,4], a )
46
+ end
47
+
48
+ # def test_op_mod
49
+ # a = [:A,:B,:C]
50
+ # assert_equal( a[1], a/1 )
51
+ # assert_equal( :B, a/1 )
52
+ # end
53
+ #
54
+ # def test_op_div
55
+ # a = [:A,:B,:C]
56
+ # assert_equal( a[1], a/1 )
57
+ # assert_equal( :B, a/1 )
58
+ # end
59
+
60
+ def test_group_by_for_array
61
+ a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
62
+ r = {0=>[0, 2, 4, 6, 8], 1=>[1, 3, 5, 7, 9]}
63
+ assert_equal(r, a.group_by{|e| e%2}.each{|k, v| v.sort!})
64
+
65
+ h = {0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, 9=>9}
66
+ r = {0=>[[0, 0], [2, 2], [4, 4], [6, 6], [8, 8]], 1=>[[1, 1], [3, 3], [5, 5], [7, 7], [9, 9]]}
67
+ assert_equal(r, h.group_by{|k, v| v%2}.each{|k, v| v.sort!})
68
+
69
+ x = (1..5).group_by{ |n| n % 3 }
70
+ o = { 0 => [3], 1 => [1, 4], 2 => [2,5] }
71
+ assert_equal( o, x )
72
+
73
+ x = ["I had", 1, "dollar and", 50, "cents"].group_by{ |e| e.class }
74
+ o = { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
75
+ assert_equal( o, x )
76
+ end
77
+
78
+ def test_cluster_by
79
+ a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
80
+ r = [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]
81
+ assert_equal(r, a.cluster_by{|e| e%2}.each{|a| a.sort!})
82
+ h = {0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, 9=>9}
83
+ r = [[[0, 0], [2, 2], [4, 4], [6, 6], [8, 8]], [[1, 1], [3, 3], [5, 5], [7, 7], [9, 9]]]
84
+ assert_equal(r, h.cluster_by{|k, v| v%2}.each{|a| a.sort!})
85
+ end
86
+
87
+ def test_each_by_01
88
+ x = []
89
+ [1,2,3,4].each_by{ |a,b| x << [a,b] }
90
+ o = [[1,2],[3,4]]
91
+ assert_equal( o, x )
92
+ end
93
+
94
+ def test_each_by_02
95
+ x = []
96
+ [1,2,3,4,5].each_by{ |a,b,c| x << [a,b,c] }
97
+ o = [[1,2,3],[4,5,nil]]
98
+ assert_equal( o, x )
99
+ end
100
+
101
+ def test_each_by_03
102
+ x = []
103
+ [1,2,3,4].each_by(2){ |a,b| x << [a,b] }
104
+ o = [[1,2],[3,4]]
105
+ assert_equal( o, x )
106
+ end
107
+
108
+ def test_each_by_04
109
+ x = []
110
+ [1,2,3,4,5,6,7,8].each_by(4){ |*a| x << a }
111
+ o = [ [[1,2,3,4]],[[5,6,7,8]] ]
112
+ assert_equal( o, x )
113
+ end
114
+
115
+ def test_each_by_05
116
+ x = []
117
+ [1,2,3,4,5,6].each_by(3){ |*a| x << a }
118
+ o = [ [[1,2,3]],[[4,5,6]] ]
119
+ assert_equal( o, x )
120
+ end
121
+
122
+ def test_each_by_06
123
+ a = [1,2,3,4,5,6]
124
+ r = []
125
+ a.each_by(2){ |x,y| r << [x,y] }
126
+ assert_equal( [[1,2],[3,4],[5,6]], r )
127
+ end
18
128
 
19
- class TestEnumerableCollect < Test::Unit::TestCase
129
+ def test_each_by_07
130
+ a = [1,2,3,4,5,6]
131
+ r = []
132
+ a.each_by(3){ |*e| r << e }
133
+ assert_equal( [ [[1,2,3]], [[4,5,6]] ], r )
134
+ end
20
135
 
21
- def test_filter_collect
22
- e = [3,4]
23
- a = [1,2,3,4].filter_collect { |n|
24
- throw(:skip) if n < 3
25
- n
26
- }
27
- assert_equal( e, a )
28
- end
136
+ def test_each_pair
137
+ r = []
138
+ a = [1,2,3,4]
139
+ a.each_pair{ |a,b| r << [a,b] }
140
+ assert_equal( [[1,2],[3,4]], r )
141
+ end
29
142
 
30
- def test_compact_collect
31
- a = [1,2,nil,4].compact_collect { |e| e }
32
- assert_equal( [1,2,4], a )
33
- end
143
+ def test_each_unique_pair
144
+ r = []
145
+ a = [1,2,3,4]
146
+ a.each_unique_pair{ |a,b| r << [a,b] }
147
+ assert_equal( [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], r )
148
+ end
34
149
 
35
- def test_filter_collect
36
- e = [3,4]
37
- a = [1,2,3,4].filter_collect { |n|
38
- throw(:skip) if n < 3
39
- n
40
- }
41
- assert_equal( e, a )
42
- end
150
+ def test_eachn
151
+ x = []
152
+ [1,2,3,4].eachn{ |a,b| x << [a,b] }
153
+ o = [[1,2],[3,4]]
154
+ assert_equal( o, x )
43
155
 
44
- def test_compact_collect
45
- a = [1,2,nil,4].compact_collect { |e| e }
46
- assert_equal( [1,2,4], a )
47
- end
156
+ x = []
157
+ [1,2,3,4,5].eachn{ |a,b,c| x << [a,b,c] }
158
+ o = [[1,2,3],[4,5,nil]]
159
+ assert_equal( o, x )
160
+ end
48
161
 
162
+ def test_each_combination
163
+ r = []
164
+ a = [1,2,3,4]
165
+ a.each_combination(2){ |a,b| r << [a,b] }
166
+ assert_equal( [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], r )
49
167
  end
50
168
 
51
169
 
170
+ def test_collect_with_index
171
+ a = [1,2,3].collect_with_index{ |e,i| e*i }
172
+ assert_equal( [0,2,6], a )
173
+ end
52
174
 
175
+ end
metadata CHANGED
@@ -3,13 +3,13 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: facets
5
5
  version: !ruby/object:Gem::Version
6
- version: 2.0.0
7
- date: 2007-10-03 00:00:00 -07:00
6
+ version: 2.0.1
7
+ date: 2007-10-07 00:00:00 -07:00
8
8
  summary: Premium Core Extensions and Standard Additions
9
9
  require_paths:
10
- - lib/core
11
- - lib/more
12
10
  - lib/methods
11
+ - lib/more
12
+ - lib/core
13
13
  email: facets-universal@rubyforge.org
14
14
  homepage: http://facets.rubyforge.org
15
15
  rubyforge_project:
@@ -36,7 +36,11 @@ files:
36
36
  - README
37
37
  - demo
38
38
  - demo/bench
39
- - demo/bench/bench_factorial.rb
39
+ - demo/bench/enumerable
40
+ - demo/bench/enumerable/bench_collect.rb
41
+ - demo/bench/enumerable/bench_count.rb
42
+ - demo/bench/integer
43
+ - demo/bench/integer/bench_factorial.rb
40
44
  - demo/sample
41
45
  - demo/sample/annotations
42
46
  - demo/sample/annotations/annotations1.rb
@@ -90,7 +94,6 @@ files:
90
94
  - lib/core/facets/enumerable/combination.rb
91
95
  - lib/core/facets/enumerable/count.rb
92
96
  - lib/core/facets/enumerable/instance_map.rb
93
- - lib/core/facets/enumerable/partition.rb
94
97
  - lib/core/facets/enumerable/permutation.rb
95
98
  - lib/core/facets/enumerable/probability.rb
96
99
  - lib/core/facets/file
@@ -319,6 +322,7 @@ files:
319
322
  - lib/methods/facets/enumerable/filter_map.rb
320
323
  - lib/methods/facets/enumerable/frequency.rb
321
324
  - lib/methods/facets/enumerable/graph.rb
325
+ - lib/methods/facets/enumerable/group_by.rb
322
326
  - lib/methods/facets/enumerable/ideal_entropy.rb
323
327
  - lib/methods/facets/enumerable/injecting.rb
324
328
  - lib/methods/facets/enumerable/map_if.rb
@@ -847,7 +851,6 @@ files:
847
851
  - test/unit/enumerable/test_collect.rb
848
852
  - test/unit/enumerable/test_combination.rb
849
853
  - test/unit/enumerable/test_count.rb
850
- - test/unit/enumerable/test_partition.rb
851
854
  - test/unit/enumerable/test_permutation.rb
852
855
  - test/unit/enumerable/test_probability.rb
853
856
  - test/unit/file
@@ -1027,7 +1030,6 @@ test_files:
1027
1030
  - test/unit/enumerable/test_collect.rb
1028
1031
  - test/unit/enumerable/test_combination.rb
1029
1032
  - test/unit/enumerable/test_count.rb
1030
- - test/unit/enumerable/test_partition.rb
1031
1033
  - test/unit/enumerable/test_permutation.rb
1032
1034
  - test/unit/enumerable/test_probability.rb
1033
1035
  - test/unit/file
@@ -1,285 +0,0 @@
1
- # TITLE:
2
- # Partition
3
- #
4
- # DESCRIPTION:
5
- # Enumerble partitioning extensions.
6
- #
7
- # AUTHORS:
8
- # - Thibaut Barrère
9
- # - WhyTheLuckyStiff
10
- # - Daniel Sheppard
11
- # - Paul Battley
12
- # - Paul Betts
13
- # - Tom Moertel
14
- #
15
- # NOTES:
16
- # - TODO Change name and/or move #partition_by?
17
- # - TODO Suggest Enumerator's #each_slice use block arity
18
- # if no parameter is given.
19
-
20
- require 'enumerator' # for each_slice
21
-
22
- #
23
- module Enumerable
24
-
25
- # Partition an array into parts of given length.
26
- #
27
- # CREDIT WhyTheLuckyStiff
28
-
29
- # def / len
30
- # inject([]) do |ary, x|
31
- # ary << [] if [*ary.last].nitems % len == 0
32
- # ary.last << x
33
- # ary
34
- # end
35
- # end
36
-
37
- # See Enumerable#partition for the background. #partition_by is best
38
- # explained by example.
39
- #
40
- # (1..5).partition_by { |n| n % 3 }
41
- # #=> { 0 => [3], 1 => [1, 4], 2 => [2,5] }
42
- #
43
- # ["I had", 1, "dollar and", 50, "cents"].partition_by { |e| e.class }
44
- # #=> { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
45
- #
46
- # #partition_by is used to group items in a collection by something they
47
- # have in common. The common factor is the key in the resulting hash, the
48
- # array of like elements is the value.
49
- #
50
- # CREDIT Gavin Sinclair
51
- # CREDIT Gregory of Laurel, Maryland
52
-
53
- def partition_by #:yield:
54
- r = Hash.new{ |h,k| h[k]=[] }
55
- each do |e|
56
- r[ yield(e) ] << e
57
- end
58
- return r
59
- end
60
-
61
- # Similar to oartition_by but returns an array of arrays.
62
- #
63
- # %w{this is a test}.cluster_by {|x| x[0]}
64
- #
65
- # _produces_
66
- #
67
- # [ ['a'], ['is'], ['this', 'test'] ]
68
- #
69
- # CREDIT Paul Betts
70
- # CREDIT Tom Moertel
71
-
72
- def cluster_by(&b)
73
- h = {}; self.each { |x| (h[sig_fn[x]] ||= []) << x }
74
- h.keys.sort!.map { |k| h[k] }
75
- end
76
-
77
- # Split on matching pattern.
78
- #
79
- # ['a1','b1','a2','b2'].divide(/^a/)
80
- # => [['a1,'b1'],['a2','b2']]
81
-
82
- def divide(pattern)
83
- inject([]) do |memo,obj|
84
- memo.push [] if pattern === obj
85
- memo.last << obj
86
- memo
87
- end
88
- end
89
-
90
- # Iterate through slices. If slicing +step+ is not
91
- # given, the the arity if the block is used.
92
- #
93
- # x = []
94
- # [1,2,3,4].each_by{ |a,b| x << [a,b] }
95
- # x #=> [ [1,2], [3,4] ]
96
- #
97
- # x = []
98
- # [1,2,3,4,5,6].each_by(3){ |a| x << a }
99
- # x #=> [ [1,2,3], [4,5,6] ]
100
-
101
- def each_by(step=nil, &yld)
102
- if step
103
- each_slice(step,&yld)
104
- else
105
- step = yld.arity.abs
106
- each_slice(step,&yld)
107
- end
108
- end
109
-
110
- # Iterators over each element pairing.
111
- #
112
- # [:a,:b,:c,:d].each_pair { |a,b| puts "#{a} -> #{b}" }
113
- #
114
- # _produces_
115
- #
116
- # a -> b
117
- # c -> d
118
-
119
- def each_pair #:yield:
120
- e1 = nil
121
- each_with_index do |e,i|
122
- if i % 2 == 0
123
- e1 = e
124
- next
125
- else
126
- yield(e1,e)
127
- end
128
- end
129
- end
130
-
131
- #
132
- #
133
- # CREDIT Martin DeMello
134
-
135
- def eachn(&block)
136
- n = block.arity.abs
137
- each_slice(n) {|i| block.call(*i)}
138
- end
139
-
140
- #
141
-
142
- def collect_if(&b)
143
- a = map(&b)
144
- # to get the same semantics as select{|e| e}
145
- a.delete(false)
146
- a.compact!
147
- a
148
- end
149
-
150
- alias_method :map_if, :collect_if
151
- end
152
-
153
-
154
-
155
-
156
- # _____ _
157
- # |_ _|__ ___| |_
158
- # | |/ _ \/ __| __|
159
- # | | __/\__ \ |_
160
- # |_|\___||___/\__|
161
- #
162
- =begin test
163
-
164
- require 'test/unit'
165
- require 'set'
166
-
167
- class TestEnumerable < Test::Unit::TestCase
168
-
169
- # def test_op_mod
170
- # a = [:A,:B,:C]
171
- # assert_equal( a[1], a/1 )
172
- # assert_equal( :B, a/1 )
173
- # end
174
- #
175
- # def test_op_div
176
- # a = [:A,:B,:C]
177
- # assert_equal( a[1], a/1 )
178
- # assert_equal( :B, a/1 )
179
- # end
180
-
181
- def test_partition_by
182
- x = (1..5).partition_by{ |n| n % 3 }
183
- o = { 0 => [3], 1 => [1, 4], 2 => [2,5] }
184
- assert_equal( o, x )
185
- x = ["I had", 1, "dollar and", 50, "cents"].partition_by { |e| e.class }
186
- o = { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
187
- assert_equal( o, x )
188
- end
189
-
190
- def test_cluster_by
191
- assert_equal( [ ['a'], ['is'], ['this', 'test'] ], %w{this is a test}.cluster_by {|x| x[0]} )
192
- assert_equal( [ ['an', 'is'], ['not'], ['this'], ['untest'] ], %w{this is not an untest}.cluster_by {|x| x.length} )
193
- end
194
-
195
- def test_each_by_01
196
- x = []
197
- [1,2,3,4].each_by{ |a,b| x << [a,b] }
198
- o = [[1,2],[3,4]]
199
- assert_equal( o, x )
200
- end
201
-
202
- def test_each_by_02
203
- x = []
204
- [1,2,3,4,5].each_by{ |a,b,c| x << [a,b,c] }
205
- o = [[1,2,3],[4,5,nil]]
206
- assert_equal( o, x )
207
- end
208
-
209
- def test_each_by_03
210
- x = []
211
- [1,2,3,4].each_by(2){ |a,b| x << [a,b] }
212
- o = [[1,2],[3,4]]
213
- assert_equal( o, x )
214
- end
215
-
216
- def test_each_by_04
217
- x = []
218
- [1,2,3,4,5,6,7,8].each_by(4){ |*a| x << a }
219
- o = [ [[1,2,3,4]],[[5,6,7,8]] ]
220
- assert_equal( o, x )
221
- end
222
-
223
- def test_each_by_05
224
- x = []
225
- [1,2,3,4,5,6].each_by(3){ |*a| x << a }
226
- o = [ [[1,2,3]],[[4,5,6]] ]
227
- assert_equal( o, x )
228
- end
229
-
230
- def test_each_by_06
231
- a = [1,2,3,4,5,6]
232
- r = []
233
- a.each_by(2){ |x,y| r << [x,y] }
234
- assert_equal( [[1,2],[3,4],[5,6]], r )
235
- end
236
-
237
- def test_each_by_07
238
- a = [1,2,3,4,5,6]
239
- r = []
240
- a.each_by(3){ |*e| r << e }
241
- assert_equal( [ [[1,2,3]], [[4,5,6]] ], r )
242
- end
243
-
244
- def test_each_pair
245
- r = []
246
- a = [1,2,3,4]
247
- a.each_pair{ |a,b| r << [a,b] }
248
- assert_equal( [[1,2],[3,4]], r )
249
- end
250
-
251
- def test_each_unique_pair
252
- r = []
253
- a = [1,2,3,4]
254
- a.each_unique_pair{ |a,b| r << [a,b] }
255
- assert_equal( [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], r )
256
- end
257
-
258
- def test_eachn
259
- x = []
260
- [1,2,3,4].eachn{ |a,b| x << [a,b] }
261
- o = [[1,2],[3,4]]
262
- assert_equal( o, x )
263
-
264
- x = []
265
- [1,2,3,4,5].eachn{ |a,b,c| x << [a,b,c] }
266
- o = [[1,2,3],[4,5,nil]]
267
- assert_equal( o, x )
268
- end
269
-
270
- def test_each_combination
271
- r = []
272
- a = [1,2,3,4]
273
- a.each_combination(2){ |a,b| r << [a,b] }
274
- assert_equal( [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], r )
275
- end
276
-
277
-
278
- def test_collect_with_index
279
- a = [1,2,3].collect_with_index{ |e,i| e*i }
280
- assert_equal( [0,2,6], a )
281
- end
282
-
283
- end
284
-
285
- =end