hamster 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,17 @@
1
+ === 0.2.2 / 2010-01-15
2
+
3
+ * Implement List#flatten.
4
+
5
+ * Implement List#each_slice (aliased as #each_chunk).
6
+
7
+ * Return a tuple rather than a list for #split_at, #partition, #break, and #span.
8
+
9
+ * Implement List#chunk.
10
+
11
+ * Implement Set#compact.
12
+
13
+ * Implement List#compact.
14
+
1
15
  === 0.2.1 / 2010-01-15
2
16
 
3
17
  * Fix bug: List#empty? would cause a stack overflow on very large streams.
@@ -1,6 +1,7 @@
1
1
  require 'forwardable'
2
2
  require 'monitor'
3
3
 
4
+ require 'hamster/tuple'
4
5
  require 'hamster/set'
5
6
 
6
7
  module Hamster
@@ -42,17 +43,8 @@ module Hamster
42
43
 
43
44
  extend Forwardable
44
45
 
45
- Undefined = Object.new
46
-
47
- CADR = /^c([ad]+)r$/
48
-
49
- def first
50
- head
51
- end
46
+ def_delegator :self, :head, :first
52
47
 
53
- def empty?
54
- false
55
- end
56
48
  def_delegator :self, :empty?, :null?
57
49
 
58
50
  def size
@@ -66,6 +58,9 @@ module Hamster
66
58
  def_delegator :self, :cons, :>>
67
59
 
68
60
  def each
61
+ # return nil if empty?
62
+ # yield(head)
63
+ # tail.each(&block)
69
64
  return self unless block_given?
70
65
  list = self
71
66
  while !list.empty?
@@ -88,6 +83,10 @@ module Hamster
88
83
  def_delegator :self, :map, :collect
89
84
 
90
85
  def reduce(memo = Undefined, &block)
86
+ # return memo if empty?
87
+ # return memo unless block_given?
88
+ # return tail.reduce(head, &block) if memo.equal?(Undefined)
89
+ # tail.reduce(yield(memo, head), &block)
91
90
  return tail.reduce(head, &block) if memo.equal?(Undefined)
92
91
  return memo unless block_given?
93
92
  each { |item| memo = yield(memo, item) }
@@ -176,6 +175,9 @@ module Hamster
176
175
  def_delegator :self, :include?, :elem?
177
176
 
178
177
  def any?
178
+ # return false if empty?
179
+ # return any? { |item| item } unless block_given?
180
+ # !! yield(head) || tail.any?(&block)
179
181
  return any? { |item| item } unless block_given?
180
182
  each { |item| return true if yield(item) }
181
183
  false
@@ -184,6 +186,9 @@ module Hamster
184
186
  def_delegator :self, :any?, :exists?
185
187
 
186
188
  def all?
189
+ # return true if empty?
190
+ # return all? { |item| item } unless block_given?
191
+ # !! yield(head) && tail.all?(&block)
187
192
  return all? { |item| item } unless block_given?
188
193
  each { |item| return false unless yield(item) }
189
194
  true
@@ -191,12 +196,18 @@ module Hamster
191
196
  def_delegator :self, :all?, :forall?
192
197
 
193
198
  def none?
199
+ # return true if empty?
200
+ # return none? { |item| item } unless block_given?
201
+ # !yield(head) && tail.none?(&block)
194
202
  return none? { |item| item } unless block_given?
195
203
  each { |item| return false if yield(item) }
196
204
  true
197
205
  end
198
206
 
199
207
  def one?(&block)
208
+ # return true if empty?
209
+ # return none? { |item| item } unless block_given?
210
+ # !yield(head) && tail.none?(&block)
200
211
  return one? { |item| item } unless block_given?
201
212
  list = self
202
213
  while !list.empty?
@@ -207,6 +218,10 @@ module Hamster
207
218
  end
208
219
 
209
220
  def find
221
+ # return nil if empty?
222
+ # return nil unless block_given?
223
+ # return head if yield(head)
224
+ # tail.find(&block)
210
225
  return nil unless block_given?
211
226
  each { |item| return item if yield(item) }
212
227
  end
