hamster 0.1.7 → 0.1.8

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.
@@ -1,6 +1,10 @@
1
+ === 0.1.8 / 2009-11-29
2
+
3
+ * Add convenience constructors: List[], Set[], and Hash[]
4
+
1
5
  === 0.1.7 / 2009-11-27
2
6
 
3
- * Implemented eql?/== for Hash and Set.
7
+ * Implemented #eql?/== for Hash and Set.
4
8
 
5
9
  === 0.1.6 / 2009-11-23
6
10
 
@@ -2,6 +2,10 @@ module Hamster
2
2
 
3
3
  class Hash
4
4
 
5
+ def self.[](pairs)
6
+ pairs.reduce(self.new) { |hash, pair| hash.put(pair.first, pair.last) }
7
+ end
8
+
5
9
  def initialize(trie = Trie.new)
6
10
  @trie = trie
7
11
  end
@@ -54,9 +58,7 @@ module Hamster
54
58
 
55
59
  # Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
56
60
  def eql?(other)
57
- return true if equal?(other)
58
- return false unless self.class.equal?(other.class)
59
- return @trie.eql?(other.instance_eval{@trie})
61
+ other.is_a?(self.class) && @trie.eql?(other.instance_eval{@trie})
60
62
  end
61
63
  alias :== :eql?
62
64
 
@@ -2,7 +2,11 @@ module Hamster
2
2
 
3
3
  class List
4
4
 
5
- include Enumerable
5
+ def self.[](*items)
6
+ list = self.new
7
+ items.reverse_each { |item| list = list.cons(item) }
8
+ list
9
+ end
6
10
 
7
11
  def initialize(head = nil, tail = self)
8
12
  @head = head
@@ -24,12 +28,12 @@ module Hamster
24
28
  end
25
29
 
26
30
  # Returns the first item.
27
- def car
31
+ def head
28
32
  @head
29
33
  end
30
34
 
31
35
  # Returns a copy of <tt>self</tt> without the first item.
32
- def cdr
36
+ def tail
33
37
  @tail
34
38
  end
35
39
 
@@ -51,7 +55,10 @@ module Hamster
51
55
 
52
56
  # Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
53
57
  def eql?(other)
54
- blammo!
58
+ return true if other.equal?(self)
59
+ return false unless other.is_a?(self.class)
60
+ return true if other.empty? && empty?
61
+ return other.head == head && other.tail.eql?(tail)
55
62
  end
56
63
  alias :== :eql?
57
64
 
@@ -60,7 +67,7 @@ module Hamster
60
67
  self
61
68
  end
62
69
  alias :clone :dup
63
-
70
+
64
71
  def map
65
72
  if empty?
66
73
  self
@@ -68,7 +75,7 @@ module Hamster
68
75
  @tail.map { |item| yield(item) }.cons(yield(@head))
69
76
  end
70
77
  end
71
-
78
+
72
79
  def reduce(memo)
73
80
  if empty?
74
81
  memo
@@ -94,8 +101,8 @@ module Hamster
94
101
  def accessor(sequence)
