hamster 0.1.19 → 0.1.20
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +10 -0
- data/README.rdoc +1 -1
- data/lib/hamster/list.rb +19 -1
- data/lib/hamster/stack.rb +4 -0
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/all_spec.rb +5 -5
- data/spec/hamster/hash/any_spec.rb +5 -5
- data/spec/hamster/hash/eql_spec.rb +2 -2
- data/spec/hamster/hash/has_key_spec.rb +2 -2
- data/spec/hamster/hash/none_spec.rb +5 -5
- data/spec/hamster/list/all_spec.rb +44 -12
- data/spec/hamster/list/any_spec.rb +8 -8
- data/spec/hamster/list/append_spec.rb +2 -2
- data/spec/hamster/list/break_spec.rb +183 -0
- data/spec/hamster/list/cadr_spec.rb +7 -2
- data/spec/hamster/list/clear_spec.rb +36 -0
- data/spec/hamster/list/copying_spec.rb +3 -2
- data/spec/hamster/list/cycle_spec.rb +20 -7
- data/spec/hamster/list/drop_spec.rb +10 -5
- data/spec/hamster/list/drop_while_spec.rb +17 -5
- data/spec/hamster/list/each_spec.rb +15 -8
- data/spec/hamster/list/empty_spec.rb +2 -2
- data/spec/hamster/list/eql_spec.rb +6 -6
- data/spec/hamster/list/filter_spec.rb +14 -6
- data/spec/hamster/list/find_spec.rb +2 -2
- data/spec/hamster/list/grep_spec.rb +10 -5
- data/spec/hamster/list/head_spec.rb +16 -12
- data/spec/hamster/list/include_spec.rb +2 -2
- data/spec/hamster/list/inspect_spec.rb +2 -2
- data/spec/hamster/list/map_spec.rb +18 -6
- data/spec/hamster/list/maximum_spec.rb +2 -2
- data/spec/hamster/list/minimum_spec.rb +2 -2
- data/spec/hamster/list/none_spec.rb +8 -8
- data/spec/hamster/list/one_spec.rb +9 -9
- data/spec/hamster/list/partition_spec.rb +87 -39
- data/spec/hamster/list/reduce_spec.rb +2 -2
- data/spec/hamster/list/reject_spec.rb +14 -6
- data/spec/hamster/list/reverse_spec.rb +9 -4
- data/spec/hamster/list/size_spec.rb +2 -2
- data/spec/hamster/list/span_spec.rb +183 -0
- data/spec/hamster/list/split_at_spec.rb +68 -17
- data/spec/hamster/list/tail_spec.rb +2 -2
- data/spec/hamster/list/take_spec.rb +10 -5
- data/spec/hamster/list/take_while_spec.rb +15 -6
- data/spec/hamster/list/to_a_spec.rb +2 -2
- data/spec/hamster/list/to_ary_spec.rb +3 -3
- data/spec/hamster/list/to_list_spec.rb +3 -2
- data/spec/hamster/list/zip_spec.rb +16 -7
- data/spec/hamster/set/all_spec.rb +6 -6
- data/spec/hamster/set/any_spec.rb +6 -6
- data/spec/hamster/set/eql_spec.rb +2 -2
- data/spec/hamster/set/include_spec.rb +2 -2
- data/spec/hamster/set/none_spec.rb +6 -6
- data/spec/hamster/set/to_list.rb +1 -1
- data/spec/hamster/stack/clear_spec.rb +36 -0
- data/spec/hamster/stack/eql_spec.rb +2 -2
- data/spec/hamster/stack/pop_spec.rb +2 -2
- data/spec/hamster/trie/remove_spec.rb +3 -3
- data/spec/spec_helper.rb +2 -0
- metadata +9 -89
- data/lib/hamster/core_ext/enumerable.rbc +0 -481
- data/lib/hamster/core_ext/io.rbc +0 -604
- data/lib/hamster/hash.rbc +0 -3117
- data/lib/hamster/list.rbc +0 -7274
- data/lib/hamster/set.rbc +0 -3152
- data/lib/hamster/stack.rbc +0 -1358
- data/lib/hamster/trie.rbc +0 -3416
- data/spec/hamster/core_ext/enumerable_spec.rbc +0 -747
- data/spec/hamster/core_ext/io_spec.rbc +0 -409
- data/spec/hamster/hash/all_spec.rbc +0 -1214
- data/spec/hamster/hash/any_spec.rbc +0 -1495
- data/spec/hamster/hash/construction_spec.rbc +0 -742
- data/spec/hamster/hash/copying_spec.rbc +0 -519
- data/spec/hamster/hash/each_spec.rbc +0 -908
- data/spec/hamster/hash/empty_spec.rbc +0 -517
- data/spec/hamster/hash/eql_spec.rbc +0 -1702
- data/spec/hamster/hash/filter_spec.rbc +0 -1433
- data/spec/hamster/hash/get_spec.rbc +0 -791
- data/spec/hamster/hash/has_key_spec.rbc +0 -719
- data/spec/hamster/hash/map_spec.rbc +0 -1454
- data/spec/hamster/hash/none_spec.rbc +0 -1399
- data/spec/hamster/hash/put_spec.rbc +0 -1334
- data/spec/hamster/hash/reduce_spec.rbc +0 -1234
- data/spec/hamster/hash/reject_spec.rbc +0 -1445
- data/spec/hamster/hash/remove_spec.rbc +0 -1135
- data/spec/hamster/hash/size_spec.rbc +0 -596
- data/spec/hamster/list/all_spec.rbc +0 -1760
- data/spec/hamster/list/any_spec.rbc +0 -1888
- data/spec/hamster/list/append_spec.rbc +0 -1555
- data/spec/hamster/list/cadr_spec.rbc +0 -855
- data/spec/hamster/list/cons_spec.rbc +0 -848
- data/spec/hamster/list/construction_spec.rbc +0 -1732
- data/spec/hamster/list/copying_spec.rbc +0 -555
- data/spec/hamster/list/drop_spec.rbc +0 -1081
- data/spec/hamster/list/drop_while_spec.rbc +0 -1286
- data/spec/hamster/list/each_spec.rbc +0 -1365
- data/spec/hamster/list/empty_spec.rbc +0 -571
- data/spec/hamster/list/eql_spec.rbc +0 -1819
- data/spec/hamster/list/filter_spec.rbc +0 -1595
- data/spec/hamster/list/find_spec.rbc +0 -1456
- data/spec/hamster/list/head_spec.rbc +0 -575
- data/spec/hamster/list/include_spec.rbc +0 -1173
- data/spec/hamster/list/inspect_spec.rbc +0 -995
- data/spec/hamster/list/map_spec.rbc +0 -1545
- data/spec/hamster/list/none_spec.rbc +0 -1788
- data/spec/hamster/list/partition_spec.rbc +0 -2715
- data/spec/hamster/list/reduce_spec.rbc +0 -2081
- data/spec/hamster/list/reject_spec.rbc +0 -1590
- data/spec/hamster/list/reverse_spec.rbc +0 -1061
- data/spec/hamster/list/size_spec.rbc +0 -1063
- data/spec/hamster/list/tail_spec.rbc +0 -595
- data/spec/hamster/list/take_spec.rbc +0 -1075
- data/spec/hamster/list/take_while_spec.rbc +0 -1446
- data/spec/hamster/list/to_a_spec.rbc +0 -961
- data/spec/hamster/list/to_ary_spec.rbc +0 -1080
- data/spec/hamster/set/add_spec.rbc +0 -1039
- data/spec/hamster/set/all_spec.rbc +0 -1316
- data/spec/hamster/set/any_spec.rbc +0 -1444
- data/spec/hamster/set/construction_spec.rbc +0 -422
- data/spec/hamster/set/copying_spec.rbc +0 -469
- data/spec/hamster/set/each_spec.rbc +0 -820
- data/spec/hamster/set/empty_spec.rbc +0 -452
- data/spec/hamster/set/eql_spec.rbc +0 -1309
- data/spec/hamster/set/filter_spec.rbc +0 -1272
- data/spec/hamster/set/include_spec.rbc +0 -657
- data/spec/hamster/set/map_spec.rbc +0 -1285
- data/spec/hamster/set/none_spec.rbc +0 -1344
- data/spec/hamster/set/reduce_spec.rbc +0 -1177
- data/spec/hamster/set/reject_spec.rbc +0 -1273
- data/spec/hamster/set/remove_spec.rbc +0 -947
- data/spec/hamster/set/size_spec.rbc +0 -531
- data/spec/hamster/set/to_a_spec.rbc +0 -559
- data/spec/hamster/stack/construction_spec.rbc +0 -862
- data/spec/hamster/stack/copying_spec.rbc +0 -555
- data/spec/hamster/stack/empty_spec.rbc +0 -571
- data/spec/hamster/stack/eql_spec.rbc +0 -1281
- data/spec/hamster/stack/inspect_spec.rbc +0 -586
- data/spec/hamster/stack/pop_spec.rbc +0 -1099
- data/spec/hamster/stack/push_spec.rbc +0 -848
- data/spec/hamster/stack/size_spec.rbc +0 -650
- data/spec/hamster/stack/top_spec.rbc +0 -676
- data/spec/hamster/trie/remove_spec.rbc +0 -40
- data/spec/spec_helper.rbc +0 -163
- data/tasks/spec.rbc +0 -201
data/History.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= Hamster
|
1
|
+
= Hamster - Efficient, Immutable, Thread-Safe Collection classes for Ruby
|
2
2
|
|
3
3
|
Hamster started out as an implementation of Hash Array Mapped Trees (HAMT) for Ruby (see http://lamp.epfl.ch/papers/idealhashtrees.pdf) and has since expanded to include implementations of other Persistent Data Structures (see http://en.wikipedia.org/wiki/Persistent_data_structure) such as Sets, Lists, Stacks, etc.
|
4
4
|
|
data/lib/hamster/list.rb
CHANGED
@@ -170,7 +170,7 @@ module Hamster
|
|
170
170
|
|
171
171
|
def partition(&block)
|
172
172
|
return self unless block_given?
|
173
|
-
|
173
|
+
Stream.new(filter(&block)) { EmptyList.cons(reject(&block)) }
|
174
174
|
end
|
175
175
|
|
176
176
|
def append(other)
|
@@ -209,9 +209,24 @@ module Hamster
|
|
209
209
|
end
|
210
210
|
|
211
211
|
def split_at(number)
|
212
|
+
# Stream.new(take(number)) { EmptyList.cons(drop(number)) }
|
212
213
|
EmptyList.cons(drop(number)).cons(take(number))
|
213
214
|
end
|
214
215
|
|
216
|
+
def span(&block)
|
217
|
+
return EmptyList.cons(EmptyList).cons(self) unless block_given?
|
218
|
+
Stream.new(take_while(&block)) { EmptyList.cons(drop_while(&block)) }
|
219
|
+
end
|
220
|
+
|
221
|
+
def break(&block)
|
222
|
+
return EmptyList.cons(EmptyList).cons(self) unless block_given?
|
223
|
+
span { |item| !yield(item) }
|
224
|
+
end
|
225
|
+
|
226
|
+
def clear
|
227
|
+
EmptyList
|
228
|
+
end
|
229
|
+
|
215
230
|
def eql?(other)
|
216
231
|
return false unless other.is_a?(List)
|
217
232
|
|
@@ -276,6 +291,7 @@ module Hamster
|
|
276
291
|
include List
|
277
292
|
|
278
293
|
attr_reader :head, :tail
|
294
|
+
alias_method :first, :head
|
279
295
|
|
280
296
|
def initialize(head, tail = self)
|
281
297
|
@head = head
|
@@ -289,6 +305,7 @@ module Hamster
|
|
289
305
|
include List
|
290
306
|
|
291
307
|
attr_reader :head
|
308
|
+
alias_method :first, :head
|
292
309
|
|
293
310
|
def initialize(head, &tail)
|
294
311
|
@head = head
|
@@ -316,6 +333,7 @@ module Hamster
|
|
316
333
|
def head
|
317
334
|
nil
|
318
335
|
end
|
336
|
+
alias_method :first, :head
|
319
337
|
|
320
338
|
def tail
|
321
339
|
self
|
data/lib/hamster/stack.rb
CHANGED
data/lib/hamster/version.rb
CHANGED
@@ -13,11 +13,11 @@ describe Hamster::Hash do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "with a block returns true" do
|
16
|
-
@hash.all? {}.should
|
16
|
+
@hash.all? {}.should == true
|
17
17
|
end
|
18
18
|
|
19
19
|
it "with no block returns true" do
|
20
|
-
@hash.all?.should
|
20
|
+
@hash.all?.should == true
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
@@ -31,11 +31,11 @@ describe Hamster::Hash do
|
|
31
31
|
describe "with a block" do
|
32
32
|
|
33
33
|
it "returns true if the block always returns true" do
|
34
|
-
@hash.all? { |key, value| true }.should
|
34
|
+
@hash.all? { |key, value| true }.should == true
|
35
35
|
end
|
36
36
|
|
37
37
|
it "returns false if the block ever returns false" do
|
38
|
-
@hash.all? { |key, value| key == "D" || value == "dee" }.should
|
38
|
+
@hash.all? { |key, value| key == "D" || value == "dee" }.should == false
|
39
39
|
end
|
40
40
|
|
41
41
|
end
|
@@ -43,7 +43,7 @@ describe Hamster::Hash do
|
|
43
43
|
describe "with no block" do
|
44
44
|
|
45
45
|
it "returns true" do
|
46
|
-
@hash.all?.should
|
46
|
+
@hash.all?.should == true
|
47
47
|
end
|
48
48
|
|
49
49
|
end
|
@@ -15,11 +15,11 @@ describe Hamster::Hash do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it "with a block returns false" do
|
18
|
-
@hash.send(method) {}.should
|
18
|
+
@hash.send(method) {}.should == false
|
19
19
|
end
|
20
20
|
|
21
21
|
it "with no block returns false" do
|
22
|
-
@hash.send(method).should
|
22
|
+
@hash.send(method).should == false
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
@@ -40,11 +40,11 @@ describe Hamster::Hash do
|
|
40
40
|
].each do |pair|
|
41
41
|
|
42
42
|
it "returns true if the block ever returns true (#{pair.inspect})" do
|
43
|
-
@hash.send(method) { |key, value| key == pair.first && value == pair.last }.should
|
43
|
+
@hash.send(method) { |key, value| key == pair.first && value == pair.last }.should == true
|
44
44
|
end
|
45
45
|
|
46
46
|
it "returns false if the block always returns false" do
|
47
|
-
@hash.send(method) { |key, value| key == "D" && value == "dee" }.should
|
47
|
+
@hash.send(method) { |key, value| key == "D" && value == "dee" }.should == false
|
48
48
|
end
|
49
49
|
|
50
50
|
end
|
@@ -54,7 +54,7 @@ describe Hamster::Hash do
|
|
54
54
|
describe "with no block" do
|
55
55
|
|
56
56
|
it "returns true" do
|
57
|
-
@hash.send(method).should
|
57
|
+
@hash.send(method).should == true
|
58
58
|
end
|
59
59
|
|
60
60
|
end
|
@@ -15,11 +15,11 @@ describe Hamster::Hash do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it "a standard hash" do
|
18
|
-
@hash.send(method, "A" => "aye", "B" => "bee", "C" => "see").should
|
18
|
+
@hash.send(method, "A" => "aye", "B" => "bee", "C" => "see").should == false
|
19
19
|
end
|
20
20
|
|
21
21
|
it "an aribtrary object" do
|
22
|
-
@hash.send(method, Object.new).should
|
22
|
+
@hash.send(method, Object.new).should == false
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
@@ -15,13 +15,13 @@ describe Hamster::Hash do
|
|
15
15
|
["A", "B", "C", nil].each do |key|
|
16
16
|
|
17
17
|
it "returns true for an existing key (#{key.inspect})" do
|
18
|
-
@hash.send(method, key).should
|
18
|
+
@hash.send(method, key).should == true
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "returns false for a non-existing key" do
|
24
|
-
@hash.send(method, "D").should
|
24
|
+
@hash.send(method, "D").should == false
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
@@ -13,11 +13,11 @@ describe Hamster::Hash do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "with a block returns true" do
|
16
|
-
@hash.none? {}.should
|
16
|
+
@hash.none? {}.should == true
|
17
17
|
end
|
18
18
|
|
19
19
|
it "with no block returns true" do
|
20
|
-
@hash.none?.should
|
20
|
+
@hash.none?.should == true
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
@@ -38,11 +38,11 @@ describe Hamster::Hash do
|
|
38
38
|
].each do |pair|
|
39
39
|
|
40
40
|
it "returns false if the block ever returns true (#{pair.inspect})" do
|
41
|
-
@hash.none? { |key, value| key == pair.first && value == pair.last }.should
|
41
|
+
@hash.none? { |key, value| key == pair.first && value == pair.last }.should == false
|
42
42
|
end
|
43
43
|
|
44
44
|
it "returns true if the block always returns false" do
|
45
|
-
@hash.none? { |key, value| key == "D" && value == "dee" }.should
|
45
|
+
@hash.none? { |key, value| key == "D" && value == "dee" }.should == true
|
46
46
|
end
|
47
47
|
|
48
48
|
end
|
@@ -52,7 +52,7 @@ describe Hamster::Hash do
|
|
52
52
|
describe "with no block" do
|
53
53
|
|
54
54
|
it "returns false" do
|
55
|
-
@hash.none?.should
|
55
|
+
@hash.none?.should == false
|
56
56
|
end
|
57
57
|
|
58
58
|
end
|
@@ -9,11 +9,11 @@ describe Hamster::List do
|
|
9
9
|
describe "doesn't run out of stack space on a really big" do
|
10
10
|
|
11
11
|
it "stream" do
|
12
|
-
@list = Hamster.interval(0,
|
12
|
+
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "list" do
|
16
|
-
@list = (0
|
16
|
+
@list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
17
17
|
end
|
18
18
|
|
19
19
|
after do
|
@@ -29,11 +29,11 @@ describe Hamster::List do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it "with a block returns true" do
|
32
|
-
@list.all? {}.should
|
32
|
+
@list.all? {}.should == true
|
33
33
|
end
|
34
34
|
|
35
35
|
it "with no block returns true" do
|
36
|
-
@list.all?.should
|
36
|
+
@list.all?.should == true
|
37
37
|
end
|
38
38
|
|
39
39
|
end
|
@@ -46,26 +46,58 @@ describe Hamster::List do
|
|
46
46
|
@list = Hamster.list("A", "B", "C")
|
47
47
|
end
|
48
48
|
|
49
|
-
|
50
|
-
|
49
|
+
describe "if the block always returns true" do
|
50
|
+
|
51
|
+
before do
|
52
|
+
@result = @list.all? { |item| true }
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns true" do
|
56
|
+
@result.should == true
|
57
|
+
end
|
58
|
+
|
51
59
|
end
|
52
60
|
|
53
|
-
|
54
|
-
|
61
|
+
describe "if the block ever returns false" do
|
62
|
+
|
63
|
+
before do
|
64
|
+
@result = @list.all? { |item| item == "D" }
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns false" do
|
68
|
+
@result.should == false
|
69
|
+
end
|
70
|
+
|
55
71
|
end
|
56
72
|
|
57
73
|
end
|
58
74
|
|
59
75
|
describe "with no block" do
|
60
76
|
|
61
|
-
|
62
|
-
|
77
|
+
describe "if all values are truthy" do
|
78
|
+
|
79
|
+
before do
|
80
|
+
@result = Hamster.list(true, "A").all?
|
81
|
+
end
|
82
|
+
|
83
|
+
it "returns true" do
|
84
|
+
@result.should == true
|
85
|
+
end
|
86
|
+
|
63
87
|
end
|
64
88
|
|
65
89
|
[nil, false].each do |value|
|
66
90
|
|
67
|
-
|
68
|
-
|
91
|
+
describe "if any value is #{value.inspect}" do
|
92
|
+
|
93
|
+
before do
|
94
|
+
@result = Hamster.list(value, true, "A").all?
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns false" do
|
98
|
+
@result.should == false
|
99
|
+
end
|
100
|
+
|
69
101
|
end
|
70
102
|
|
71
103
|
end
|
@@ -11,11 +11,11 @@ describe Hamster::List do
|
|
11
11
|
describe "doesn't run out of stack space on a really big" do
|
12
12
|
|
13
13
|
it "stream" do
|
14
|
-
@list = Hamster.interval(0,
|
14
|
+
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "list" do
|
18
|
-
@list = (0
|
18
|
+
@list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
19
19
|
end
|
20
20
|
|
21
21
|
after do
|
@@ -31,11 +31,11 @@ describe Hamster::List do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "with a block returns false" do
|
34
|
-
@list.send(method) {}.should
|
34
|
+
@list.send(method) {}.should == false
|
35
35
|
end
|
36
36
|
|
37
37
|
it "with no block returns false" do
|
38
|
-
@list.send(method).should
|
38
|
+
@list.send(method).should == false
|
39
39
|
end
|
40
40
|
|
41
41
|
end
|
@@ -51,13 +51,13 @@ describe Hamster::List do
|
|
51
51
|
["A", "B", "C", nil].each do |value|
|
52
52
|
|
53
53
|
it "returns true if the block ever returns true (#{value.inspect})" do
|
54
|
-
@list.send(method) { |item| item == value }.should
|
54
|
+
@list.send(method) { |item| item == value }.should == true
|
55
55
|
end
|
56
56
|
|
57
57
|
end
|
58
58
|
|
59
59
|
it "returns false if the block always returns false" do
|
60
|
-
@list.send(method) { |item| item == "D" }.should
|
60
|
+
@list.send(method) { |item| item == "D" }.should == false
|
61
61
|
end
|
62
62
|
|
63
63
|
end
|
@@ -65,11 +65,11 @@ describe Hamster::List do
|
|
65
65
|
describe "with no block" do
|
66
66
|
|
67
67
|
it "returns true if any value is truthy" do
|
68
|
-
Hamster.list(nil, false,
|
68
|
+
Hamster.list(nil, false, "A", true).send(method).should == true
|
69
69
|
end
|
70
70
|
|
71
71
|
it "returns false if all values are falsey" do
|
72
|
-
Hamster.list(nil, false).send(method).should
|
72
|
+
Hamster.list(nil, false).send(method).should == false
|
73
73
|
end
|
74
74
|
|
75
75
|
end
|
@@ -11,11 +11,11 @@ describe Hamster::List do
|
|
11
11
|
describe "doesn't run out of stack space on a really big" do
|
12
12
|
|
13
13
|
it "stream" do
|
14
|
-
@a = @b = Hamster.interval(0,
|
14
|
+
@a = @b = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "list" do
|
18
|
-
@a = @b = (0
|
18
|
+
@a = @b = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
19
19
|
end
|
20
20
|
|
21
21
|
after do
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'hamster/list'
|
4
|
+
|
5
|
+
describe Hamster::List do
|
6
|
+
|
7
|
+
shared_examples_for "#break without a block" do
|
8
|
+
|
9
|
+
describe "without a block" do
|
10
|
+
|
11
|
+
before do
|
12
|
+
@result = @original.break
|
13
|
+
@prefix = @result.car
|
14
|
+
@remainder = @result.cadr
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns a list with two items" do
|
18
|
+
@result.size.should == 2
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns self as the prefix" do
|
22
|
+
@prefix.should equal(@original)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "leaves the remainder empty" do
|
26
|
+
@remainder.should be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
shared_examples_for "#break is lazy" do
|
34
|
+
|
35
|
+
it "is lazy" do
|
36
|
+
count = 0
|
37
|
+
@original.break { |item| count += 1; false }
|
38
|
+
count.should <= 1
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#break" do
|
44
|
+
|
45
|
+
describe "doesn't run out of stack space on a really big" do
|
46
|
+
|
47
|
+
it "stream" do
|
48
|
+
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "list" do
|
52
|
+
@list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
53
|
+
end
|
54
|
+
|
55
|
+
after do
|
56
|
+
@list.break { |item| item < 5000 }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "on a stream" do
|
62
|
+
|
63
|
+
before do
|
64
|
+
count = 0
|
65
|
+
@original = Hamster.stream { count += 1 }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "with a block" do
|
69
|
+
|
70
|
+
before do
|
71
|
+
@result = @original.break { |item| item > 5 }
|
72
|
+
@prefix = @result.car
|
73
|
+
@remainder = @result.cadr
|
74
|
+
end
|
75
|
+
|
76
|
+
it "returns a list with two items" do
|
77
|
+
@result.size.should == 2
|
78
|
+
end
|
79
|
+
|
80
|
+
it "correctly identifies the prefix" do
|
81
|
+
@prefix.should == Hamster.list(1, 2, 3, 4, 5)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "correctly identifies the remainder" do
|
85
|
+
@remainder.take(5).should == Hamster.list(6, 7, 8, 9, 10)
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
it_should_behave_like "#break without a block"
|
91
|
+
|
92
|
+
it_should_behave_like "#break is lazy"
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "on an interval" do
|
97
|
+
|
98
|
+
before do
|
99
|
+
@original = Hamster.interval(1, 10)
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "with a block" do
|
103
|
+
|
104
|
+
before do
|
105
|
+
@result = @original.break { |item| item > 5 }
|
106
|
+
@prefix = @result.car
|
107
|
+
@remainder = @result.cadr
|
108
|
+
end
|
109
|
+
|
110
|
+
it "returns a list with two items" do
|
111
|
+
@result.size.should == 2
|
112
|
+
end
|
113
|
+
|
114
|
+
it "correctly identifies the prefix" do
|
115
|
+
@prefix.should == Hamster.list(1, 2, 3, 4, 5)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "correctly identifies the remainder" do
|
119
|
+
@remainder.should == Hamster.list(6, 7, 8, 9, 10)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
it_should_behave_like "#break without a block"
|
125
|
+
|
126
|
+
it_should_behave_like "#break is lazy"
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
[
|
131
|
+
[[], [], []],
|
132
|
+
[[1], [1], []],
|
133
|
+
[[1, 2], [1, 2], []],
|
134
|
+
[[1, 2, 3], [1, 2], [3]],
|
135
|
+
[[1, 2, 3, 4], [1, 2], [3, 4]],
|
136
|
+
[[2, 3, 4], [2], [3, 4]],
|
137
|
+
[[3, 4], [], [3, 4]],
|
138
|
+
[[4], [], [4]],
|
139
|
+
].each do |values, expected_prefix, expected_remainder|
|
140
|
+
|
141
|
+
describe "on #{values.inspect}" do
|
142
|
+
|
143
|
+
before do
|
144
|
+
@original = Hamster.list(*values)
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "with a block" do
|
148
|
+
|
149
|
+
before do
|
150
|
+
@result = @original.break { |item| item > 2 }
|
151
|
+
@prefix = @result.car
|
152
|
+
@remainder = @result.cadr
|
153
|
+
end
|
154
|
+
|
155
|
+
it "preserves the original" do
|
156
|
+
@original.should == Hamster.list(*values)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "returns a list with two items" do
|
160
|
+
@result.size.should == 2
|
161
|
+
end
|
162
|
+
|
163
|
+
it "correctly identifies the prefix" do
|
164
|
+
@prefix.should == Hamster.list(*expected_prefix)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "correctly identifies the remainder" do
|
168
|
+
@remainder.should == Hamster.list(*expected_remainder)
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
it_should_behave_like "#break without a block"
|
174
|
+
|
175
|
+
it_should_behave_like "#break is lazy"
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|