hamster 0.1.21 → 0.1.22
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/History.rdoc +26 -0
- data/lib/hamster/hash.rb +0 -1
- data/lib/hamster/list.rb +37 -6
- data/lib/hamster/set.rb +34 -20
- data/lib/hamster/stack.rb +9 -0
- data/lib/hamster/trie.rb +1 -0
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/each_spec.rb +2 -2
- data/spec/hamster/list/chunk_spec.rb +38 -0
- data/spec/hamster/list/{sort_by_spec.rb → count_spec.rb} +13 -18
- data/spec/hamster/list/find_spec.rb +1 -1
- data/spec/hamster/list/include_spec.rb +1 -1
- data/spec/hamster/{set/sort_spec.rb → list/init_spec.rb} +9 -8
- data/spec/hamster/list/{sort_spec.rb → intersperse_spec.rb} +4 -4
- data/spec/hamster/list/last_spec.rb +34 -0
- data/spec/hamster/list/maximum_spec.rb +8 -6
- data/spec/hamster/list/minimum_spec.rb +8 -6
- data/spec/hamster/list/partition_spec.rb +3 -3
- data/spec/hamster/list/sorting_spec.rb +82 -0
- data/spec/hamster/list/tail_spec.rb +6 -1
- data/spec/hamster/list/union_spec.rb +68 -0
- data/spec/hamster/list/uniq_spec.rb +57 -0
- data/spec/hamster/list/zip_spec.rb +3 -3
- data/spec/hamster/set/count_spec.rb +54 -0
- data/spec/hamster/set/each_spec.rb +2 -2
- data/spec/hamster/set/find_spec.rb +53 -0
- data/spec/hamster/set/include_spec.rb +1 -1
- data/spec/hamster/set/join_spec.rb +61 -0
- data/spec/hamster/set/maximum_spec.rb +69 -0
- data/spec/hamster/set/minimum_spec.rb +69 -0
- data/spec/hamster/set/partition_spec.rb +70 -0
- data/spec/hamster/set/sorting_spec.rb +58 -0
- data/spec/hamster/set/{to_list.rb → to_list_spec.rb} +1 -0
- data/spec/hamster/stack/to_a_spec.rb +36 -0
- data/spec/hamster/stack/to_list_spec.rb +33 -0
- metadata +20 -7
- data/spec/hamster/set/sort_by_spec.rb +0 -60
data/History.rdoc
CHANGED
@@ -1,3 +1,29 @@
|
|
1
|
+
=== 0.1.22 / 2010-01-10
|
2
|
+
|
3
|
+
* Implement List#union (aliased as #|).
|
4
|
+
|
5
|
+
* Implement List#uniq (aliased as #nub).
|
6
|
+
|
7
|
+
* Implement List#intersperse.
|
8
|
+
|
9
|
+
* Alias #include? as #elem?
|
10
|
+
|
11
|
+
* Implement Stack#to_list.
|
12
|
+
|
13
|
+
* Implement Stack#to_a.
|
14
|
+
|
15
|
+
* Implement Set#join.
|
16
|
+
|
17
|
+
* Implement Set#count.
|
18
|
+
|
19
|
+
* Implement List#count.
|
20
|
+
|
21
|
+
* Alias #include? as #contains?
|
22
|
+
|
23
|
+
* #each now returns nil if you specify a block.
|
24
|
+
|
25
|
+
* Implement Set#find (aliased as #detect).
|
26
|
+
|
1
27
|
=== 0.1.21 / 2010-01-08
|
2
28
|
|
3
29
|
* Implement List#sort. (Very, VERY inefficient implementation!)
|
data/lib/hamster/hash.rb
CHANGED
data/lib/hamster/list.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'monitor'
|
2
2
|
|
3
|
+
require 'hamster/set'
|
4
|
+
|
3
5
|
module Hamster
|
4
6
|
|
5
7
|
class << self
|
@@ -127,10 +129,11 @@ module Hamster
|
|
127
129
|
end
|
128
130
|
|
129
131
|
def include?(object)
|
130
|
-
|
131
|
-
false
|
132
|
+
any? { |item| item == object }
|
132
133
|
end
|
133
134
|
alias_method :member?, :include?
|
135
|
+
alias_method :contains?, :include?
|
136
|
+
alias_method :elem?, :include?
|
134
137
|
|
135
138
|
def any?
|
136
139
|
return any? { |item| item } unless block_given?
|
@@ -222,12 +225,19 @@ module Hamster
|
|
222
225
|
span { |item| !yield(item) }
|
223
226
|
end
|
224
227
|
|
228
|
+
def count(&block)
|
229
|
+
return length unless block_given?
|
230
|
+
count = 0
|
231
|
+
each { |item| count += 1 if yield(item) }
|
232
|
+
count
|
233
|
+
end
|
234
|
+
|
225
235
|
def clear
|
226
236
|
EmptyList
|
227
237
|
end
|
228
238
|
|
229
|
-
def sort
|
230
|
-
|
239
|
+
def sort(&block)
|
240
|
+
Hamster.list(*to_a.sort(&block))
|
231
241
|
end
|
232
242
|
|
233
243
|
def sort_by(&block)
|
@@ -235,9 +245,30 @@ module Hamster
|
|
235
245
|
Hamster.list(*to_a.sort_by(&block))
|
236
246
|
end
|
237
247
|
|
238
|
-
def join(sep =
|
239
|
-
|
248
|
+
def join(sep = "")
|
249
|
+
sep = sep.to_s
|
250
|
+
tail.reduce(head.to_s) { |string, item| string << sep << item.to_s }
|
251
|
+
end
|
252
|
+
|
253
|
+
def intersperse(sep)
|
254
|
+
return self if tail.empty?
|
255
|
+
Stream.new(head) { Stream.new(sep) { tail.intersperse(sep) } }
|
256
|
+
end
|
257
|
+
|
258
|
+
def uniq(items = Set.new)
|
259
|
+
list = self
|
260
|
+
while !list.empty? && items.include?(list.head)
|
261
|
+
list = list.tail
|
262
|
+
end
|
263
|
+
return list if list.empty?
|
264
|
+
Stream.new(list.head) { list.tail.uniq(items.add(list.head)) }
|
265
|
+
end
|
266
|
+
alias_method :nub, :uniq
|
267
|
+
|
268
|
+
def union(other)
|
269
|
+
self.append(other).uniq
|
240
270
|
end
|
271
|
+
alias_method :|, :union
|
241
272
|
|
242
273
|
def eql?(other)
|
243
274
|
return false unless other.is_a?(List)
|
data/lib/hamster/set.rb
CHANGED
@@ -27,6 +27,8 @@ module Hamster
|
|
27
27
|
@trie.has_key?(item)
|
28
28
|
end
|
29
29
|
alias_method :member?, :include?
|
30
|
+
alias_method :contains?, :include?
|
31
|
+
alias_method :elem?, :include?
|
30
32
|
|
31
33
|
def add(item)
|
32
34
|
if include?(item)
|
@@ -49,7 +51,6 @@ module Hamster
|
|
49
51
|
def each
|
50
52
|
return self unless block_given?
|
51
53
|
@trie.each { |entry| yield(entry.key) }
|
52
|
-
self
|
53
54
|
end
|
54
55
|
|
55
56
|
def map
|
@@ -88,52 +89,65 @@ module Hamster
|
|
88
89
|
alias_method :delete_if, :reject
|
89
90
|
|
90
91
|
def any?
|
91
|
-
|
92
|
-
|
93
|
-
else
|
94
|
-
each { |item| return true if item }
|
95
|
-
end
|
92
|
+
return any? { |item| item } unless block_given?
|
93
|
+
each { |item| return true if yield(item) }
|
96
94
|
false
|
97
95
|
end
|
98
96
|
alias_method :exist?, :any?
|
99
97
|
alias_method :exists?, :any?
|
100
98
|
|
101
99
|
def all?
|
102
|
-
|
103
|
-
|
104
|
-
else
|
105
|
-
each { |item| return false unless item }
|
106
|
-
end
|
100
|
+
return all? { |item| item } unless block_given?
|
101
|
+
each { |item| return false unless yield(item) }
|
107
102
|
true
|
108
103
|
end
|
109
104
|
|
110
105
|
def none?
|
111
|
-
|
112
|
-
|
113
|
-
else
|
114
|
-
each { |item| return false if item }
|
115
|
-
end
|
106
|
+
return none? { |item| item } unless block_given?
|
107
|
+
each { |item| return false if yield(item) }
|
116
108
|
true
|
117
109
|
end
|
118
110
|
|
111
|
+
def find
|
112
|
+
return nil unless block_given?
|
113
|
+
each { |item| return item if yield(item) }
|
114
|
+
nil
|
115
|
+
end
|
116
|
+
alias_method :detect, :find
|
117
|
+
|
118
|
+
def partition(&block)
|
119
|
+
return self unless block_given?
|
120
|
+
Stream.new(filter(&block)) { Sequence.new(reject(&block)) }
|
121
|
+
end
|
122
|
+
|
119
123
|
def grep(pattern, &block)
|
120
124
|
filter { |item| pattern === item }.map(&block)
|
121
125
|
end
|
122
126
|
|
127
|
+
def count(&block)
|
128
|
+
return length unless block_given?
|
129
|
+
count = 0
|
130
|
+
each { |item| count += 1 if yield(item) }
|
131
|
+
count
|
132
|
+
end
|
133
|
+
|
123
134
|
def head
|
124
|
-
|
125
|
-
nil
|
135
|
+
find { true }
|
126
136
|
end
|
127
137
|
alias_method :first, :head
|
128
138
|
|
129
|
-
def sort
|
130
|
-
to_list.sort
|
139
|
+
def sort(&block)
|
140
|
+
to_list.sort(&block)
|
131
141
|
end
|
132
142
|
|
133
143
|
def sort_by(&block)
|
134
144
|
to_list.sort_by(&block)
|
135
145
|
end
|
136
146
|
|
147
|
+
def join(sep = nil)
|
148
|
+
to_a.join(sep)
|
149
|
+
end
|
150
|
+
|
137
151
|
def eql?(other)
|
138
152
|
other.is_a?(self.class) && @trie.eql?(other.instance_eval{@trie})
|
139
153
|
end
|
data/lib/hamster/stack.rb
CHANGED
data/lib/hamster/trie.rb
CHANGED
data/lib/hamster/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'hamster/list'
|
4
|
+
|
5
|
+
describe Hamster::List do
|
6
|
+
|
7
|
+
describe "#chunk" do
|
8
|
+
|
9
|
+
[
|
10
|
+
[[], []],
|
11
|
+
[["A"], [Hamster.list("A")]],
|
12
|
+
[["A", "B", "C"], [Hamster.list("A", "B"), Hamster.list("C")]],
|
13
|
+
].each do |values, expected|
|
14
|
+
|
15
|
+
describe "on #{values.inspect}" do
|
16
|
+
|
17
|
+
before do
|
18
|
+
@original = Hamster.list(*values)
|
19
|
+
pending do
|
20
|
+
@result = @original.chunk(2)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "preserves the original" do
|
25
|
+
@original.should == Hamster.list(*values)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns #{expected.inspect}" do
|
29
|
+
@result.should == Hamster.list(*expected)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -4,7 +4,7 @@ require 'hamster/list'
|
|
4
4
|
|
5
5
|
describe Hamster::List do
|
6
6
|
|
7
|
-
describe "#
|
7
|
+
describe "#count" do
|
8
8
|
|
9
9
|
describe "doesn't run out of stack space on a really big" do
|
10
10
|
|
@@ -17,15 +17,18 @@ describe Hamster::List do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
after do
|
20
|
-
@list.
|
20
|
+
@list.count { true }
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
24
24
|
|
25
25
|
[
|
26
|
-
[[],
|
27
|
-
[[
|
28
|
-
[[
|
26
|
+
[[], 0],
|
27
|
+
[[1], 1],
|
28
|
+
[[1, 2], 1],
|
29
|
+
[[1, 2, 3], 2],
|
30
|
+
[[1, 2, 3, 4], 2],
|
31
|
+
[[1, 2, 3, 4, 5], 3],
|
29
32
|
].each do |values, expected|
|
30
33
|
|
31
34
|
describe "on #{values.inspect}" do
|
@@ -37,15 +40,11 @@ describe Hamster::List do
|
|
37
40
|
describe "with a block" do
|
38
41
|
|
39
42
|
before do
|
40
|
-
@result = @original.
|
41
|
-
end
|
42
|
-
|
43
|
-
it "preserves the original" do
|
44
|
-
@original.should == Hamster.list(*values)
|
43
|
+
@result = @original.count(&:odd?)
|
45
44
|
end
|
46
45
|
|
47
46
|
it "returns #{expected.inspect}" do
|
48
|
-
@result.should ==
|
47
|
+
@result.should == expected
|
49
48
|
end
|
50
49
|
|
51
50
|
end
|
@@ -53,15 +52,11 @@ describe Hamster::List do
|
|
53
52
|
describe "without a block" do
|
54
53
|
|
55
54
|
before do
|
56
|
-
@result = @original.
|
57
|
-
end
|
58
|
-
|
59
|
-
it "preserves the original" do
|
60
|
-
@original.should == Hamster.list(*values)
|
55
|
+
@result = @original.count
|
61
56
|
end
|
62
57
|
|
63
|
-
it "returns
|
64
|
-
@result.should ==
|
58
|
+
it "returns length" do
|
59
|
+
@result.should == @original.length
|
65
60
|
end
|
66
61
|
|
67
62
|
end
|
@@ -1,27 +1,28 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
2
|
|
3
|
-
require 'hamster/set'
|
4
3
|
require 'hamster/list'
|
5
4
|
|
6
|
-
describe Hamster::
|
5
|
+
describe Hamster::List do
|
7
6
|
|
8
|
-
describe "#
|
7
|
+
describe "#init" do
|
9
8
|
|
10
9
|
[
|
11
10
|
[[], []],
|
12
|
-
[["A"], [
|
13
|
-
[["
|
11
|
+
[["A"], []],
|
12
|
+
[["A", "B", "C"], ["A", "B"]],
|
14
13
|
].each do |values, expected|
|
15
14
|
|
16
15
|
describe "on #{values.inspect}" do
|
17
16
|
|
18
17
|
before do
|
19
|
-
@original = Hamster.
|
20
|
-
|
18
|
+
@original = Hamster.list(*values)
|
19
|
+
pending do
|
20
|
+
@result = @original.init
|
21
|
+
end
|
21
22
|
end
|
22
23
|
|
23
24
|
it "preserves the original" do
|
24
|
-
@original.should == Hamster.
|
25
|
+
@original.should == Hamster.list(*values)
|
25
26
|
end
|
26
27
|
|
27
28
|
it "returns #{expected.inspect}" do
|
@@ -4,7 +4,7 @@ require 'hamster/list'
|
|
4
4
|
|
5
5
|
describe Hamster::List do
|
6
6
|
|
7
|
-
describe "#
|
7
|
+
describe "#intersperse" do
|
8
8
|
|
9
9
|
describe "doesn't run out of stack space on a really big" do
|
10
10
|
|
@@ -17,7 +17,7 @@ describe Hamster::List do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
after do
|
20
|
-
@list.
|
20
|
+
@list.intersperse(":")
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
@@ -25,14 +25,14 @@ describe Hamster::List do
|
|
25
25
|
[
|
26
26
|
[[], []],
|
27
27
|
[["A"], ["A"]],
|
28
|
-
[["
|
28
|
+
[["A", "B", "C"], ["A", "|", "B", "|", "C"]]
|
29
29
|
].each do |values, expected|
|
30
30
|
|
31
31
|
describe "on #{values.inspect}" do
|
32
32
|
|
33
33
|
before do
|
34
34
|
@original = Hamster.list(*values)
|
35
|
-
@result = @original.
|
35
|
+
@result = @original.intersperse("|")
|
36
36
|
end
|
37
37
|
|
38
38
|
it "preserves the original" do
|