Skip to content

Commit

Permalink
Add support for consed and sliced to Sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
olabini committed Feb 27, 2010
1 parent 91799fc commit 777173d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
53 changes: 53 additions & 0 deletions src/builtin/F32_sequence.ik
Expand Up @@ -85,6 +85,8 @@ Sequence interpose = method(inbetween, Sequence Interpose create(@, inbetween
Sequence cell("%") = method(inbetween, Sequence Interpose create(@, inbetween))
Sequence interleave = method(right, Sequence Interleave create(@, right))
Sequence cell("&") = method(right, Sequence Interleave create(@, right))
Sequence consed = method(consSize 2, Sequence Cons create(@, Ground, [], consSize))
Sequence sliced = method(sliceSize 2, Sequence Slice create(@, Ground, [], sliceSize))

let(
generateNextPMethod, method(takeCurrentObject, returnObject,
Expand Down Expand Up @@ -179,6 +181,57 @@ let(
)
)

Sequence Cons = Sequence mimic do(
create = method(wrappedSequence, context, messages, +rest,
res = mimic
res wrappedSequence = wrappedSequence
res context = context
res messages = messages
res restArguments = rest
res ary = list()
res
)

next? = method(
while((ary length + 2 < restArguments[0]) && wrappedSequence next?,
ary push!(wrappedSequence next))
((ary length + 2) >= restArguments[0]) && wrappedSequence next?
)

next = method(
while(wrappedSequence next?,
if(ary length == restArguments[0],
ary shift!)
ary push!(wrappedSequence next)
if(ary length == restArguments[0],
return(ary mimic)))
)
)

Sequence Slice = Sequence mimic do(
create = method(wrappedSequence, context, messages, +rest,
res = mimic
res wrappedSequence = wrappedSequence
res context = context
res messages = messages
res restArguments = rest
res ary = list()
res
)

next? = method(wrappedSequence next? || ary length > 0)

next = method(
while(wrappedSequence next?,
ary push!(wrappedSequence next)
if(ary length == restArguments[0],
yieldAry = ary mimic
@ary = list()
return(yieldAry)))
if(ary length > 0, ary)
)
)

Sequence Index = sequenceObject(true,
result = list(@index, cell(:n))
@index = @index + @step
Expand Down
51 changes: 48 additions & 3 deletions test/sequence_spec.ik
Expand Up @@ -360,6 +360,8 @@ describe(Sequence,
ss = SequenceTester with(val: [1,2,3], len: 3) seq
ss collected(x, x*2) asList should == [2,4,6]
)

it("should be possible to destructure on the argument names")
)

describe("filtered",
Expand All @@ -381,6 +383,8 @@ describe(Sequence,
ss = SequenceTester with(val: [1,2,3,4,5,6,7,8], len: 8) seq
ss filtered(x, x>4) asList should == [5,6,7,8]
)

it("should be possible to destructure on the argument names")
)

describe("selected",
Expand All @@ -402,6 +406,8 @@ describe(Sequence,
ss = SequenceTester with(val: [1,2,3,4,5,6,7,8], len: 8) seq
ss selected(x, x>4) asList should == [5,6,7,8]
)

it("should be possible to destructure on the argument names")
)

describe("grepped",
Expand All @@ -418,6 +424,8 @@ describe(Sequence,
ss = SequenceTester with(val: [1,2,4,5,6,7,8], len: 7) seq
ss grepped(2..4) asList should == [2,4]
)

it("should be possible to destructure on the argument names")
)

describe("zipped",
Expand Down Expand Up @@ -503,6 +511,8 @@ describe(Sequence,
ss = SequenceTester with(val: [1,2,4,5,6,7,8], len: 7) seq
ss droppedWhile(x, x < 3) asList should == [4,5,6,7,8]
)

it("should be possible to destructure on the argument names")
)

describe("rejected",
Expand All @@ -521,14 +531,46 @@ describe(Sequence,
ss = SequenceTester with(val: [1,2,3,4,5,6,7,8], len: 8) seq
ss rejected(x, x>4) asList should == [1,2,3,4]
)

it("should be possible to destructure on the argument names")
)

describe("consed",
it("should have tests")
it("should create a new Sequence Cons with the arguments sent to it",
ss = SequenceTester with(val: [1,2,3,4,5,6,7,8], len: 8) seq
val = ss consed(3)
val should mimic(Sequence Cons)
val wrappedSequence should be same(ss)
val messages should == []
val restArguments should == [3]
)

it("should cons the objects",
ss = SequenceTester with(val: [1,2,4,5,6,7,8], len: 7) seq
ss consed asList should == [[1,2], [2,4], [4,5], [5,6], [6,7], [7,8]]

ss = SequenceTester with(val: [1,2,4,5,6,7,8], len: 7) seq
ss consed(3) asList should == [[1,2,4], [2,4,5], [4,5,6], [5,6,7], [6,7,8]]
)
)

describe("sliced",
it("should have tests")
it("should create a new Sequence Slice with the arguments sent to it",
ss = SequenceTester with(val: [1,2,3,4,5,6,7,8], len: 8) seq
val = ss sliced(3)
val should mimic(Sequence Slice)
val wrappedSequence should be same(ss)
val messages should == []
val restArguments should == [3]
)

it("should slice the objects",
ss = SequenceTester with(val: [1,2,4,5,6,7], len: 6) seq
ss sliced asList should == [[1,2], [4,5], [6,7]]

ss = SequenceTester with(val: [1,2,3,4,5,6,7,8,9], len: 9) seq
ss sliced(3) asList should == [[1,2,3], [4,5,6], [7,8,9]]
)
)

describe("+",
Expand Down Expand Up @@ -557,7 +599,6 @@ describe(Sequence,
)
)

; TODO add "interleave" on Enumerable too
describe("interleave",
it("should create a new sequence",
[1,2,3] seq interleave(1..5) should mimic(Sequence)
Expand Down Expand Up @@ -947,4 +988,8 @@ describe(Sequence,
ss asList should == [false, true]
)
)

describe("Cons",
it("should have tests")
)
)

0 comments on commit 777173d

Please sign in to comment.