hamster 0.1.23 → 0.2.0

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.
Files changed (65) hide show
  1. data/History.rdoc +26 -0
  2. data/README.rdoc +1 -1
  3. data/lib/hamster.rb +1 -1
  4. data/lib/hamster/core_ext/io.rb +7 -5
  5. data/lib/hamster/hash.rb +31 -29
  6. data/lib/hamster/list.rb +203 -128
  7. data/lib/hamster/set.rb +39 -25
  8. data/lib/hamster/stack.rb +9 -5
  9. data/lib/hamster/trie.rb +5 -1
  10. data/lib/hamster/version.rb +1 -1
  11. data/spec/hamster/hash/all_spec.rb +29 -25
  12. data/spec/hamster/hash/reduce_spec.rb +1 -1
  13. data/spec/hamster/hash/uniq_spec.rb +23 -0
  14. data/spec/hamster/list/all_spec.rb +55 -55
  15. data/spec/hamster/list/any_spec.rb +4 -8
  16. data/spec/hamster/list/append_spec.rb +2 -20
  17. data/spec/hamster/list/break_spec.rb +22 -121
  18. data/spec/hamster/list/cadr_spec.rb +4 -0
  19. data/spec/hamster/list/chunk_spec.rb +6 -0
  20. data/spec/hamster/list/combinations_spec.rb +51 -0
  21. data/spec/hamster/list/count_spec.rb +4 -8
  22. data/spec/hamster/list/cycle_spec.rb +5 -24
  23. data/spec/hamster/list/drop_spec.rb +2 -14
  24. data/spec/hamster/list/drop_while_spec.rb +2 -14
  25. data/spec/hamster/list/each_spec.rb +4 -8
  26. data/spec/hamster/list/eql_spec.rb +9 -10
  27. data/spec/hamster/list/filter_spec.rb +2 -20
  28. data/spec/hamster/list/find_spec.rb +4 -8
  29. data/spec/hamster/list/grep_spec.rb +2 -20
  30. data/spec/hamster/list/include_spec.rb +4 -8
  31. data/spec/hamster/list/init_spec.rb +4 -0
  32. data/spec/hamster/list/inits_spec.rb +42 -0
  33. data/spec/hamster/list/inspect_spec.rb +4 -8
  34. data/spec/hamster/list/intersperse_spec.rb +2 -14
  35. data/spec/hamster/list/join_spec.rb +4 -8
  36. data/spec/hamster/list/last_spec.rb +4 -8
  37. data/spec/hamster/list/map_spec.rb +2 -14
  38. data/spec/hamster/list/maximum_spec.rb +4 -8
  39. data/spec/hamster/list/minimum_spec.rb +4 -8
  40. data/spec/hamster/list/none_spec.rb +4 -8
  41. data/spec/hamster/list/one_spec.rb +4 -8
  42. data/spec/hamster/list/partition_spec.rb +12 -111
  43. data/spec/hamster/list/product_spec.rb +4 -8
  44. data/spec/hamster/list/reduce_spec.rb +4 -8
  45. data/spec/hamster/list/remove_spec.rb +2 -14
  46. data/spec/hamster/list/reverse_spec.rb +4 -8
  47. data/spec/hamster/list/size_spec.rb +4 -8
  48. data/spec/hamster/list/sorting_spec.rb +2 -14
  49. data/spec/hamster/list/span_spec.rb +22 -121
  50. data/spec/hamster/list/split_at_spec.rb +2 -65
  51. data/spec/hamster/list/sum_spec.rb +4 -8
  52. data/spec/hamster/list/tails_spec.rb +42 -0
  53. data/spec/hamster/list/take_spec.rb +2 -14
  54. data/spec/hamster/list/take_while_spec.rb +2 -14
  55. data/spec/hamster/list/to_a_spec.rb +4 -8
  56. data/spec/hamster/list/to_ary_spec.rb +4 -8
  57. data/spec/hamster/list/union_spec.rb +1 -21
  58. data/spec/hamster/list/uniq_spec.rb +3 -15
  59. data/spec/hamster/list/zip_spec.rb +1 -21
  60. data/spec/hamster/set/all_spec.rb +33 -29
  61. data/spec/hamster/set/product_spec.rb +32 -0
  62. data/spec/hamster/set/sum_spec.rb +32 -0
  63. data/spec/hamster/set/uniq_spec.rb +1 -1
  64. data/spec/spec.opts +0 -1
  65. metadata +8 -2
