hamster 0.1.19 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. data/History.rdoc +10 -0
  2. data/README.rdoc +1 -1
  3. data/lib/hamster/list.rb +19 -1
  4. data/lib/hamster/stack.rb +4 -0
  5. data/lib/hamster/version.rb +1 -1
  6. data/spec/hamster/hash/all_spec.rb +5 -5
  7. data/spec/hamster/hash/any_spec.rb +5 -5
  8. data/spec/hamster/hash/eql_spec.rb +2 -2
  9. data/spec/hamster/hash/has_key_spec.rb +2 -2
  10. data/spec/hamster/hash/none_spec.rb +5 -5
  11. data/spec/hamster/list/all_spec.rb +44 -12
  12. data/spec/hamster/list/any_spec.rb +8 -8
  13. data/spec/hamster/list/append_spec.rb +2 -2
  14. data/spec/hamster/list/break_spec.rb +183 -0
  15. data/spec/hamster/list/cadr_spec.rb +7 -2
  16. data/spec/hamster/list/clear_spec.rb +36 -0
  17. data/spec/hamster/list/copying_spec.rb +3 -2
  18. data/spec/hamster/list/cycle_spec.rb +20 -7
  19. data/spec/hamster/list/drop_spec.rb +10 -5
  20. data/spec/hamster/list/drop_while_spec.rb +17 -5
  21. data/spec/hamster/list/each_spec.rb +15 -8
  22. data/spec/hamster/list/empty_spec.rb +2 -2
  23. data/spec/hamster/list/eql_spec.rb +6 -6
  24. data/spec/hamster/list/filter_spec.rb +14 -6
  25. data/spec/hamster/list/find_spec.rb +2 -2
  26. data/spec/hamster/list/grep_spec.rb +10 -5
  27. data/spec/hamster/list/head_spec.rb +16 -12
  28. data/spec/hamster/list/include_spec.rb +2 -2
  29. data/spec/hamster/list/inspect_spec.rb +2 -2
  30. data/spec/hamster/list/map_spec.rb +18 -6
  31. data/spec/hamster/list/maximum_spec.rb +2 -2
  32. data/spec/hamster/list/minimum_spec.rb +2 -2
  33. data/spec/hamster/list/none_spec.rb +8 -8
  34. data/spec/hamster/list/one_spec.rb +9 -9
  35. data/spec/hamster/list/partition_spec.rb +87 -39
  36. data/spec/hamster/list/reduce_spec.rb +2 -2
  37. data/spec/hamster/list/reject_spec.rb +14 -6
  38. data/spec/hamster/list/reverse_spec.rb +9 -4
  39. data/spec/hamster/list/size_spec.rb +2 -2
  40. data/spec/hamster/list/span_spec.rb +183 -0
  41. data/spec/hamster/list/split_at_spec.rb +68 -17
  42. data/spec/hamster/list/tail_spec.rb +2 -2
  43. data/spec/hamster/list/take_spec.rb +10 -5
  44. data/spec/hamster/list/take_while_spec.rb +15 -6
  45. data/spec/hamster/list/to_a_spec.rb +2 -2
  46. data/spec/hamster/list/to_ary_spec.rb +3 -3
  47. data/spec/hamster/list/to_list_spec.rb +3 -2
  48. data/spec/hamster/list/zip_spec.rb +16 -7
  49. data/spec/hamster/set/all_spec.rb +6 -6
  50. data/spec/hamster/set/any_spec.rb +6 -6
  51. data/spec/hamster/set/eql_spec.rb +2 -2
  52. data/spec/hamster/set/include_spec.rb +2 -2
  53. data/spec/hamster/set/none_spec.rb +6 -6
  54. data/spec/hamster/set/to_list.rb +1 -1
  55. data/spec/hamster/stack/clear_spec.rb +36 -0
  56. data/spec/hamster/stack/eql_spec.rb +2 -2
  57. data/spec/hamster/stack/pop_spec.rb +2 -2
  58. data/spec/hamster/trie/remove_spec.rb +3 -3
  59. data/spec/spec_helper.rb +2 -0
  60. metadata +9 -89
  61. data/lib/hamster/core_ext/enumerable.rbc +0 -481
  62. data/lib/hamster/core_ext/io.rbc +0 -604
  63. data/lib/hamster/hash.rbc +0 -3117
  64. data/lib/hamster/list.rbc +0 -7274
  65. data/lib/hamster/set.rbc +0 -3152
  66. data/lib/hamster/stack.rbc +0 -1358
  67. data/lib/hamster/trie.rbc +0 -3416
  68. data/spec/hamster/core_ext/enumerable_spec.rbc +0 -747
  69. data/spec/hamster/core_ext/io_spec.rbc +0 -409
  70. data/spec/hamster/hash/all_spec.rbc +0 -1214
  71. data/spec/hamster/hash/any_spec.rbc +0 -1495
  72. data/spec/hamster/hash/construction_spec.rbc +0 -742
  73. data/spec/hamster/hash/copying_spec.rbc +0 -519
  74. data/spec/hamster/hash/each_spec.rbc +0 -908
  75. data/spec/hamster/hash/empty_spec.rbc +0 -517
  76. data/spec/hamster/hash/eql_spec.rbc +0 -1702
  77. data/spec/hamster/hash/filter_spec.rbc +0 -1433
  78. data/spec/hamster/hash/get_spec.rbc +0 -791
  79. data/spec/hamster/hash/has_key_spec.rbc +0 -719
  80. data/spec/hamster/hash/map_spec.rbc +0 -1454
  81. data/spec/hamster/hash/none_spec.rbc +0 -1399
  82. data/spec/hamster/hash/put_spec.rbc +0 -1334
  83. data/spec/hamster/hash/reduce_spec.rbc +0 -1234
  84. data/spec/hamster/hash/reject_spec.rbc +0 -1445
  85. data/spec/hamster/hash/remove_spec.rbc +0 -1135
  86. data/spec/hamster/hash/size_spec.rbc +0 -596
  87. data/spec/hamster/list/all_spec.rbc +0 -1760
  88. data/spec/hamster/list/any_spec.rbc +0 -1888
  89. data/spec/hamster/list/append_spec.rbc +0 -1555
  90. data/spec/hamster/list/cadr_spec.rbc +0 -855
  91. data/spec/hamster/list/cons_spec.rbc +0 -848
  92. data/spec/hamster/list/construction_spec.rbc +0 -1732
  93. data/spec/hamster/list/copying_spec.rbc +0 -555
  94. data/spec/hamster/list/drop_spec.rbc +0 -1081
  95. data/spec/hamster/list/drop_while_spec.rbc +0 -1286
  96. data/spec/hamster/list/each_spec.rbc +0 -1365
  97. data/spec/hamster/list/empty_spec.rbc +0 -571
  98. data/spec/hamster/list/eql_spec.rbc +0 -1819
  99. data/spec/hamster/list/filter_spec.rbc +0 -1595
  100. data/spec/hamster/list/find_spec.rbc +0 -1456
  101. data/spec/hamster/list/head_spec.rbc +0 -575
  102. data/spec/hamster/list/include_spec.rbc +0 -1173
  103. data/spec/hamster/list/inspect_spec.rbc +0 -995
  104. data/spec/hamster/list/map_spec.rbc +0 -1545
  105. data/spec/hamster/list/none_spec.rbc +0 -1788
  106. data/spec/hamster/list/partition_spec.rbc +0 -2715
  107. data/spec/hamster/list/reduce_spec.rbc +0 -2081
  108. data/spec/hamster/list/reject_spec.rbc +0 -1590
  109. data/spec/hamster/list/reverse_spec.rbc +0 -1061
  110. data/spec/hamster/list/size_spec.rbc +0 -1063
  111. data/spec/hamster/list/tail_spec.rbc +0 -595
  112. data/spec/hamster/list/take_spec.rbc +0 -1075
  113. data/spec/hamster/list/take_while_spec.rbc +0 -1446
  114. data/spec/hamster/list/to_a_spec.rbc +0 -961
  115. data/spec/hamster/list/to_ary_spec.rbc +0 -1080
  116. data/spec/hamster/set/add_spec.rbc +0 -1039
  117. data/spec/hamster/set/all_spec.rbc +0 -1316
  118. data/spec/hamster/set/any_spec.rbc +0 -1444
  119. data/spec/hamster/set/construction_spec.rbc +0 -422
  120. data/spec/hamster/set/copying_spec.rbc +0 -469
  121. data/spec/hamster/set/each_spec.rbc +0 -820
  122. data/spec/hamster/set/empty_spec.rbc +0 -452
  123. data/spec/hamster/set/eql_spec.rbc +0 -1309
  124. data/spec/hamster/set/filter_spec.rbc +0 -1272
  125. data/spec/hamster/set/include_spec.rbc +0 -657
  126. data/spec/hamster/set/map_spec.rbc +0 -1285
  127. data/spec/hamster/set/none_spec.rbc +0 -1344
  128. data/spec/hamster/set/reduce_spec.rbc +0 -1177
  129. data/spec/hamster/set/reject_spec.rbc +0 -1273
  130. data/spec/hamster/set/remove_spec.rbc +0 -947
  131. data/spec/hamster/set/size_spec.rbc +0 -531
  132. data/spec/hamster/set/to_a_spec.rbc +0 -559
  133. data/spec/hamster/stack/construction_spec.rbc +0 -862
  134. data/spec/hamster/stack/copying_spec.rbc +0 -555
  135. data/spec/hamster/stack/empty_spec.rbc +0 -571
  136. data/spec/hamster/stack/eql_spec.rbc +0 -1281
  137. data/spec/hamster/stack/inspect_spec.rbc +0 -586
  138. data/spec/hamster/stack/pop_spec.rbc +0 -1099
  139. data/spec/hamster/stack/push_spec.rbc +0 -848
  140. data/spec/hamster/stack/size_spec.rbc +0 -650
  141. data/spec/hamster/stack/top_spec.rbc +0 -676
  142. data/spec/hamster/trie/remove_spec.rbc +0 -40
  143. data/spec/spec_helper.rbc +0 -163
  144. data/tasks/spec.rbc +0 -201