@@ -214,7 +229,7 @@ module Hamster
214
229
 
215
230
  def partition(&block)
216
231
  return self unless block_given?
217
- Stream.new { Sequence.new(filter(&block), Sequence.new(remove(&block))) }
232
+ Tuple.new(filter(&block), remove(&block))
218
233
  end
219
234
 
220
235
  def append(other)
@@ -271,16 +286,16 @@ module Hamster
271
286
  end
272
287
 
273
288
  def split_at(number)
274
- Sequence.new(take(number), Sequence.new(drop(number)))
289
+ Tuple.new(take(number), drop(number))
275
290
  end
276
291
 
277
292
  def span(&block)
278
- return Sequence.new(self, Sequence.new(EmptyList)) unless block_given?
279
- Sequence.new(take_while(&block), Sequence.new(drop_while(&block)))
293
+ return Tuple.new(self, EmptyList) unless block_given?
294
+ Tuple.new(take_while(&block), drop_while(&block))
280
295
  end
281
296
 
282
297
  def break(&block)
283
- return Sequence.new(self, Sequence.new(EmptyList)) unless block_given?
298
+ return Tuple.new(self, EmptyList) unless block_given?
284
299
  span { |item| !yield(item) }
285
300
  end
286
301
 
@@ -393,9 +408,45 @@ module Hamster
393
408
  end
394
409
  def_delegator :self, :combinations, :combination
395
410
 
411
+ def compact
412
+ remove(&:nil?)
413
+ end
414
+
415
+ def chunk(number)
416
+ Stream.new do
417
+ if empty?
418
+ self
419
+ else
420
+ first, remainder = split_at(number)
421
+ Sequence.new(first, remainder.chunk(number))
422
+ end
423
+ end
424
+ end
425
+
426
+ def each_chunk(number, &block)
427
+ chunk(number).each(&block)
428
+ end
429
+ def_delegator :self, :each_chunk, :each_slice
430
+
431
+ def flatten
432
+ Stream.new do
433
+ if empty?
434
+ self
435
+ elsif head.is_a?(List)
436
+ head.append(tail.flatten)
437
+ else
438
+ Sequence.new(head, tail.flatten)
439
+ end
440
+ end
441
+ end
442
+
396
443
  def eql?(other)
444
+ # return true if other.equal?(self)
445
+ # return false unless other.is_a?(List)
446
+ # return other.empty? if empty?
447
+ # return empty? if other.empty?
448
+ # other.head.eql?(head) && other.tail.eql?(tail)
397
449
  return false unless other.is_a?(List)
398
-
399
450
  list = self
400
451
  while !list.empty? && !other.empty?
401
452
  return true if other.equal?(list)
@@ -404,7 +455,6 @@ module Hamster
404
455
  list = list.tail
405
456
  other = other.tail
406
457
  end
407
-
408
458
  other.empty? && list.empty?
409
459
  end
410
460
  def_delegator :self, :eql?, :==
@@ -417,8 +467,8 @@ module Hamster
417
467
  def to_a
418
468
  reduce([]) { |a, item| a << item }
419
469
  end
420
- def_delegator :self, :to_a, :to_ary
421
470
  def_delegator :self, :to_a, :entries
471
+ def_delegator :self, :to_a, :to_ary
422
472
 
423
473
  def to_list
424
474
  self
@@ -434,6 +484,10 @@ module Hamster
434
484
 
435
485
  private
436
486
 
487
+ Undefined = Object.new
488
+
489
+ CADR = /^c([ad]+)r$/
490
+
437
491
  def method_missing(name, *args, &block)
438
492
  if CADR === name
439
493
  accessor($1)
@@ -447,7 +501,7 @@ module Hamster
447
501
  # identify the series of car and cdr operations that is performed by the function. The order in which the 'a's and
448
502
  # 'd's appear is the inverse of the order in which the corresponding operations are performed.
449
503
  def accessor(sequence)
450
- sequence.split(//).reverse!.reduce(self) do |memo, char|
504
+ sequence.reverse.each_char.reduce(self) do |memo, char|
451
505
  case char
452
506
  when "a" then memo.head
453
507
  when "d" then memo.tail
@@ -468,6 +522,10 @@ module Hamster
468
522
  @tail = tail
469
523
  end
470
524
 
525
+ def empty?
526
+ false
527
+ end
528
+
471
529
  end
472
530
 
473
531
  class Stream
@@ -1,5 +1,6 @@
1
1
  require 'forwardable'
2
2
 
3
+ require 'hamster/tuple'
3
4
  require 'hamster/trie'
4
5
  require 'hamster/list'
5
6
 
@@ -124,7 +125,7 @@ module Hamster
124
125
 
125
126
  def partition(&block)
126
127
  return self unless block_given?
127
- Stream.new { Sequence.new(filter(&block), Sequence.new(reject(&block))) }
128
+ Tuple.new(filter(&block), reject(&block))
128
129
  end
129
130
 
130
131
  def grep(pattern, &block)
@@ -160,6 +161,10 @@ module Hamster
160
161
  to_a.join(sep)
161
162
  end
162
163
 
164
+ def compact
165
+ remove(&:nil?)
166
+ end
167
+
163
168
  def eql?(other)
164
169
  other.is_a?(self.class) && @trie.eql?(other.instance_eval{@trie})
165
170
  end
@@ -0,0 +1,40 @@
1
+ require 'forwardable'
2
+
3
+ module Hamster
4
+
5
+ class Tuple
6
+
7
+ extend Forwardable
8
+
9
+ def initialize(*items)
10
+ @items = items.freeze
11
+ end
12
+
13
+ def first
14
+ @items.first
15
+ end
16
+
17
+ def last
18
+ @items.last
19
+ end
20
+
21
+ def dup
22
+ self
23
+ end
24
+ def_delegator :self, :dup, :clone
25
+
26
+ def to_ary
27
+ @items
28
+ end
29
+
30
+ def to_a
31
+ @items.dup
32
+ end
33
+
34
+ def inspect
35
+ "(#{@items.inspect[1..-2]})"
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -1,5 +1,5 @@
1
1
  module Hamster
2
2
 
3
- VERSION = "0.2.1".freeze
3
+ VERSION = "0.2.2".freeze
4
4
 
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
+ require 'hamster/tuple'
3
4
  require 'hamster/list'
4
5
 
5
6
  describe Hamster::List do
@@ -31,16 +32,16 @@ describe Hamster::List do
31
32
 
32
33
  before do
33
34
  @result = @original.break { |item| item > 2 }
34
- @prefix = @result.car
35
- @remainder = @result.cadr
35
+ @prefix = @result.first
36
+ @remainder = @result.last
36
37
  end
37
38
 
38
39
  it "preserves the original" do
39
40
  @original.should == Hamster.list(*values)
40
41
  end
41
42
 
42
- it "returns a list with two items" do
43
- @result.size.should == 2
43
+ it "returns a tuple with two items" do
44
+ @result.is_a?(Hamster::Tuple).should == true
44
45
  end
45
46
 
46
47
  it "correctly identifies the prefix" do
@@ -57,12 +58,12 @@ describe Hamster::List do
57
58
 
58
59
  before do
59
60
  @result = @original.break
60
- @prefix = @result.car
61
- @remainder = @result.cadr
61
+ @prefix = @result.first
62
+ @remainder = @result.last
62
63
  end
63
64
 
64
- it "returns a list with two items" do
65
- @result.size.should == 2
65
+ it "returns a tuple with two items" do
66
+ @result.is_a?(Hamster::Tuple).should == true
66
67
  end
67
68
 
68
69
  it "returns self as the prefix" do
@@ -7,9 +7,7 @@ describe Hamster::List do
7
7
  describe "#chunk" do
8
8
 
9
9
  it "is lazy" do
10
- pending do
11
- lambda { Hamster.stream { fail }.chunk(2) }.should_not raise_error
12
- end
10
+ lambda { Hamster.stream { fail }.chunk(2) }.should_not raise_error
13
11
  end
14
12
 
15
13
  [
@@ -22,9 +20,7 @@ describe Hamster::List do
22
20
 
23
21
  before do
24
22
  @original = Hamster.list(*values)
25
- pending do
26
- @result = @original.chunk(2)
27
- end
23
+ @result = @original.chunk(2)
28
24
  end
29
25
 
30
26
  it "preserves the original" do
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster::List do
6
+
7
+ describe "#compact" do
8
+
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.compact }.should_not raise_error
11
+ end
12
+
13
+ [
14
+ [[], []],
15
+ [["A"], ["A"]],
16
+ [["A", "B", "C"], ["A", "B", "C"]],
17
+ [[nil], []],
18
+ [[nil, "B"], ["B"]],
19
+ [["A", nil], ["A"]],
20
+ [[nil, nil], []],
21
+ [["A", nil, "C"], ["A", "C"]],
22
+ [[nil, "B", nil], ["B"]],
23
+ ].each do |values, expected|
24
+
25
+ describe "on #{values.inspect}" do
26
+
27
+ before do
28
+ @original = Hamster.list(*values)
29
+ @result = @original.compact
30
+ end
31
+
32
+ it "preserves the original" do
33
+ @original.should == Hamster.list(*values)
34
+ end
35
+
36
+ it "returns #{expected.inspect}" do
37
+ @result.should == Hamster.list(*expected)
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster::List do
6
+
7
+ [:each_chunk, :each_slice].each do |method|
8
+
9
+ describe "##{method}" do
10
+
11
+ describe "on a really big list" do
12
+
13
+ before do
14
+ @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
15
+ end
16
+
17
+ it "doesn't run out of stack" do
18
+ lambda { @list.send(method, 1) { |item| } }.should_not raise_error
19
+ end
20
+
21
+ end
22
+
23
+ [
24
+ [[], []],
25
+ [["A"], [Hamster.list("A")]],
26
+ [["A", "B", "C"], [Hamster.list("A", "B"), Hamster.list("C")]],
27
+ ].each do |values, expected|
28
+
29
+ describe "on #{values.inspect}" do
30
+
31
+ before do
32
+ @original = Hamster.list(*values)
33
+ end
34
+
35
+ describe "with a block" do
36
+
37
+ before do
38
+ @items = []
39
+ @result = @original.send(method, 2) { |item| @items << item }
40
+ end
41
+
42
+ it "preserves the original" do
43
+ @original.should == Hamster.list(*values)
44
+ end
45
+
46
+ it "iterates over the items in order" do
47
+ @items.should == expected
48
+ end
49
+
50
+ it "returns nil" do
51
+ @result.should be_nil
52
+ end
53
+
54
+ end
55
+
56
+ describe "without a block" do
57
+
58
+ before do
59
+ @result = @original.send(method, 2)
60
+ end
61
+
62
+ it "preserves the original" do
63
+ @original.should == Hamster.list(*values)
64
+ end
65
+
66
+ it "returns the expected items" do
67
+ @result.should == Hamster.list(*expected)
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -36,7 +36,7 @@ describe Hamster::List do
36
36
 
37
37
  before do
38
38
  @items = []
39
- @result = @original.send(method) { |value| @items << value }
39
+ @result = @original.send(method) { |item| @items << item }
40
40
  end
41
41
 
42
42
  it "iterates over the items in order" do
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/list'
4
+
5
+ describe Hamster do
6
+
7
+ describe "#flatten" do
8
+
9
+ it "is lazy" do
10
+ lambda { Hamster.stream { fail }.flatten }.should_not raise_error
11
+ end
12
+
13
+ [
14
+ [[], []],
15
+ [["A"], ["A"]],
16
+ [["A", "B", "C"], ["A", "B", "C"]],
17
+ [["A", Hamster.list("B"), "C"], ["A", "B", "C"]],
18
+ [[Hamster.list("A"), Hamster.list("B"), Hamster.list("C")], ["A", "B", "C"]],
19
+ ].each do |values, expected|
20
+
21
+ describe "on #{values}" do
22
+
23
+ before do
24
+ @original = Hamster.list(*values)
25
+ @result = @original.flatten
26
+ end
27
+
28
+ it "preserves the original" do
29
+ @original.should == Hamster.list(*values)
30
+ end
31
+
32
+ it "returns an empty list" do
33
+ @result.should == Hamster.list(*expected)
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
+ require 'hamster/tuple'
3
4
  require 'hamster/list'
4
5
 
5
6
  describe Hamster::List do
@@ -31,16 +32,16 @@ describe Hamster::List do
31
32
 
32
33
  before do
33
34
  @result = @original.partition(&:odd?)
34
- @matches = @result.car
35
- @remainder = @result.cadr
35
+ @matches = @result.first
36
+ @remainder = @result.last
36
37
  end
37
38
 
38
39
  it "preserves the original" do
39
40
  @original.should == Hamster.list(*values)
40
41
  end
41
42
 
42
- it "returns a list with two items" do
43
- @result.size.should == 2
43
+ it "returns a tuple with two items" do
44
+ @result.is_a?(Hamster::Tuple).should == true
44
45
  end
45
46
 
46
47
  it "correctly identifies the matches" do
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
+ require 'hamster/tuple'
3
4
  require 'hamster/list'
4
5
 
5
6
  describe Hamster::List do
@@ -31,16 +32,16 @@ describe Hamster::List do
31
32
 
32
33
  before do
33
34
  @result = @original.span { |item| item <= 2 }
34
- @prefix = @result.car
35
- @remainder = @result.cadr
35
+ @prefix = @result.first
36
+ @remainder = @result.last
36
37
  end
37
38
 
38
39
  it "preserves the original" do
39
40
  @original.should == Hamster.list(*values)
40
41
  end
41
42
 
42
- it "returns a list with two items" do
43
- @result.size.should == 2
43
+ it "returns a tuple with two items" do
44
+ @result.is_a?(Hamster::Tuple).should == true
44
45
  end
45
46
 
46
47
  it "correctly identifies the prefix" do
@@ -57,12 +58,12 @@ describe Hamster::List do
57
58
 
58
59
  before do
59
60
  @result = @original.span
60
- @prefix = @result.car
61
- @remainder = @result.cadr
61
+ @prefix = @result.first
62
+ @remainder = @result.last
62
63
  end
63
64
 
64
- it "returns a list with two items" do
65
- @result.size.should == 2
65
+ it "returns a tuple with two items" do
66
+ @result.is_a?(Hamster::Tuple).should == true
66
67
  end
67
68
 
68
69
  it "returns self as the prefix" do
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
+ require 'hamster/tuple'
3
4
  require 'hamster/list'
4
5
 
5
6
  describe Hamster::List do
@@ -23,16 +24,16 @@ describe Hamster::List do
23
24
  before do
24
25
  @original = Hamster.list(*values)
25
26
  @result = @original.split_at(2)
26
- @prefix = @result.car
27
- @remainder = @result.cadr
27
+ @prefix = @result.first
28
+ @remainder = @result.last
28
29
  end
29
30
 
30
31
  it "preserves the original" do
31
32
  @original.should == Hamster.list(*values)
32
33
  end
33
34
 
34
- it "returns a list with two items" do
35
- @result.size.should == 2
35
+ it "returns a tuple with two items" do
36
+ @result.is_a?(Hamster::Tuple).should == true
36
37
  end
37
38
 
38
39
  it "correctly identifies the matches" do
@@ -30,10 +30,22 @@ describe Hamster::List do
30
30
 
31
31
  before do
32
32
  @list = Hamster.list(*values)
33
+ @result = @list.send(method)
33
34
  end
34
35
 
35
36
  it "returns #{values.inspect}" do
36
- @list.send(method).should == values
37
+ @result.should == values
38
+ end
39
+
40
+ it "works with splat" do
41
+ array = *@list
42
+ array.should == values
43
+ end
44
+
45
+ it "returns a mutable array" do
46
+ @result.last.should_not == "The End"
47
+ @result << "The End"
48
+ @result.last.should == "The End"
37
49
  end
38
50
 
39
51
  end
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/set'
4
+
5
+ describe Hamster::Set do
6
+
7
+ describe "#compact" do
8
+
9
+ [
10
+ [[], []],
11
+ [["A"], ["A"]],
12
+ [["A", "B", "C"], ["A", "B", "C"]],
13
+ [[nil], []],
14
+ [[nil, "B"], ["B"]],
15
+ [["A", nil], ["A"]],
16
+ [[nil, nil], []],
17
+ [["A", nil, "C"], ["A", "C"]],
18
+ [[nil, "B", nil], ["B"]],
19
+ ].each do |values, expected|
20
+
21
+ describe "on #{values.inspect}" do
22
+
23
+ before do
24
+ @original = Hamster.set(*values)
25
+ @result = @original.compact
26
+ end
27
+
28
+ it "preserves the original" do
29
+ @original.should == Hamster.set(*values)
30
+ end
31
+
32
+ it "returns #{expected.inspect}" do
33
+ @result.should == Hamster.set(*expected)
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
+ require 'hamster/tuple'
3
4
  require 'hamster/set'
4
5
 
5
6
  describe Hamster::Set do
@@ -27,16 +28,16 @@ describe Hamster::Set do
27
28
 
28
29
  before do
29
30
  @result = @original.partition(&:odd?)
30
- @matches = @result.car
31
- @remainder = @result.cadr
31
+ @matches = @result.first
32
+ @remainder = @result.last
32
33
  end
33
34
 
34
35
  it "preserves the original" do
35
36
  @original.should == Hamster.set(*values)
36
37
  end
37
38
 
38
- it "returns a list with two items" do
39
- @result.size.should == 2
39
+ it "returns a tuple with two items" do
40
+ @result.is_a?(Hamster::Tuple).should == true
40
41
  end
41
42
 
42
43
  it "correctly identifies the matches" do
@@ -18,10 +18,17 @@ describe Hamster::Set do
18
18
 
19
19
  before do
20
20
  @set = Hamster.set(*values)
21
+ @result = @set.send(method)
21
22
  end
22
23
 
23
24
  it "returns #{values.inspect}" do
24
- @set.send(method).sort.should == values.sort
25
+ @result.sort.should == values.sort
26
+ end
27
+
28
+ it "returns a mutable array" do
29
+ @result.last.should_not == "The End"
30
+ @result << "The End"
31
+ @result.last.should == "The End"
25
32
  end
26
33
 
27
34
  end
@@ -25,6 +25,12 @@ describe Hamster::Stack do
25
25
  @result.should == expected
26
26
  end
27
27
 
28
+ it "returns a mutable array" do
29
+ @result.last.should_not == "The End"
30
+ @result << "The End"
31
+ @result.last.should == "The End"
32
+ end
33
+
28
34
  end
29
35
 
30
36
  end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/tuple'
4
+
5
+ describe Hamster::Tuple do
6
+
7
+ [:dup, :clone].each do |method|
8
+
9
+ describe "#{method}" do
10
+
11
+ before do
12
+ @original = Hamster::Tuple.new("A", "B")
13
+ @result = @original.send(method)
14
+ end
15
+
16
+ it "returns self" do
17
+ @result.should equal(@original)
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/tuple'
4
+
5
+ describe Hamster::Tuple do
6
+
7
+ describe "#first" do
8
+
9
+ before do
10
+ @tuple = Hamster::Tuple.new("A", "B")
11
+ end
12
+
13
+ it "returns the first value" do
14
+ @tuple.first.should == "A"
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/tuple'
4
+
5
+ describe Hamster::Tuple do
6
+
7
+ describe "#inspect" do
8
+
9
+ before do
10
+ @list = Hamster::Tuple.new("A", "B")
11
+ end
12
+
13
+ it "returns a string with the inspected values" do
14
+ @list.inspect.should == "(\"A\", \"B\")"
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/tuple'
4
+
5
+ describe Hamster::Tuple do
6
+
7
+ describe "#last" do
8
+
9
+ before do
10
+ @tuple = Hamster::Tuple.new("A", "B")
11
+ end
12
+
13
+ it "returns the last value" do
14
+ @tuple.last.should == "B"
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/tuple'
4
+
5
+ describe Hamster::Tuple do
6
+
7
+ describe "#to_a" do
8
+
9
+ [
10
+ [],
11
+ ["A"],
12
+ ["A", "B", "C"],
13
+ ].each do |values|
14
+
15
+ describe "on #{values.inspect}" do
16
+
17
+ before do
18
+ @tuple = Hamster::Tuple.new(*values)
19
+ @result = @tuple.to_a
20
+ end
21
+
22
+ it "returns #{values.inspect}" do
23
+ @result.should == values
24
+ end
25
+
26
+ it "works with splat" do
27
+ array = *@tuple
28
+ array.should == values
29
+ end
30
+
31
+ it "returns a mutable array" do
32
+ @result.last.should_not == "The End"
33
+ @result << "The End"
34
+ @result.last.should == "The End"
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'hamster/tuple'
4
+
5
+ describe Hamster::Tuple do
6
+
7
+ describe "#to_ary" do
8
+
9
+ describe "enables implicit conversion to" do
10
+
11
+ before do
12
+ @tuple = Hamster::Tuple.new("A", "B")
13
+ end
14
+
15
+ it "call parameters" do
16
+ [@tuple].each do |a, b|
17
+ a.should == "A"
18
+ b.should == "B"
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Harris
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-15 00:00:00 +11:00
12
+ date: 2010-01-16 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -41,6 +41,7 @@ files:
41
41
  - lib/hamster/set.rb
42
42
  - lib/hamster/stack.rb
43
43
  - lib/hamster/trie.rb
44
+ - lib/hamster/tuple.rb
44
45
  - lib/hamster/version.rb
45
46
  - lib/hamster.rb
46
47
  - spec/hamster/core_ext/enumerable_spec.rb
@@ -72,6 +73,7 @@ files:
72
73
  - spec/hamster/list/chunk_spec.rb
73
74
  - spec/hamster/list/clear_spec.rb
74
75
  - spec/hamster/list/combinations_spec.rb
76
+ - spec/hamster/list/compact_spec.rb
75
77
  - spec/hamster/list/cons_spec.rb
76
78
  - spec/hamster/list/construction_spec.rb
77
79
  - spec/hamster/list/copying_spec.rb
@@ -79,11 +81,13 @@ files:
79
81
  - spec/hamster/list/cycle_spec.rb
80
82
  - spec/hamster/list/drop_spec.rb
81
83
  - spec/hamster/list/drop_while_spec.rb
84
+ - spec/hamster/list/each_slice_spec.rb
82
85
  - spec/hamster/list/each_spec.rb
83
86
  - spec/hamster/list/empty_spec.rb
84
87
  - spec/hamster/list/eql_spec.rb
85
88
  - spec/hamster/list/filter_spec.rb
86
89
  - spec/hamster/list/find_spec.rb
90
+ - spec/hamster/list/flatten_spec.rb
87
91
  - spec/hamster/list/grep_spec.rb
88
92
  - spec/hamster/list/head_spec.rb
89
93
  - spec/hamster/list/include_spec.rb
@@ -121,6 +125,7 @@ files:
121
125
  - spec/hamster/set/add_spec.rb
122
126
  - spec/hamster/set/all_spec.rb
123
127
  - spec/hamster/set/any_spec.rb
128
+ - spec/hamster/set/compact_spec.rb
124
129
  - spec/hamster/set/construction_spec.rb
125
130
  - spec/hamster/set/copying_spec.rb
126
131
  - spec/hamster/set/count_spec.rb
@@ -163,6 +168,12 @@ files:
163
168
  - spec/hamster/stack/to_list_spec.rb
164
169
  - spec/hamster/stack/top_spec.rb
165
170
  - spec/hamster/trie/remove_spec.rb
171
+ - spec/hamster/tuple/copying_spec.rb
172
+ - spec/hamster/tuple/first_spec.rb
173
+ - spec/hamster/tuple/inspect_spec.rb
174
+ - spec/hamster/tuple/last_spec.rb
175
+ - spec/hamster/tuple/to_a_spec.rb
176
+ - spec/hamster/tuple/to_ary_spec.rb
166
177
  - spec/spec.opts
167
178
  - spec/spec_helper.rb
168
179
  - tasks/spec.rb