Skip to content

Commit

Permalink
Implement Vector#map.
Browse files Browse the repository at this point in the history
  • Loading branch information
harukizaemon committed Jan 13, 2011
1 parent 6b1980a commit 2985a65
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/hamster/vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def each(&block)
nil
end

def map(&block)
return self unless block_given?
reduce(EmptyVector) { |vector, item| vector.add(yield(item)) }
end
def_delegator :self, :map, :collect

def filter
return self unless block_given?
reduce(EmptyVector) { |vector, item| yield(item) ? vector.add(item) : vector }
Expand Down
2 changes: 1 addition & 1 deletion spec/hamster/list/grep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

before do
@original = Hamster.list(*values)
@result = @original.grep(String) { |item| item.downcase }
@result = @original.grep(String, &:downcase)
end

it "preserves the original" do
Expand Down
2 changes: 1 addition & 1 deletion spec/hamster/list/map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
describe "with a block" do

before do
@result = @original.send(method) { |item| item.downcase }
@result = @original.send(method, &:downcase)
end

it "preserves the original" do
Expand Down
2 changes: 1 addition & 1 deletion spec/hamster/set/grep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
end

it "returns #{expected.inspect}" do
@list.grep(String) { |item| item.downcase }.should == Hamster.set(*expected)
@list.grep(String, &:downcase).should == Hamster.set(*expected)
end


Expand Down
2 changes: 1 addition & 1 deletion spec/hamster/set/map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
describe "with a block" do

before do
@mapped = @original.send(method) { |item| item.downcase }
@mapped = @original.send(method, &:downcase)
end

it "preserves the original values" do
Expand Down
64 changes: 64 additions & 0 deletions spec/hamster/vector/map_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'

require 'hamster/vector'

describe Hamster::Vector do

[:map, :collect].each do |method|

describe "##{method}" do

describe "when empty" do

before do
@original = Hamster.vector
@mapped = @original.send(method) {}
end

it "returns self" do
@mapped.should equal(@original)
end

end

describe "when not empty" do

before do
@original = Hamster.vector("A", "B", "C")
end

describe "with a block" do

before do
@mapped = @original.send(method, &:downcase)
end

it "preserves the original values" do
@original.should == Hamster.vector("A", "B", "C")
end

it "returns a new vector with the mapped values" do
@mapped.should == Hamster.vector("a", "b", "c")
end

end

describe "with no block" do

before do
@result = @original.send(method)
end

it "returns self" do
@result.should equal(@original)
end

end

end

end

end

end

0 comments on commit 2985a65

Please sign in to comment.