data/History.rdoc CHANGED
@@ -1,3 +1,13 @@
1
+ === 0.1.20 / 2010-01-07
2
+
3
+ * Implement Stack#clear.
4
+
5
+ * Implement List#clear and Stack#clear.
6
+
7
+ * Implement List#break.
8
+
9
+ * Implement List#span.
10
+
1
11
  === 0.1.19 / 2010-01-03
2
12
 
3
13
  * Bump minimum Ruby version to 1.8.7. I mean really, c'mon!
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
- EmptyList.cons(reject(&block)).cons(filter(&block))
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
@@ -39,6 +39,10 @@ module Hamster
39
39
  end
40
40
  end
41
41
 
42
+ def clear
43
+ EmptyStack
44
+ end
45
+
42
46
  def eql?(other)
43
47
  return true if other.equal?(self)
44
48
  return false unless other.class.equal?(self.class)
@@ -1,5 +1,5 @@
1
1
  module Hamster
2
2
 
3
- VERSION = "0.1.19".freeze
3
+ VERSION = "0.1.20".freeze
4
4
 
5
5
  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.all? {}.should be_true
16
+ @hash.all? {}.should == true
17
17
  end
18
18
 
19
19
  it "with no block returns true" do
20
- @hash.all?.should be_true
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 be_true
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 be_false
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 be_true
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 be_false
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 be_false
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 be_true
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 be_false
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 be_true
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 be_false
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 be_false
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 be_true
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 be_false
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 be_true
16
+ @hash.none? {}.should == true
17
17
  end
