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.
- data/History.rdoc +5 -1
- data/lib/hamster/hash.rb +5 -3
- data/lib/hamster/list.rb +15 -8
- data/lib/hamster/set.rb +5 -3
- data/lib/hamster/stack.rb +1 -1
- data/lib/hamster/trie.rb +5 -3
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/construction_spec.rb +17 -0
- data/spec/hamster/list/accessor_spec.rb +1 -1
- data/spec/hamster/list/car_spec.rb +3 -3
- data/spec/hamster/list/construction_spec.rb +17 -0
- data/spec/hamster/list/eql_spec.rb +2 -6
- data/spec/hamster/set/construction_spec.rb +17 -0
- data/spec/hamster/stack/eql_spec.rb +1 -3
- metadata +5 -2
data/History.rdoc
CHANGED
data/lib/hamster/hash.rb
CHANGED
@@ -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
|
-
|
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
|
|
data/lib/hamster/list.rb
CHANGED
@@ -2,7 +2,11 @@ module Hamster
|
|
2
2
|
|
3
3
|
class List
|
4
4
|
|
5
|
-
|
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
|
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
|
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
|
-
|
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.
|
98
|
-
when "d" then memo.
|
104
|
+
when "a" then memo.head
|
105
|
+
when "d" then memo.tail
|
99
106
|
end
|
100
107
|
end
|
101
108
|
end
|
data/lib/hamster/set.rb
CHANGED
@@ -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
|
-
|
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
|
|
data/lib/hamster/stack.rb
CHANGED
@@ -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
|
-
|
41
|
+
other.is_a?(self.class) && @list.eql?(other.instance_eval{@list})
|
42
42
|
end
|
43
43
|
alias :== :eql?
|
44
44
|
|
data/lib/hamster/trie.rb
CHANGED
@@ -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?(
|
83
|
-
return false unless
|
84
|
-
each
|
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?
|
data/lib/hamster/version.rb
CHANGED
@@ -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
|
@@ -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 "#
|
5
|
+
describe "#head" do
|
6
6
|
|
7
7
|
it "initially returns nil" do
|
8
|
-
Hamster::List.new.
|
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").
|
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
|
-
|
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
|
-
|
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
|
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.
|
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-
|
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
|