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,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
- @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) { false }
23
- end
24
-
11
+ it "is lazy" do
12
+ lambda { Hamster.stream { fail }.filter { |item| false } }.should_not raise_error
25
13
  end
26
14
 
27
15
  [
@@ -52,12 +40,6 @@ describe Hamster::List do
52
40
  @result.should == Hamster.list(*expected)
53
41
  end
54
42
 
55
- it "is lazy" do
56
- count = 0
57
- @original.send(method) { |item| count += 1; true }
58
- count.should <= 1
59
- end
60
-
61
43
  end
62
44
 
63
45
  describe "without a block" do
@@ -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) { 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
@@ -6,20 +6,8 @@ describe Hamster::List do
6
6
 
7
7
  describe "#grep" 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.grep(Object)
21
- end
22
-
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.grep(Object) { |item| item } }.should_not raise_error
23
11
  end
24
12
 
25
13
  describe "without a block" do
@@ -71,12 +59,6 @@ describe Hamster::List do
71
59
  @result.should == Hamster.list(*expected)
72
60
  end
73
61
 
74
- it "is lazy" do
75
- count = 0
76
- @original.grep(Object) { |item| count += 1; item }
77
- count.should <= 1
78
- end
79
-
80
62
  end
81
63
 
82
64
  end
@@ -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, nil)
17
+ it "doesn't run out of stack" do
18
+ lambda { @list.send(method, nil) }.should_not raise_error
23
19
  end
24
20
 
25
21
  end
@@ -6,6 +6,10 @@ describe Hamster::List do
6
6
 
7
7
  describe "#init" do
8
8
 
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { false }.init }.should_not raise_error
11
+ end
12
+
9
13
  [
10
14
  [[], []],
11
15
  [["A"], []],
@@ -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 "#inits" do
8
+
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.inits }.should_not raise_error
11
+ end
12
+
13
+ [
14
+ [[], [[]]],
15
+ [["A"], [[], ["A"]]],
16
+ [["A", "B", "C"], [[], ["A"], ["A", "B"], ["A", "B", "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.inits
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,18 +6,14 @@ describe Hamster::List do
6
6
 
7
7
  describe "#inspect" 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.inspect
15
+ it "doesn't run out of stack" do
16
+ lambda { @list.inspect }.should_not raise_error
21
17
  end
22
18
 
23
19
  end
@@ -6,20 +6,8 @@ describe Hamster::List do
6
6
 
7
7
  describe "#intersperse" 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.intersperse(":")
21
- end
22
-
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.intersperse("") }.should_not raise_error
23
11
  end
24
12
 
25
13
  [
@@ -6,18 +6,14 @@ describe Hamster::List do
6
6
 
7
7
  describe "#join" 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.join
15
+ it "doesn't run out of stack" do
16
+ lambda { @list.join }.should_not raise_error
21
17
  end
22
18
 
23
19
  end
@@ -6,18 +6,14 @@ describe Hamster::List do
6
6
 
7
7
  describe "#last" 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.last
15
+ it "doesn't run out of stack" do
16
+ lambda { @list.last }.should_not raise_error
21
17
  end
22
18
 
23
19
  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
- @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) { |item| item }
23
- end
24
-
11
+ it "is lazy" do
12
+ lambda { Hamster.stream { fail }.map { |item| item } }.should_not raise_error
25
13
  end
26
14
 
27
15
  [
@@ -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) }.should_not raise_error
23
19
  end
24
20
 
25
21
  end
@@ -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) }.should_not raise_error
23
19
  end
24
20
 
25
21
  end
@@ -6,18 +6,14 @@ describe Hamster::List do
6
6
 
7
7
  describe "#none?" 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.none? { false }
15
+ it "doesn't run out of stack" do
16
+ lambda { @list.none? { false } }.should_not raise_error
21
17
  end
22
18
 
23
19
  end
@@ -6,18 +6,14 @@ describe Hamster::List do
6
6
 
7
7
  describe "#one?" 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.one? { false }
15
+ it "doesn't run out of stack" do
16
+ lambda { @list.one? { false } }.should_not raise_error
21
17
  end
22
18
 
23
19
  end
@@ -4,117 +4,10 @@ require 'hamster/list'
4
4
 
5
5
  describe Hamster::List do
6
6
 
7
- shared_examples_for "#partition without a block" do
8
-
9
- describe "without a block" do
10
-
11
- before do
12
- @result = @original.partition
13
- end
14
-
15
- it "returns self" do
16
- @result.should equal(@original)
17
- end
18
-
19
- end
20
-
21
- end
22
-
23
- shared_examples_for "#partition is lazy" do
24
-
25
- it "is lazy" do
26
- count = 0
27
- @original.partition { |item| count += 1; true }
28
- count.should <= 1
29
- end
30
-
31
- end
32
-
33
7
  describe "#partition" do
34
8
 
35
- describe "doesn't run out of stack space on a really big" do
36
-
37
- it "stream" do
38
- @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
39
- end
40
-
41
- it "list" do
42
- @list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
43
- end
44
-
45
- after do
46
- @list.partition { |item| item % 2 == 0 }
47
- end
48
-
49
- end
50
-
51
- describe "on a stream" do
52
-
53
- before do
54
- count = 0
55
- @original = Hamster.stream { count += 1 }
56
- end
57
-
58
- describe "with a block" do
59
-
60
- before do
61
- @result = @original.partition(&:odd?)
62
- @matches = @result.car
63
- @remainder = @result.cadr
64
- end
65
-
66
- it "returns a list with two items" do
67
- @result.size.should == 2
68
- end
69
-
70
- it "correctly identifies the matches" do
71
- @matches.take(5).should == Hamster.list(1, 3, 5, 7, 9)
72
- end
73
-
74
- it "correctly identifies the remainder" do
75
- @remainder.take(5).should == Hamster.list(2, 4, 6, 8, 10)
76
- end
77
-
78
- end
79
-
80
- it_should_behave_like "#partition without a block"
81
-
82
- it_should_behave_like "#partition is lazy"
83
-
84
- end
85
-
86
- describe "on an interval" do
87
-
88
- before do
89
- @original = Hamster.interval(0, 10)
90
- end
91
-
92
- describe "with a block" do
93
-
94
- before do
95
- @result = @original.partition(&:odd?)
96
- @matches = @result.car
97
- @remainder = @result.cadr
98
- end
99
-
100
- it "returns a list with two items" do
101
- @result.size.should == 2
102
- end
103
-
104
- it "correctly identifies the matches" do
105
- @matches.should == Hamster.list(1, 3, 5, 7, 9)
106
- end
107
-
108
- it "correctly identifies the remainder" do
109
- @remainder.should == Hamster.list(0, 2, 4, 6, 8, 10)
110
- end
111
-
112
- end
113
-
114
- it_should_behave_like "#partition without a block"
115
-
116
- it_should_behave_like "#partition is lazy"
117
-
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.partition }.should_not raise_error
118
11
  end
119
12
 
120
13
  [
@@ -160,9 +53,17 @@ describe Hamster::List do
160
53
 
161
54
  end
162
55
 
163
- it_should_behave_like "#partition without a block"
56
+ describe "without a block" do
57
+
58
+ before do
59
+ @result = @original.partition
60
+ end
61
+
62
+ it "returns self" do
63
+ @result.should equal(@original)
64
+ end
164
65
 
165
- it_should_behave_like "#partition is lazy"
66
+ end
166
67
 
167
68
  end
168
69