18
18
 
19
19
  it "with no block returns true" do
20
- @hash.none?.should be_true
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 be_false
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 be_true
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 be_false
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, 10000)
12
+ @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
13
13
  end
14
14
 
15
15
  it "list" do
16
- @list = (0..10000).reduce(Hamster.list) { |list, i| list.cons(i) }
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 be_true
32
+ @list.all? {}.should == true
33
33
  end
34
34
 
35
35
  it "with no block returns true" do
36
- @list.all?.should be_true
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
- it "returns true if the block always returns true" do
50
- @list.all? { |item| true }.should be_true
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
- it "returns false if the block ever returns false" do
54
- @list.all? { |item| item == "D" }.should be_false
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
- it "returns true if all values are truthy" do
62
- Hamster.list(true, "A").all?.should be_true
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
- it "returns false if any value is #{value.inspect}" do
68
- Hamster.list(value, true, "A").all?.should be_false
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, 10000)
14
+ @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
15
15
  end
16
16
 
17
17
  it "list" do
18
- @list = (0..10000).reduce(Hamster.list) { |list, i| list.cons(i) }
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 be_false
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 be_false
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 be_true
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 be_false
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, true, "A").send(method).should be_true
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 be_false
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, 10000)
14
+ @a = @b = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
15
15
  end
16
16
 
17
17
  it "list" do
18
- @a = @b = (0..10000).reduce(Hamster.list) { |list, i| list.cons(i) }
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