@@ -8,18 +8,14 @@ describe Hamster::List do
8
8
 
9
9
  describe "##{method}" do
10
10
 
11
- describe "doesn't run out of stack space on a really big" do
11
+ describe "on a really big list" do
12
12
 
13
- it "stream" do
13
+ before do
14
14
  @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
15
15
  end
16
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.any? { false }
17
+ it "doesn't run out of stack" do
18
+ lambda { @list.send(method) { false } }.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
- describe "doesn't run out of stack space on a really big" do
12
-
13
- it "stream" do
14
- @a = @b = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
15
- end
16
-
17
- it "list" do
18
- @a = @b = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
19
- end
20
-
21
- after do
22
- @a.send(method, @b)
23
- end
24
-
11
+ it "is lazy" do
12
+ lambda { Hamster.stream { fail }.append(Hamster.stream { fail }) }.should_not raise_error
25
13
  end
26
14
 
27
15
  [
@@ -55,12 +43,6 @@ describe Hamster::List do
55
43
 
56
44
  end
57
45
 
58
- it "is lazy" do
59
- count = 0
60
- Hamster.stream { count += 1 }.append(Hamster.list("A"))
61
- count.should <= 1
62
- end
63
-
64
46
  end
65
47
 
66
48
  end
@@ -4,127 +4,10 @@ require 'hamster/list'
4
4
 
5
5
  describe Hamster::List do
6
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
7
  describe "#break" do
44
8
 
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
-
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.break { |item| false } }.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
- it_should_behave_like "#break without a block"
56
+ describe "without a block" do
57
+
58
+ before do
59
+ @result = @original.break
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
- it_should_behave_like "#break is lazy"
72
+ it "leaves the remainder empty" do
73
+ @remainder.should be_empty
74
+ end
75
+
76
+ end
176
77
 
177
78
  end
178
79
 
@@ -29,6 +29,10 @@ describe Hamster::List do
29
29
  @result = @original.send(method)
30
30
  end
31
31
 
32
+ it "responds to #{method}" do
33
+ @original.respond_to?(method, false).should == true
34
+ end
35
+
32
36
  it "preserves the original" do
33
37
  @original.should == Hamster.list(*values)
34
38
  end
@@ -6,6 +6,12 @@ describe Hamster::List do
6
6
 
7
7
  describe "#chunk" do
8
8
 
