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.
Files changed (37) hide show
  1. data/History.rdoc +26 -0
  2. data/lib/hamster/hash.rb +0 -1
  3. data/lib/hamster/list.rb +37 -6
  4. data/lib/hamster/set.rb +34 -20
  5. data/lib/hamster/stack.rb +9 -0
  6. data/lib/hamster/trie.rb +1 -0
  7. data/lib/hamster/version.rb +1 -1
  8. data/spec/hamster/hash/each_spec.rb +2 -2
  9. data/spec/hamster/list/chunk_spec.rb +38 -0
  10. data/spec/hamster/list/{sort_by_spec.rb → count_spec.rb} +13 -18
  11. data/spec/hamster/list/find_spec.rb +1 -1
  12. data/spec/hamster/list/include_spec.rb +1 -1
  13. data/spec/hamster/{set/sort_spec.rb → list/init_spec.rb} +9 -8
  14. data/spec/hamster/list/{sort_spec.rb → intersperse_spec.rb} +4 -4
  15. data/spec/hamster/list/last_spec.rb +34 -0
  16. data/spec/hamster/list/maximum_spec.rb +8 -6
  17. data/spec/hamster/list/minimum_spec.rb +8 -6
  18. data/spec/hamster/list/partition_spec.rb +3 -3
  19. data/spec/hamster/list/sorting_spec.rb +82 -0
  20. data/spec/hamster/list/tail_spec.rb +6 -1
  21. data/spec/hamster/list/union_spec.rb +68 -0
  22. data/spec/hamster/list/uniq_spec.rb +57 -0
  23. data/spec/hamster/list/zip_spec.rb +3 -3
  24. data/spec/hamster/set/count_spec.rb +54 -0
  25. data/spec/hamster/set/each_spec.rb +2 -2
  26. data/spec/hamster/set/find_spec.rb +53 -0
  27. data/spec/hamster/set/include_spec.rb +1 -1
  28. data/spec/hamster/set/join_spec.rb +61 -0
  29. data/spec/hamster/set/maximum_spec.rb +69 -0
  30. data/spec/hamster/set/minimum_spec.rb +69 -0
  31. data/spec/hamster/set/partition_spec.rb +70 -0
  32. data/spec/hamster/set/sorting_spec.rb +58 -0
  33. data/spec/hamster/set/{to_list.rb → to_list_spec.rb} +1 -0
  34. data/spec/hamster/stack/to_a_spec.rb +36 -0
  35. data/spec/hamster/stack/to_list_spec.rb +33 -0
  36. metadata +20 -7
  37. data/spec/hamster/set/sort_by_spec.rb +0 -60
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster::List do
6
+
7
+ describe "#last" do
8
+
9
+ [
10
+ [[], nil],
11
+ [["A"], ["A"]],
12
+ [["A", "B", "C"], "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.last
21
+ end
22
+ end
23
+
24
+ it "returns #{expected.inspect}" do
25
+ @result.should == Hamster.list(*expected)
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -29,17 +29,18 @@ describe Hamster::List do
29
29
  [
30
30
  [[], nil],
31
31
  [["A"], "A"],
32
- [["A", "B", "C"], "A"],
32
+ [["Ichi", "Ni", "San"], "Ichi"],
33
33
  ].each do |values, expected|
34
34
 
35
35
  describe "on #{values.inspect}" do
36
36
 
37
37
  before do
38
- @list = Hamster.list(*values)
38
+ original = Hamster.list(*values)
39
+ @result = original.send(method) { |maximum, item| item.length <=> maximum.length }
39
40
  end
40
41
 
41
42
  it "returns #{expected.inspect}" do
42
- @list.send(method) { |maximum, item| maximum <=> item }.should == expected
43
+ @result.should == expected
43
44
  end
44
45
 
45
46
  end
@@ -53,17 +54,18 @@ describe Hamster::List do
53
54
  [
54
55
  [[], nil],
55
56
  [["A"], "A"],
56
- [["A", "B", "C"], "C"],
57
+ [["Ichi", "Ni", "San"], "San"],
57
58
  ].each do |values, expected|
58
59
 
59
60
  describe "on #{values.inspect}" do
60
61
 
61
62
  before do
62
- @list = Hamster.list(*values)
63
+ original = Hamster.list(*values)
64
+ @result = original.send(method)
63
65
  end
64
66
 
65
67
  it "returns #{expected.inspect}" do
66
- @list.send(method).should == expected
68
+ @result.should == expected
67
69
  end
68
70
 
69
71
  end
@@ -29,17 +29,18 @@ describe Hamster::List do
29
29
  [
30
30
  [[], nil],
31
31
  [["A"], "A"],
32
- [["A", "B", "C"], "C"],
32
+ [["Ichi", "Ni", "San"], "Ni"],
33
33
  ].each do |values, expected|
34
34
 
35
35
  describe "on #{values.inspect}" do
36
36
 
37
37
  before do
38
- @list = Hamster.list(*values)
38
+ original = Hamster.list(*values)
39
+ @result = original.send(method) { |maximum, item| item.length <=> maximum.length }
39
40
  end
40
41
 
41
42
  it "returns #{expected.inspect}" do
42
- @list.send(method) { |minimum, item| minimum <=> item }.should == expected
43
+ @result.should == expected
43
44
  end
44
45
 
45
46
  end
@@ -53,17 +54,18 @@ describe Hamster::List do
53
54
  [
54
55
  [[], nil],
55
56
  [["A"], "A"],
56
- [["A", "B", "C"], "A"],
57
+ [["Ichi", "Ni", "San"], "Ichi"],
57
58
  ].each do |values, expected|
58
59
 
59
60
  describe "on #{values.inspect}" do
60
61
 
61
62
  before do
62
- @list = Hamster.list(*values)
63
+ original = Hamster.list(*values)
64
+ @result = original.send(method)
63
65
  end
64
66
 
65
67
  it "returns #{expected.inspect}" do
66
- @list.send(method).should == expected
68
+ @result.should == expected
67
69
  end
68
70
 
69
71
  end
@@ -58,7 +58,7 @@ describe Hamster::List do
58
58
  describe "with a block" do
59
59
 
60
60
  before do
61
- @result = @original.partition { |item| (item %2) != 0 }
61
+ @result = @original.partition(&:odd?)
62
62
  @matches = @result.car
63
63
  @remainder = @result.cadr
64
64
  end
@@ -92,7 +92,7 @@ describe Hamster::List do
92
92
  describe "with a block" do
93
93
 
94
94
  before do
95
- @result = @original.partition { |item| (item % 2) != 0 }
95
+ @result = @original.partition(&:odd?)
96
96
  @matches = @result.car
97
97
  @remainder = @result.cadr
98
98
  end
@@ -137,7 +137,7 @@ describe Hamster::List do
137
137
  describe "with a block" do
138
138
 
139
139
  before do
140
- @result = @original.partition { |item| (item % 2) != 0 }
140
+ @result = @original.partition(&:odd?)
141
141
  @matches = @result.car
142
142
  @remainder = @result.cadr
143
143
  end
@@ -0,0 +1,82 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster::List do
6
+
7
+ [
8
+ [:sort, lambda { |left, right| left.length <=> right.length }],
9
+ [:sort_by, lambda { |item| item.length }],
10
+ ].each do |method, comparator|
11
+
12
+ describe "##{method}" do
13
+
14
+ describe "doesn't run out of stack space on a really big" do
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
+
28
+ end
29
+
30
+ [
31
+ [[], []],
32
+ [["A"], ["A"]],
33
+ [["Ichi", "Ni", "San"], ["Ni", "San", "Ichi"]],
34
+ ].each do |values, expected|
35
+
36
+ describe "on #{values.inspect}" do
37
+
38
+ before do
39
+ @original = Hamster.list(*values)
40
+ end
41
+
42
+ describe "with a block" do
43
+
44
+ before do
45
+ @result = @original.send(method, &comparator)
46
+ end
47
+
48
+ it "preserves the original" do
49
+ @original.should == Hamster.list(*values)
50
+ end
51
+
52
+ it "returns #{expected.inspect}" do
53
+ @result.should == Hamster.list(*expected)
54
+ end
55
+
56
+ end
57
+
58
+ describe "without a block" do
59
+
60
+ before do
61
+ @result = @original.send(method)
62
+ end
63
+
64
+ it "preserves the original" do
65
+ @original.should == Hamster.list(*values)
66
+ end
67
+
68
+ it "returns #{expected.sort.inspect}" do
69
+ @result.should == Hamster.list(*expected.sort)
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -15,7 +15,12 @@ describe Hamster::List do
15
15
  describe "on #{values.inspect}" do
16
16
 
17
17
  before do
18
- @result = Hamster.list(*values).tail
18
+ @original = Hamster.list(*values)
19
+ @result = @original.tail
20
+ end
21
+
22
+ it "preserves the original" do
23
+ @original.should == Hamster.list(*values)
19
24
  end
20
25
 
21
26
  it "returns #{expected.inspect}" do
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster::List do
6
+
7
+ [:union, :|].each do |method|
8
+
9
+ describe "#union" do
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
+
25
+ end
26
+
27
+ it "is lazy" do
28
+ count = 0
29
+ a = Hamster.stream { count += 1 }
30
+ b = Hamster.stream { count += 1 }
31
+ result = a.send(method, b)
32
+ count.should <= 2
33
+ end
34
+
35
+ [
36
+ [[], [], []],
37
+ [["A"], [], ["A"]],
38
+ [["A", "B", "C"], [], ["A", "B", "C"]],
39
+ ].each do |a, b, expected|
40
+
41
+ describe "returns #{expected.inspect}" do
42
+
43
+ before do
44
+ @a = Hamster.list(*a)
45
+ @b = Hamster.list(*b)
46
+ end
47
+
48
+ it "for #{a.inspect} and #{b.inspect}" do
49
+ @result = @a.send(method, @b)
50
+ end
51
+
52
+ it "for #{b.inspect} and #{a.inspect}" do
53
+ @result = @b.send(method, @a)
54
+ end
55
+
56
+ after do
57
+ @result.should == Hamster.list(*expected)
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster::List do
6
+
7
+ [:uniq, :nub].each do |method|
8
+
9
+ describe "##{method}" do
10
+
11
+ describe "doesn't run out of stack space on a really big" do
12
+
13
+ it "stream" do
14
+ @list = Hamster.replicate(STACK_OVERFLOW_DEPTH, 0)
15
+ end
16
+
17
+ it "list" do
18
+ @list = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(0) }
19
+ end
20
+
21
+ after do
22
+ @list.send(method).each {}
23
+ end
24
+
25
+ end
26
+
27
+ [
28
+ [[], []],
29
+ [["A"], ["A"]],
30
+ [["A", "B", "C"], ["A", "B", "C"]],
31
+ [["A", "B", "A", "C", "C"], ["A", "B", "C"]],
32
+ ].each do |values, expected|
33
+
34
+ describe "on #{values.inspect}" do
35
+
36
+ before do
37
+ @original = Hamster.list(*values)
38
+ @result = @original.send(method)
39
+ end
40
+
41
+ it "preserves the original" do
42
+ @original.should == Hamster.list(*values)
43
+ end
44
+
45
+ it "returns #{expected.inspect}" do
46
+ @result.should == Hamster.list(*expected)
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -9,15 +9,15 @@ 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
- @a = @b = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
12
+ @left = @right = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
13
13
  end
14
14
 
15
15
  it "list" do
16
- @a = @b = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
16
+ @left = @right = (0...STACK_OVERFLOW_DEPTH).reduce(Hamster.list) { |list, i| list.cons(i) }
17
17
  end
18
18
 
19
19
  after do
20
- @a.zip(@b)
20
+ @left.zip(@right)
21
21
  end
22
22
 
23
23
  end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/set'
4
+
5
+ describe Hamster::Set do
6
+
7
+ describe "#count" do
8
+
9
+ [
10
+ [[], 0],
11
+ [[1], 1],
12
+ [[1, 2], 1],
13
+ [[1, 2, 3], 2],
14
+ [[1, 2, 3, 4], 2],
15
+ [[1, 2, 3, 4, 5], 3],
16
+ ].each do |values, expected|
17
+
18
+ describe "on #{values.inspect}" do
19
+
20
+ before do
21
+ @original = Hamster.set(*values)
22
+ end
23
+
24
+ describe "with a block" do
25
+
26
+ before do
27
+ @result = @original.count(&:odd?)
28
+ end
29
+
30
+ it "returns #{expected.inspect}" do
31
+ @result.should == expected
32
+ end
33
+
34
+ end
35
+
36
+ describe "without a block" do
37
+
38
+ before do
39
+ @result = @original.count
40
+ end
41
+
42
+ it "returns length" do
43
+ @result.should == @original.length
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ end