Skip to content

Commit

Permalink
List#infold_up and List#infold_down
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaguery committed Feb 16, 2012
1 parent 32cc897 commit 9a25722
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 4 deletions.
36 changes: 32 additions & 4 deletions lib/list_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,41 @@ def to_s
size < @@result_size_limit ? List.new(results) : Error.new("OVERSIZE")
end
end




duck_handle :infold_down do
@contents.reverse.inject do |memo, item|
case
when memo.nil?
item
when memo.kind_of?(Array)
memo.collect {|branch| item.grab(branch)}.flatten.compact
else
item.grab(memo)
end
end
end


duck_handle :infold_up do
@contents.inject do |memo, item|
case
when memo.nil?
item
when memo.kind_of?(Array)
memo.collect {|branch| item.grab(branch)}.flatten.compact
else
item.grab(memo)
end
end
end


duck_handle :length do
Int.new(@contents.length)
end


duck_handle :map do
Closure.new(["be"],"map(#{self.inspect}, ?)") do |item|
new_contents = @contents.collect {|i| i.deep_copy}
Expand Down
48 changes: 48 additions & 0 deletions spec/lists/messages/infold_down_message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#encoding: utf-8
require_relative '../../spec_helper'

describe "List" do
describe "the :infold_down message for Lists" do
it "should be something Lists recognize" do
List.recognized_messages.should include(:infold_down)
end

it "should produce the result of each item BEING GRABBED BY the next from RIGHT to LEFT" do
bunch = list([int(2), int(3), int(4)])
bunch.infold_down.should be_a_kind_of(Int)
bunch.infold_down.inspect.should == "2"
end

it "should work for empty Lists, returning nil" do
list.infold_down.should == nil
end

it "should do all the grabbing stuff" do
do_math_later = list( [message("+"), message("neg"), int(2)] )
interpreter(contents:[do_math_later]).inspect.should == "[(:+, :neg, 2) :: :: «»]"
interpreter(contents:[do_math_later], script:"infold_down").run.inspect.should ==
"[λ(-2 + ?,[\"neg\"]) :: :: «»]"
end

it "should work even when things don't technically #grab one another, since that's how grab works" do
interpreter(contents:[list([int(9)]*7)], script:"infold_down").run.inspect.should == "[9 :: :: «»]"
end

it "should work when an intermediate result returns an Array" do
cause_havoc = list([
message("shatter"),
list( [message("*"), message("foo"),message("trunc")] )
])
interpreter(contents:[cause_havoc]).inspect.should == "[(:shatter, (:*, :foo, :trunc)) :: :: «»]"
interpreter(contents:[cause_havoc], script:"infold_down").run.inspect.should ==
"[:*, :foo, :trunc :: :: «»]"
end

it "should work when an intermediate result returns nil" do
cause_havoc = list( [message("zap"), message("foo"), int(4)])
interpreter(contents:[cause_havoc]).inspect.should == "[(:zap, :foo, 4) :: :: «»]"
interpreter(contents:[cause_havoc], script:"infold_down").run.inspect.should == "[:: :: «»]"
end

end
end
49 changes: 49 additions & 0 deletions spec/lists/messages/infold_up_message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#encoding: utf-8
require_relative '../../spec_helper'

describe "List" do
describe "the :infold_up message for Lists" do
it "should be something Lists recognize" do
List.recognized_messages.should include(:infold_up)
end

it "should produce the result of each item BEING GRABBED BY the next from RIGHT to LEFT" do
bunch = list([int(2), int(3), int(4)])
bunch.infold_up.should be_a_kind_of(Int)
bunch.infold_up.inspect.should == "4"
end

it "should work for empty Lists, returning nil" do
list.infold_up.should == nil
end

it "should do all the grabbing stuff" do
do_math_later = list( [int(2), message("+") ] )
interpreter(contents:[do_math_later]).inspect.should == "[(2, :+) :: :: «»]"
interpreter(contents:[do_math_later], script:"infold_up").run.inspect.should ==
"[λ(2 + ?,[\"neg\"]) :: :: «»]"
end

it "should work even when things don't technically #grab one another, since that's how grab works" do
interpreter(contents:[list([int(9)]*7)], script:"infold_up").run.inspect.should == "[9 :: :: «»]"
end

it "should work when an intermediate result returns an Array" do
cause_havoc = list([
list( [message("*"), int(99), message("trunc")] ),
message("shatter"),
message("neg")]
)
interpreter(contents:[cause_havoc]).inspect.should == "[((:*, 99, :trunc), :shatter, :neg) :: :: «»]"
interpreter(contents:[cause_havoc], script:"infold_up").run.inspect.should ==
"[-99 :: :: «»]"
end

it "should work when an intermediate result returns nil" do
cause_havoc = list( [ message("foo"), message("zap"), int(4)])
interpreter(contents:[cause_havoc]).inspect.should == "[(:foo, :zap, 4) :: :: «»]"
interpreter(contents:[cause_havoc], script:"infold_up").run.inspect.should == "[4 :: :: «»]"
end

end
end

0 comments on commit 9a25722

Please sign in to comment.