9
+ it "is lazy" do
10
+ pending do
11
+ lambda { Hamster.stream { fail }.chunk(2) }.should_not raise_error
12
+ end
13
+ end
14
+
9
15
  [
10
16
  [[], []],
11
17
  [["A"], [Hamster.list("A")]],
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster::List do
6
+
7
+ [:combinations, :combination].each do |method|
8
+
9
+ describe "##{method}" do
10
+
11
+ it "is lazy" do
12
+ lambda { Hamster.stream { fail }.combinations(2) }.should_not raise_error
13
+ end
14
+
15
+ [
16
+ [["A", "B", "C", "D"], 1, [["A"], ["B"], ["C"], ["D"]]],
17
+ [["A", "B", "C", "D"], 2, [["A", "B"], ["A", "C"], ["A", "D"], ["B", "C"], ["B", "D"], ["C", "D"]]],
18
+ [["A", "B", "C", "D"], 3, [["A", "B", "C"], ["A", "B", "D"], ["A", "C", "D"], ["B", "C", "D"]]],
19
+ [["A", "B", "C", "D"], 4, [["A", "B", "C", "D"]]],
20
+ [["A", "B", "C", "D"], 0, [[]]],
21
+ [["A", "B", "C", "D"], 5, []],
22
+ [[], 0, [[]]],
23
+ [[], 1, []],
24
+ ].each do |values, number, expected|
25
+
26
+ expected = expected.map { |x| Hamster.list(*x) }
27
+
28
+ describe "on #{values.inspect} in groups of #{number}" do
29
+
30
+ before do
31
+ @original = Hamster.list(*values)
32
+ @result = @original.send(method, number)
33
+ end
34
+
35
+ it "preserves the original" do
36
+ @original.should == Hamster.list(*values)
37
+ end
38
+
39
+ it "returns #{expected.inspect}" do
40
+ @result.should == Hamster.list(*expected)
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -6,18 +6,14 @@ describe Hamster::List do
6
6
 
7
7
  describe "#count" do
8
8
 
9
- describe "doesn't run out of stack space on a really big" do
9
+ describe "on a really big list" do
10
10
 
11
- it "stream" do
11
+ before do
12
12
  @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
13
13
  end
14
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.count { true }
15
+ it "doesn't run out of stack" do
16
+ lambda { @list.count }.should_not raise_error
21
17
  end
22
18
 
23
19
  end
@@ -6,38 +6,19 @@ describe Hamster do
6
6
 
7
7
  describe "#cycle" do
8
8
 
9
- describe "doesn't run out of stack space on a really big" do
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.cycle
21
- end
22
-
23
- end
24
-
25
9
  it "is lazy" do
26
- count = 0
27
- list = Hamster.stream { count += 1 }
28
- list.cycle
29
- count.should <= 1
10
+ lambda { Hamster.stream { fail }.cycle }.should_not raise_error
30
11
  end
31
12
 
32
13
  describe "with an empty list" do
33
14
 
34
15
  before do
35
- @original = Hamster.list
36
- @result = @original.cycle
16
+ original = Hamster.list
17
+ @result = original.cycle
37
18
  end
38
19
 
39
- it "returns self" do
40
- @result.should equal(@original)
20
+ it "returns an empty list" do
21
+ @result.should be_empty
41
22
  end
42
23
 
43
24
  end
@@ -6,20 +6,8 @@ describe Hamster::List do
6
6
 
7
7
  describe "#drop" do
8
8
 
9
- describe "doesn't run out of stack space on a really big" do
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.drop(STACK_OVERFLOW_DEPTH)
21
- end
22
-
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.drop(1) }.should_not raise_error
23
11
  end
24
12
 
25
13
  [
@@ -6,20 +6,8 @@ describe Hamster::List do
6
6
 
7
7
  describe "#drop_while" do
8
8
 
9
- describe "doesn't run out of stack space on a really big" do
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.drop_while { true }
21
- end
22
-
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.drop_while { false } }.should_not raise_error
23
11
  end
24
12
 
25
13
  [
@@ -8,18 +8,14 @@ describe Hamster::List do
8
8
 
9
9
  describe "##{method}" do
10
10
 
11
- describe "doesn't run out of stack space on a really big" do
11
+ describe "on a really big list" do
12
12
 
13
- it "stream" do
13
+ before do
14
14
  @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
15
15
  end
16
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) { }
17
+ it "doesn't run out of stack" do
18
+ lambda { @list.send(method) { |item| } }.should_not raise_error
23
19
  end
24
20
 
25
21
  end
@@ -8,20 +8,15 @@ describe Hamster::List do
8
8
 
9
9
  describe "##{method}" do
10
10
 
11
- describe "doesn't run out of stack space on a really big" do
11
+ describe "on a really big list" do
12
12
 
13
- it "stream" do
13
+ before do
14
14
  @a = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
15
15
  @b = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
16
16
  end
17
17
 
18
- it "list" do
19
- @a = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
20
- @b = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
21
- end
22
-
23
- after do
24
- @a.send(method, @b)
18
+ it "doesn't run out of stack" do
19
+ lambda { @a.send(method, @b) }.should_not raise_error
25
20
  end
26
21
 
27
22
  end
@@ -32,7 +27,7 @@ describe Hamster::List do
32
27
  @list = Hamster.list("A", "B", "C")
33
28
  end
34
29
 
35
- it "an array" do
30
+ it "an array with the same contents" do
36
31
  @list.send(method, ["A", "B", "C"]).should == false
37
32
  end
38
33
 
@@ -42,6 +37,10 @@ describe Hamster::List do
42
37
 
43
38
  end
44
39
 
40
+ it "returns false when comparing an empty list with an empty array" do
41
+ Hamster.list.send(method, []).should == false
42
+ end
43
+
45
44
  [
46
45
  [[], [], true],
47
46
  [[], [nil], false],