95
102
  sequence.split(//).reverse!.inject(self) do |memo, char|
96
103
  case char
97
- when "a" then memo.car
98
- when "d" then memo.cdr
104
+ when "a" then memo.head
105
+ when "d" then memo.tail
99
106
  end
100
107
  end
101
108
  end
@@ -2,6 +2,10 @@ module Hamster
2
2
 
3
3
  class Set
4
4
 
5
+ def self.[](*items)
6
+ items.reduce(self.new) { |set, item| set.add(item) }
7
+ end
8
+
5
9
  def initialize(trie = Trie.new)
6
10
  @trie = trie
7
11
  end
@@ -50,9 +54,7 @@ module Hamster
50
54
 
51
55
  # Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
52
56
  def eql?(other)
53
- return true if equal?(other)
54
- return false unless self.class.equal?(other.class)
55
- return @trie.eql?(other.instance_eval{@trie})
57
+ other.is_a?(self.class) && @trie.eql?(other.instance_eval{@trie})
56
58
  end
57
59
  alias :== :eql?
58
60
 
@@ -38,7 +38,7 @@ module Hamster
38
38
 
39
39
  # Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
40
40
  def eql?(other)
41
- equal?(other) || (self.class.equal?(other.class) && @list.eql?(other.instance_eval{@list}))
41
+ other.is_a?(self.class) && @list.eql?(other.instance_eval{@list})
42
42
  end
43
43
  alias :== :eql?
44
44
 
@@ -79,9 +79,11 @@ module Hamster
79
79
 
80
80
  # Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
81
81
  def eql?(other)
82
- return true if equal?(other)
83
- return false unless self.class.equal?(other.class) && self.size == other.size
84
- each { |entry| return false unless other.include?(entry.key, entry.value) }
82
+ return true if other.equal?(self)
83
+ return false unless other.is_a?(self.class) && other.size == size
84
+ each do |entry|
85
+ return false unless other.include?(entry.key, entry.value)
86
+ end
85
87
  true
86
88
  end
87
89
  alias :== :eql?
@@ -1,5 +1,5 @@
1
1
  module Hamster
2
2
 
3
- VERSION = "0.1.7".freeze
3
+ VERSION = "0.1.8".freeze
4
4
 
5
5
  end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Hash do
4
+
5
+ describe ".[]" do
6
+
7
+ before do
8
+ @hash = Hamster::Hash["A" => "aye", "B" => "bee", "C" => "see"]
9
+ end
10
+
11
+ it "is equivalent to repeatedly using #put" do
12
+ @hash.should eql(Hamster::Hash.new.put("A", "aye").put("B", "bee").put("C", "see"))
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -10,7 +10,7 @@ describe Hamster::List do
10
10
 
11
11
  describe "##{method_name}" do
12
12
 
13
- it "initially returns nil" do
13
+ it "when empty is nil" do
14
14
  Hamster::List.new.send(method_name).should be_nil
15
15
  end
16
16
 
@@ -2,14 +2,14 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::List do
4
4
 
5
- describe "#car" do
5
+ describe "#head" do
6
6
 
7
7
  it "initially returns nil" do
8
- Hamster::List.new.car.should be_nil
8
+ Hamster::List.new.head.should be_nil
9
9
  end
10
10
 
11
11
  it "returns the first item in the list" do
12
- Hamster::List.new.cons("A").car.should == "A"
12
+ Hamster::List.new.cons("A").head.should == "A"
13
13
  end
14
14
 
15
15
  end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::List do
4
+
5
+ describe ".[]" do
6
+
7
+ before do
8
+ @list = Hamster::List["A", "B", "C"]
9
+ end
10
+
11
+ it "is equivalent to repeatedly using #cons" do
12
+ @list.should eql(Hamster::List.new.cons("C").cons("B").cons("A"))
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -6,15 +6,11 @@ describe Hamster::List do
6
6
 
7
7
  it "is true for the same instance" do
8
8
  list = Hamster::List.new
9
- pending do
10
- list.should eql(list)
11
- end
9
+ list.should eql(list)
12
10
  end
13
11
 
14
12
  it "is true for two empty instances" do
15
- pending do
16
- Hamster::List.new.should eql(Hamster::List.new)
17
- end
13
+ Hamster::List.new.should eql(Hamster::List.new)
18
14
  end
19
15
 
20
16
  end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set do
4
+
5
+ describe ".[]" do
6
+
7
+ before do
8
+ @set = Hamster::Set["A", "B", "C"]
9
+ end
10
+
11
+ it "is equivalent to repeatedly using #add" do
12
+ @set.should eql(Hamster::Set.new.add("A").add("B").add("C"))
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -10,9 +10,7 @@ describe Hamster::Stack do
10
10
  end
11
11
 
12
12
  it "is true for two empty instances" do
13
- pending do
14
- Hamster::Stack.new.should eql(Hamster::Stack.new)
15
- end
13
+ Hamster::Stack.new.should eql(Hamster::Stack.new)
16
14
  end
17
15
 
18
16
  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.1.7
4
+ version: 0.1.8
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: 2009-11-28 00:00:00 +11:00
12
+ date: 2009-11-29 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -32,6 +32,7 @@ files:
32
32
  - lib/hamster/trie.rb
33
33
  - lib/hamster/version.rb
34
34
  - lib/hamster.rb
35
+ - spec/hamster/hash/construction_spec.rb
35
36
  - spec/hamster/hash/copying_spec.rb
36
37
  - spec/hamster/hash/each_spec.rb
37
38
  - spec/hamster/hash/empty_spec.rb
@@ -42,12 +43,14 @@ files:
42
43
  - spec/hamster/hash/remove_spec.rb
43
44
  - spec/hamster/list/accessor_spec.rb
44
45
  - spec/hamster/list/car_spec.rb
46
+ - spec/hamster/list/construction_spec.rb
45
47
  - spec/hamster/list/copying_spec.rb
46
48
  - spec/hamster/list/each_spec.rb
47
49
  - spec/hamster/list/empty_spec.rb
48
50
  - spec/hamster/list/eql_spec.rb
49
51
  - spec/hamster/list/map_spec.rb
50
52
  - spec/hamster/list/reduce_spec.rb
53
+ - spec/hamster/set/construction_spec.rb
51
54
  - spec/hamster/stack/copying_spec.rb
52
55
  - spec/hamster/stack/empty_spec.rb
53
56
  - spec/hamster/stack/eql_spec.rb