hamster 0.1.23 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +26 -0
- data/README.rdoc +1 -1
- data/lib/hamster.rb +1 -1
- data/lib/hamster/core_ext/io.rb +7 -5
- data/lib/hamster/hash.rb +31 -29
- data/lib/hamster/list.rb +203 -128
- data/lib/hamster/set.rb +39 -25
- data/lib/hamster/stack.rb +9 -5
- data/lib/hamster/trie.rb +5 -1
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/all_spec.rb +29 -25
- data/spec/hamster/hash/reduce_spec.rb +1 -1
- data/spec/hamster/hash/uniq_spec.rb +23 -0
- data/spec/hamster/list/all_spec.rb +55 -55
- data/spec/hamster/list/any_spec.rb +4 -8
- data/spec/hamster/list/append_spec.rb +2 -20
- data/spec/hamster/list/break_spec.rb +22 -121
- data/spec/hamster/list/cadr_spec.rb +4 -0
- data/spec/hamster/list/chunk_spec.rb +6 -0
- data/spec/hamster/list/combinations_spec.rb +51 -0
- data/spec/hamster/list/count_spec.rb +4 -8
- data/spec/hamster/list/cycle_spec.rb +5 -24
- data/spec/hamster/list/drop_spec.rb +2 -14
- data/spec/hamster/list/drop_while_spec.rb +2 -14
- data/spec/hamster/list/each_spec.rb +4 -8
- data/spec/hamster/list/eql_spec.rb +9 -10
- data/spec/hamster/list/filter_spec.rb +2 -20
- data/spec/hamster/list/find_spec.rb +4 -8
- data/spec/hamster/list/grep_spec.rb +2 -20
- data/spec/hamster/list/include_spec.rb +4 -8
- data/spec/hamster/list/init_spec.rb +4 -0
- data/spec/hamster/list/inits_spec.rb +42 -0
- data/spec/hamster/list/inspect_spec.rb +4 -8
- data/spec/hamster/list/intersperse_spec.rb +2 -14
- data/spec/hamster/list/join_spec.rb +4 -8
- data/spec/hamster/list/last_spec.rb +4 -8
- data/spec/hamster/list/map_spec.rb +2 -14
- data/spec/hamster/list/maximum_spec.rb +4 -8
- data/spec/hamster/list/minimum_spec.rb +4 -8
- data/spec/hamster/list/none_spec.rb +4 -8
- data/spec/hamster/list/one_spec.rb +4 -8
- data/spec/hamster/list/partition_spec.rb +12 -111
- data/spec/hamster/list/product_spec.rb +4 -8
- data/spec/hamster/list/reduce_spec.rb +4 -8
- data/spec/hamster/list/remove_spec.rb +2 -14
- data/spec/hamster/list/reverse_spec.rb +4 -8
- data/spec/hamster/list/size_spec.rb +4 -8
- data/spec/hamster/list/sorting_spec.rb +2 -14
- data/spec/hamster/list/span_spec.rb +22 -121
- data/spec/hamster/list/split_at_spec.rb +2 -65
- data/spec/hamster/list/sum_spec.rb +4 -8
- data/spec/hamster/list/tails_spec.rb +42 -0
- data/spec/hamster/list/take_spec.rb +2 -14
- data/spec/hamster/list/take_while_spec.rb +2 -14
- data/spec/hamster/list/to_a_spec.rb +4 -8
- data/spec/hamster/list/to_ary_spec.rb +4 -8
- data/spec/hamster/list/union_spec.rb +1 -21
- data/spec/hamster/list/uniq_spec.rb +3 -15
- data/spec/hamster/list/zip_spec.rb +1 -21
- data/spec/hamster/set/all_spec.rb +33 -29
- data/spec/hamster/set/product_spec.rb +32 -0
- data/spec/hamster/set/sum_spec.rb +32 -0
- data/spec/hamster/set/uniq_spec.rb +1 -1
- data/spec/spec.opts +0 -1
- metadata +8 -2
@@ -6,18 +6,14 @@ describe Hamster::List do
|
|
6
6
|
|
7
7
|
describe "#product" do
|
8
8
|
|
9
|
-
describe "
|
9
|
+
describe "on a really big list" do
|
10
10
|
|
11
|
-
|
11
|
+
before do
|
12
12
|
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
13
13
|
end
|
14
14
|
|
15
|
-
it "
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
@list.product
|
15
|
+
it "doesn't run out of stack" do
|
16
|
+
lambda { @list.product }.should_not raise_error
|
21
17
|
end
|
22
18
|
|
23
19
|
end
|
@@ -8,18 +8,14 @@ describe Hamster::List do
|
|
8
8
|
|
9
9
|
describe "##{method}" do
|
10
10
|
|
11
|
-
describe "
|
11
|
+
describe "on a really big list" do
|
12
12
|
|
13
|
-
|
13
|
+
before do
|
14
14
|
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
15
15
|
end
|
16
16
|
|
17
|
-
it "
|
18
|
-
@list
|
19
|
-
end
|
20
|
-
|
21
|
-
after do
|
22
|
-
@list.send(method) { }
|
17
|
+
it "doesn't run out of stack" do
|
18
|
+
lambda { @list.reduce(&:+) }.should_not raise_error
|
23
19
|
end
|
24
20
|
|
25
21
|
end
|
@@ -8,20 +8,8 @@ describe Hamster::List do
|
|
8
8
|
|
9
9
|
describe "##{method}" do
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
it "stream" do
|
14
|
-
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "list" do
|
18
|
-
@list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
19
|
-
end
|
20
|
-
|
21
|
-
after do
|
22
|
-
@list.send(method) { true }
|
23
|
-
end
|
24
|
-
|
11
|
+
it "is lazy" do
|
12
|
+
lambda { Hamster.stream { fail }.send(method) { |item| false } }.should_not raise_error
|
25
13
|
end
|
26
14
|
|
27
15
|
[
|
@@ -6,18 +6,14 @@ describe Hamster::List do
|
|
6
6
|
|
7
7
|
describe "#reverse" do
|
8
8
|
|
9
|
-
describe "
|
9
|
+
describe "on a really big list" do
|
10
10
|
|
11
|
-
|
11
|
+
before do
|
12
12
|
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
13
13
|
end
|
14
14
|
|
15
|
-
it "
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
@list.reverse
|
15
|
+
it "doesn't run out of stack" do
|
16
|
+
lambda { @list.reverse }.should_not raise_error
|
21
17
|
end
|
22
18
|
|
23
19
|
end
|
@@ -8,18 +8,14 @@ describe Hamster::List do
|
|
8
8
|
|
9
9
|
describe "##{method}" do
|
10
10
|
|
11
|
-
describe "
|
11
|
+
describe "on a really big list" do
|
12
12
|
|
13
|
-
|
13
|
+
before do
|
14
14
|
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
15
15
|
end
|
16
16
|
|
17
|
-
it "
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
after do
|
22
|
-
@list.send(method)
|
17
|
+
it "doesn't run out of stack" do
|
18
|
+
lambda { @list.size }.should_not raise_error
|
23
19
|
end
|
24
20
|
|
25
21
|
end
|
@@ -11,20 +11,8 @@ describe Hamster::List do
|
|
11
11
|
|
12
12
|
describe "##{method}" do
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
it "stream" do
|
17
|
-
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
18
|
-
end
|
19
|
-
|
20
|
-
it "list" do
|
21
|
-
@list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
22
|
-
end
|
23
|
-
|
24
|
-
after do
|
25
|
-
@list.send(method)
|
26
|
-
end
|
27
|
-
|
14
|
+
it "is lazy" do
|
15
|
+
lambda { Hamster.stream { fail }.send(method, &comparator) }.should_not raise_error
|
28
16
|
end
|
29
17
|
|
30
18
|
[
|
@@ -4,127 +4,10 @@ require 'hamster/list'
|
|
4
4
|
|
5
5
|
describe Hamster::List do
|
6
6
|
|
7
|
-
shared_examples_for "#span without a block" do
|
8
|
-
|
9
|
-
describe "without a block" do
|
10
|
-
|
11
|
-
before do
|
12
|
-
@result = @original.span
|
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 "#span is lazy" do
|
34
|
-
|
35
|
-
it "is lazy" do
|
36
|
-
count = 0
|
37
|
-
@original.span { |item| count += 1; true }
|
38
|
-
count.should <= 1
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
7
|
describe "#span" do
|
44
8
|
|
45
|
-
|
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.span { |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.span { |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 "#span without a block"
|
91
|
-
|
92
|
-
it_should_behave_like "#span 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.span { |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 "#span without a block"
|
125
|
-
|
126
|
-
it_should_behave_like "#span is lazy"
|
127
|
-
|
9
|
+
it "is lazy" do
|
10
|
+
lambda { Hamster.stream { fail }.span { true } }.should_not raise_error
|
128
11
|
end
|
129
12
|
|
130
13
|
[
|
@@ -170,9 +53,27 @@ describe Hamster::List do
|
|
170
53
|
|
171
54
|
end
|
172
55
|
|
173
|
-
|
56
|
+
describe "without a block" do
|
57
|
+
|
58
|
+
before do
|
59
|
+
@result = @original.span
|
60
|
+
@prefix = @result.car
|
61
|
+
@remainder = @result.cadr
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns a list with two items" do
|
65
|
+
@result.size.should == 2
|
66
|
+
end
|
67
|
+
|
68
|
+
it "returns self as the prefix" do
|
69
|
+
@prefix.should equal(@original)
|
70
|
+
end
|
174
71
|
|
175
|
-
|
72
|
+
it "leaves the remainder empty" do
|
73
|
+
@remainder.should be_empty
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
176
77
|
|
177
78
|
end
|
178
79
|
|
@@ -6,71 +6,8 @@ describe Hamster::List do
|
|
6
6
|
|
7
7
|
describe "#split_at" do
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
it "stream" do
|
12
|
-
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "list" do
|
16
|
-
@list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
@list.split_at(STACK_OVERFLOW_DEPTH)
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "on a stream" do
|
26
|
-
|
27
|
-
before do
|
28
|
-
count = 0
|
29
|
-
counter = Hamster.stream { count += 1 }
|
30
|
-
@result = counter.split_at(5)
|
31
|
-
@prefix = @result.car
|
32
|
-
@remainder = @result.cadr
|
33
|
-
end
|
34
|
-
|
35
|
-
it "returns a list with two items" do
|
36
|
-
@result.size.should == 2
|
37
|
-
end
|
38
|
-
|
39
|
-
it "correctly identifies the prefix" do
|
40
|
-
@prefix.should == Hamster.list(1, 2, 3, 4, 5)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "correctly identifies the remainder" do
|
44
|
-
@remainder.take(5).should == Hamster.list(6, 7, 8, 9, 10)
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
describe "on an interval" do
|
50
|
-
|
51
|
-
before do
|
52
|
-
@original = Hamster.interval(1, 10)
|
53
|
-
@result = @original.split_at(5)
|
54
|
-
@prefix = @result.car
|
55
|
-
@remainder = @result.cadr
|
56
|
-
end
|
57
|
-
|
58
|
-
it "preserves the original" do
|
59
|
-
@original.should == Hamster.interval(1, 10)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "returns a list with two items" do
|
63
|
-
@result.size.should == 2
|
64
|
-
end
|
65
|
-
|
66
|
-
it "correctly identifies the prefix" do
|
67
|
-
@prefix.should == Hamster.list(1, 2, 3, 4, 5)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "correctly identifies the remainder" do
|
71
|
-
@remainder.should == Hamster.list(6, 7, 8, 9, 10)
|
72
|
-
end
|
73
|
-
|
9
|
+
it "is lazy" do
|
10
|
+
lambda { Hamster.stream { fail }.split_at(1) }.should_not raise_error
|
74
11
|
end
|
75
12
|
|
76
13
|
[
|
@@ -6,18 +6,14 @@ describe Hamster::List do
|
|
6
6
|
|
7
7
|
describe "#sum" do
|
8
8
|
|
9
|
-
describe "
|
9
|
+
describe "on a really big list" do
|
10
10
|
|
11
|
-
|
11
|
+
before do
|
12
12
|
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
13
13
|
end
|
14
14
|
|
15
|
-
it "
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
@list.sum
|
15
|
+
it "doesn't run out of stack" do
|
16
|
+
lambda { @list.sum }.should_not raise_error
|
21
17
|
end
|
22
18
|
|
23
19
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'hamster/list'
|
4
|
+
|
5
|
+
describe Hamster::List do
|
6
|
+
|
7
|
+
describe "#tails" do
|
8
|
+
|
9
|
+
it "is lazy" do
|
10
|
+
lambda { Hamster.stream { fail }.tails }.should_not raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
[
|
14
|
+
[[], [[]]],
|
15
|
+
[["A"], [["A"], []]],
|
16
|
+
[["A", "B", "C"], [["A", "B", "C"], ["B", "C"], ["C"], []]],
|
17
|
+
].each do |values, expected|
|
18
|
+
|
19
|
+
expected = expected.map { |x| Hamster.list(*x) }
|
20
|
+
|
21
|
+
describe "on #{values.inspect}" do
|
22
|
+
|
23
|
+
before do
|
24
|
+
@original = Hamster.list(*values)
|
25
|
+
@result = @original.tails
|
26
|
+
end
|
27
|
+
|
28
|
+
it "preserves the original" do
|
29
|
+
@original.should == Hamster.list(*values)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns #{expected.inspect}" do
|
33
|
+
@result.should == Hamster.list(*expected)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -6,20 +6,8 @@ describe Hamster::List do
|
|
6
6
|
|
7
7
|
describe "#take" do
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
it "stream" do
|
12
|
-
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "list" do
|
16
|
-
@list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
@list.take(STACK_OVERFLOW_DEPTH)
|
21
|
-
end
|
22
|
-
|
9
|
+
it "is lazy" do
|
10
|
+
lambda { Hamster.stream { fail }.take(1) }.should_not raise_error
|
23
11
|
end
|
24
12
|
|
25
13
|
[
|