Skip to content

Commit

Permalink
Fix a bunch of spec failures under 1.8.7 (the code actually worked bu…
Browse files Browse the repository at this point in the history
…t the specs failed).

Introduce EmptyTrie to remove redundant calls to Trie.new.
Bump version.
  • Loading branch information
Simon Harris committed Feb 14, 2010
1 parent cf82a95 commit 9eb08d7
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 20 deletions.
6 changes: 6 additions & 0 deletions History.rdoc
@@ -1,3 +1,9 @@
=== 0.2.10 / 2010-02-15

* Fix a bunch of spec failures under 1.8.7.

* Remove some redundant object construction.

=== 0.2.9 / 2010-02-11

* Fix #to_list not available in all Enumerables.
Expand Down
4 changes: 2 additions & 2 deletions lib/hamster/hash.rb
Expand Up @@ -12,7 +12,7 @@ class Hash

extend Forwardable

def initialize(trie = Trie.new)
def initialize(trie = EmptyTrie)
@trie = trie
end

Expand Down Expand Up @@ -61,7 +61,7 @@ def each
def map
return self unless block_given?
return self if empty?
self.class.new(@trie.reduce(Trie.new) { |trie, entry| trie.put(*yield(entry.key, entry.value)) })
self.class.new(@trie.reduce(EmptyTrie) { |trie, entry| trie.put(*yield(entry.key, entry.value)) })
end
def_delegator :self, :map, :collect

Expand Down
6 changes: 5 additions & 1 deletion lib/hamster/list.rb
Expand Up @@ -325,7 +325,7 @@ def intersperse(sep)
end
end

def uniq(items = Set.new)
def uniq(items = EmptySet)
Stream.new do
next self if empty?
next tail.uniq(items) if items.include?(head)
Expand Down Expand Up @@ -505,6 +505,10 @@ def to_list
self
end

def to_set
reduce(EmptySet) { |set, item| set.add(item) }
end

def inspect
to_a.inspect
end
Expand Down
4 changes: 2 additions & 2 deletions lib/hamster/set.rb
Expand Up @@ -15,7 +15,7 @@ class Set

extend Forwardable

def initialize(trie = Trie.new)
def initialize(trie = EmptyTrie)
@trie = trie
end

Expand Down Expand Up @@ -57,7 +57,7 @@ def each
def map
return self unless block_given?
return self if empty?
self.class.new(@trie.reduce(Trie.new) { |trie, entry| trie.put(yield(entry.key), nil) })
self.class.new(@trie.reduce(EmptyTrie) { |trie, entry| trie.put(yield(entry.key), nil) })
end
def_delegator :self, :map, :collect

Expand Down
4 changes: 4 additions & 0 deletions lib/hamster/stack.rb
Expand Up @@ -64,6 +64,10 @@ def to_a
end
def_delegator :self, :to_a, :entries

def to_ary
@list.to_ary
end

def to_list
@list
end
Expand Down
6 changes: 4 additions & 2 deletions lib/hamster/trie.rb
Expand Up @@ -6,7 +6,7 @@ class Trie

extend Forwardable

def initialize(significant_bits = 0, entries = [], children = [])
def initialize(significant_bits, entries = [], children = [])
@significant_bits = significant_bits
@entries = entries
@children = children
Expand Down Expand Up @@ -81,7 +81,7 @@ def get(key)

# Returns a copy of <tt>self</tt> with the given key (and associated value) deleted. If not found, returns <tt>self</tt>.
def delete(key)
find_and_delete(key) || Trie.new(@significant_bits)
find_and_delete(key) || self.class.new(@significant_bits)
end

def include?(key, value)
Expand Down Expand Up @@ -167,4 +167,6 @@ def initialize(key, value)

end

EmptyTrie = Trie.new(0)

end
2 changes: 1 addition & 1 deletion lib/hamster/version.rb
@@ -1,5 +1,5 @@
module Hamster

VERSION = "0.2.9".freeze
VERSION = "0.2.10".freeze

end
2 changes: 1 addition & 1 deletion spec/hamster/hash/find_spec.rb
Expand Up @@ -30,7 +30,7 @@
describe "with a block" do

before do
@result = @hash.send(method) { |x| x == key }
@result = @hash.send(method) { |k, v| k == key }
end

it "returns #{expected.inspect}" do
Expand Down
5 changes: 0 additions & 5 deletions spec/hamster/list/to_a_spec.rb
Expand Up @@ -37,11 +37,6 @@
@result.should == values
end

it "works with splat" do
array = *@list
array.should == values
end

it "returns a mutable array" do
@result.last.should_not == "The End"
@result << "The End"
Expand Down
5 changes: 5 additions & 0 deletions spec/hamster/list/to_ary_spec.rb
Expand Up @@ -44,6 +44,11 @@ def func(a, b, *c)
func(*@list)
end

it "works with splat" do
array = *@list
array.should == ["A", "B", "C", "D"]
end

end

end
Expand Down
33 changes: 33 additions & 0 deletions spec/hamster/list/to_set_spec.rb
@@ -0,0 +1,33 @@
require File.expand_path('../../../spec_helper', __FILE__)

require 'hamster/list'
require 'hamster/set'

describe Hamster::List do

describe "#to_set" do

[
[],
["A"],
["A", "B", "C"],
].each do |values|

describe "on #{values.inspect}" do

before do
original = Hamster.list(*values)
@result = original.to_set
end

it "returns self" do
@result.should == Hamster.set(*values)
end

end

end

end

end
44 changes: 44 additions & 0 deletions spec/hamster/stack/to_ary.rb
@@ -0,0 +1,44 @@
require File.expand_path('../../../spec_helper', __FILE__)

require 'hamster/stack'

describe Hamster::Stack do

describe "#to_ary" do

describe "enables implicit conversion to" do

before do
@stack = Hamster.stack("D", "C", "B", "A")
end

it "block parameters" do
def func(&block)
yield(@stack)
end
func do |a, b, *c|
a.should == "A"
b.should == "B"
c.should == ["C", "D"]
end
end

it "method arguments" do
def func(a, b, *c)
a.should == "A"
b.should == "B"
c.should == ["C", "D"]
end
func(*@stack)
end

it "works with splat" do
array = *@stack
array.should == ["A", "B", "C", "D"]
end

end

end

end
5 changes: 0 additions & 5 deletions spec/hamster/tuple/to_a_spec.rb
Expand Up @@ -23,11 +23,6 @@
@result.should == values
end

it "works with splat" do
array = *@tuple
array.should == values
end

it "returns a mutable array" do
@result.last.should_not == "The End"
@result << "The End"
Expand Down
5 changes: 5 additions & 0 deletions spec/hamster/tuple/to_ary_spec.rb
Expand Up @@ -32,6 +32,11 @@ def func(a, b, *c)
func(*@tuple)
end

it "works with splat" do
array = *@tuple
array.should == ["A", "B", "C", "D"]
end

end

end
Expand Down
1 change: 0 additions & 1 deletion yak-stack.txt
Expand Up @@ -10,5 +10,4 @@
* Add missing immutability tests to Stack
* Re-write Trie test-first (it was only a spike)
* Allow Trie to efficiently add multiple pairs in one go
* Implement Vector (using a Trie and integer keys?)
* Implement a queue?

0 comments on commit 9eb08d7

Please sign in to comment.