Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for destructuring on fold

  • Loading branch information...
commit 3a5b7a71c6abfcb6b918ec1947477d01577c1797 1 parent 06fd83b
@olabini olabini authored
Showing with 261 additions and 7 deletions.
  1. +6 −4 src/builtin/F30_enumerable.ik
  2. +255 −3 test/enumerable_spec.ik
View
10 src/builtin/F30_enumerable.ik
@@ -386,20 +386,22 @@ Mixins Enumerable inject = dmacro(
[sumArgName, argName, theCode]
- lexicalCode = LexicalBlock createFrom(list(sumArgName, argName, theCode), call ground)
+ destructor = Destructor from(argName)
+ lexicalCode = LexicalBlock createFrom(list(sumArgName) + destructor argNames + list(theCode), call ground)
sum = nil
self each(i, n,
if(i == 0,
sum = cell(:n),
- sum = lexicalCode call(cell(:sum), cell(:n))))
+ sum = lexicalCode call(cell(:sum), *(destructor unpack(cell(:n))))))
return(cell(:sum)),
[>sum, sumArgName, argName, theCode]
- lexicalCode = LexicalBlock createFrom(list(sumArgName, argName, theCode), call ground)
+ destructor = Destructor from(argName)
+ lexicalCode = LexicalBlock createFrom(list(sumArgName) + destructor argNames + list(theCode), call ground)
self each(n,
- sum = lexicalCode call(cell(:sum), cell(:n)))
+ sum = lexicalCode call(cell(:sum), *(destructor unpack(cell(:n)))))
return(cell(:sum)))
Mixins Enumerable flatMap = macro(
View
258 test/enumerable_spec.ik
@@ -888,7 +888,91 @@ describe(Mixins,
CustomEnumerable2 inject(100, sum, x, sum - x) should == 25
)
- it("should be possible to destructure on the argument name")
+ it("should be able to destructure on the argument name",
+ result = []
+ [[1,2], [2,3], [4,5]] inject(fluxie, (x,y), result << [x+1, y-1]. nil)
+ result should == [[3,2], [5,4]]
+ )
+
+ it("should be able to destructure and ignore the rest of something",
+ result = []
+ [[1,2,9,10], [2,3,11,12], [4,5,13,14]] inject(fluxie, (x,y,_), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[2,3], [4,5]]
+ )
+
+ it("should be able to destructure and ignore in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9], [2,3,11], [4,5,13]] inject(fluxie, (x,_,y), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[2,11], [4,13]]
+ )
+
+ it("should be able to destructure and ignore several times in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9,10,11], [2,3,11,12,13], [4,5,13,14,15]] inject(fluxie, (x,_,y,_,q), cell?(:"_") should not be true. result << [x, y, q]. nil)
+ result should == [[2,11,13], [4,13,15]]
+ )
+
+ it("should be able to destructure recursively",
+ result = []
+ [[[:x, :y, :z], [:q, :r, :p]], [[:b, :c, :d], [:i, :j, :k]], [[:i, :j, :k], [:i2, :j3, :k4]]] inject(fluxie,
+ (v, (v2, _, v3)), cell?(:"_") should be false. result << [v, v2, v3]. nil)
+ result should == [[[:b, :c, :d], :i, :k], [[:i, :j, :k], :i2, :k4]]
+ )
+
+ it("should report a destructuring match error if destructuring doesn't add upp",
+ fn([[1,2], [3,4], [4,5]] inject(fluxie, (q,p,r), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] inject(fluxie, (q), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] inject(fluxie, (q,_,r), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should report a destructuring match error if recursive destructuring doesn't add upp",
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] inject(fluxie, (q,(p)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] inject(fluxie, (q,(p,r,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] inject(fluxie, (q,(p,_,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should be able to destructure on the argument name",
+ result = []
+ [[1,2], [2,3], [4,5]] inject(nil, fluxie, (x,y), result << [x+1, y-1]. nil)
+ result should == [[2,1], [3,2], [5,4]]
+ )
+
+ it("should be able to destructure and ignore the rest of something",
+ result = []
+ [[1,2,9,10], [2,3,11,12], [4,5,13,14]] inject(nil, fluxie, (x,y,_), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[1,2], [2,3], [4,5]]
+ )
+
+ it("should be able to destructure and ignore in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9], [2,3,11], [4,5,13]] inject(nil, fluxie, (x,_,y), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[1,9], [2,11], [4,13]]
+ )
+
+ it("should be able to destructure and ignore several times in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9,10,11], [2,3,11,12,13], [4,5,13,14,15]] inject(nil, fluxie, (x,_,y,_,q), cell?(:"_") should not be true. result << [x, y, q]. nil)
+ result should == [[1,9,11], [2,11,13], [4,13,15]]
+ )
+
+ it("should be able to destructure recursively",
+ result = []
+ [[[:x, :y, :z], [:q, :r, :p]], [[:b, :c, :d], [:i, :j, :k]], [[:i, :j, :k], [:i2, :j3, :k4]]] inject(nil, fluxie,
+ (v, (v2, _, v3)), cell?(:"_") should be false. result << [v, v2, v3]. nil)
+ result should == [[[:x, :y, :z], :q, :p], [[:b, :c, :d], :i, :k], [[:i, :j, :k], :i2, :k4]]
+ )
+
+ it("should report a destructuring match error if destructuring doesn't add upp",
+ fn([[1,2], [3,4], [4,5]] inject(nil, fluxie, (q,p,r), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] inject(nil, fluxie, (q), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] inject(nil, fluxie, (q,_,r), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should report a destructuring match error if recursive destructuring doesn't add upp",
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] inject(nil, fluxie, (q,(p)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] inject(nil, fluxie, (q,(p,r,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] inject(nil, fluxie, (q,(p,_,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ )
)
describe("reduce",
@@ -924,7 +1008,91 @@ describe(Mixins,
CustomEnumerable2 reduce(100, sum, x, sum - x) should == 25
)
- it("should be possible to destructure on the argument name")
+ it("should be able to destructure on the argument name",
+ result = []
+ [[1,2], [2,3], [4,5]] reduce(fluxie, (x,y), result << [x+1, y-1]. nil)
+ result should == [[3,2], [5,4]]
+ )
+
+ it("should be able to destructure and ignore the rest of something",
+ result = []
+ [[1,2,9,10], [2,3,11,12], [4,5,13,14]] reduce(fluxie, (x,y,_), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[2,3], [4,5]]
+ )
+
+ it("should be able to destructure and ignore in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9], [2,3,11], [4,5,13]] reduce(fluxie, (x,_,y), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[2,11], [4,13]]
+ )
+
+ it("should be able to destructure and ignore several times in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9,10,11], [2,3,11,12,13], [4,5,13,14,15]] reduce(fluxie, (x,_,y,_,q), cell?(:"_") should not be true. result << [x, y, q]. nil)
+ result should == [[2,11,13], [4,13,15]]
+ )
+
+ it("should be able to destructure recursively",
+ result = []
+ [[[:x, :y, :z], [:q, :r, :p]], [[:b, :c, :d], [:i, :j, :k]], [[:i, :j, :k], [:i2, :j3, :k4]]] reduce(fluxie,
+ (v, (v2, _, v3)), cell?(:"_") should be false. result << [v, v2, v3]. nil)
+ result should == [[[:b, :c, :d], :i, :k], [[:i, :j, :k], :i2, :k4]]
+ )
+
+ it("should report a destructuring match error if destructuring doesn't add upp",
+ fn([[1,2], [3,4], [4,5]] reduce(fluxie, (q,p,r), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] reduce(fluxie, (q), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] reduce(fluxie, (q,_,r), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should report a destructuring match error if recursive destructuring doesn't add upp",
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] reduce(fluxie, (q,(p)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] reduce(fluxie, (q,(p,r,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] reduce(fluxie, (q,(p,_,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should be able to destructure on the argument name",
+ result = []
+ [[1,2], [2,3], [4,5]] reduce(nil, fluxie, (x,y), result << [x+1, y-1]. nil)
+ result should == [[2,1], [3,2], [5,4]]
+ )
+
+ it("should be able to destructure and ignore the rest of something",
+ result = []
+ [[1,2,9,10], [2,3,11,12], [4,5,13,14]] reduce(nil, fluxie, (x,y,_), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[1,2], [2,3], [4,5]]
+ )
+
+ it("should be able to destructure and ignore in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9], [2,3,11], [4,5,13]] reduce(nil, fluxie, (x,_,y), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[1,9], [2,11], [4,13]]
+ )
+
+ it("should be able to destructure and ignore several times in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9,10,11], [2,3,11,12,13], [4,5,13,14,15]] reduce(nil, fluxie, (x,_,y,_,q), cell?(:"_") should not be true. result << [x, y, q]. nil)
+ result should == [[1,9,11], [2,11,13], [4,13,15]]
+ )
+
+ it("should be able to destructure recursively",
+ result = []
+ [[[:x, :y, :z], [:q, :r, :p]], [[:b, :c, :d], [:i, :j, :k]], [[:i, :j, :k], [:i2, :j3, :k4]]] reduce(nil, fluxie,
+ (v, (v2, _, v3)), cell?(:"_") should be false. result << [v, v2, v3]. nil)
+ result should == [[[:x, :y, :z], :q, :p], [[:b, :c, :d], :i, :k], [[:i, :j, :k], :i2, :k4]]
+ )
+
+ it("should report a destructuring match error if destructuring doesn't add upp",
+ fn([[1,2], [3,4], [4,5]] reduce(nil, fluxie, (q,p,r), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] reduce(nil, fluxie, (q), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] reduce(nil, fluxie, (q,_,r), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should report a destructuring match error if recursive destructuring doesn't add upp",
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] reduce(nil, fluxie, (q,(p)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] reduce(nil, fluxie, (q,(p,r,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] reduce(nil, fluxie, (q,(p,_,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ )
)
describe("fold",
@@ -960,7 +1128,91 @@ describe(Mixins,
CustomEnumerable2 fold(100, sum, x, sum - x) should == 25
)
- it("should be possible to destructure on the argument name")
+ it("should be able to destructure on the argument name",
+ result = []
+ [[1,2], [2,3], [4,5]] fold(fluxie, (x,y), result << [x+1, y-1]. nil)
+ result should == [[3,2], [5,4]]
+ )
+
+ it("should be able to destructure and ignore the rest of something",
+ result = []
+ [[1,2,9,10], [2,3,11,12], [4,5,13,14]] fold(fluxie, (x,y,_), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[2,3], [4,5]]
+ )
+
+ it("should be able to destructure and ignore in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9], [2,3,11], [4,5,13]] fold(fluxie, (x,_,y), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[2,11], [4,13]]
+ )
+
+ it("should be able to destructure and ignore several times in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9,10,11], [2,3,11,12,13], [4,5,13,14,15]] fold(fluxie, (x,_,y,_,q), cell?(:"_") should not be true. result << [x, y, q]. nil)
+ result should == [[2,11,13], [4,13,15]]
+ )
+
+ it("should be able to destructure recursively",
+ result = []
+ [[[:x, :y, :z], [:q, :r, :p]], [[:b, :c, :d], [:i, :j, :k]], [[:i, :j, :k], [:i2, :j3, :k4]]] fold(fluxie,
+ (v, (v2, _, v3)), cell?(:"_") should be false. result << [v, v2, v3]. nil)
+ result should == [[[:b, :c, :d], :i, :k], [[:i, :j, :k], :i2, :k4]]
+ )
+
+ it("should report a destructuring match error if destructuring doesn't add upp",
+ fn([[1,2], [3,4], [4,5]] fold(fluxie, (q,p,r), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] fold(fluxie, (q), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] fold(fluxie, (q,_,r), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should report a destructuring match error if recursive destructuring doesn't add upp",
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] fold(fluxie, (q,(p)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] fold(fluxie, (q,(p,r,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] fold(fluxie, (q,(p,_,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should be able to destructure on the argument name",
+ result = []
+ [[1,2], [2,3], [4,5]] fold(nil, fluxie, (x,y), result << [x+1, y-1]. nil)
+ result should == [[2,1], [3,2], [5,4]]
+ )
+
+ it("should be able to destructure and ignore the rest of something",
+ result = []
+ [[1,2,9,10], [2,3,11,12], [4,5,13,14]] fold(nil, fluxie, (x,y,_), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[1,2], [2,3], [4,5]]
+ )
+
+ it("should be able to destructure and ignore in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9], [2,3,11], [4,5,13]] fold(nil, fluxie, (x,_,y), cell?(:"_") should not be true. result << [x, y]. nil)
+ result should == [[1,9], [2,11], [4,13]]
+ )
+
+ it("should be able to destructure and ignore several times in the middle of the pattern without binding anything",
+ result = []
+ [[1,2,9,10,11], [2,3,11,12,13], [4,5,13,14,15]] fold(nil, fluxie, (x,_,y,_,q), cell?(:"_") should not be true. result << [x, y, q]. nil)
+ result should == [[1,9,11], [2,11,13], [4,13,15]]
+ )
+
+ it("should be able to destructure recursively",
+ result = []
+ [[[:x, :y, :z], [:q, :r, :p]], [[:b, :c, :d], [:i, :j, :k]], [[:i, :j, :k], [:i2, :j3, :k4]]] fold(nil, fluxie,
+ (v, (v2, _, v3)), cell?(:"_") should be false. result << [v, v2, v3]. nil)
+ result should == [[[:x, :y, :z], :q, :p], [[:b, :c, :d], :i, :k], [[:i, :j, :k], :i2, :k4]]
+ )
+
+ it("should report a destructuring match error if destructuring doesn't add upp",
+ fn([[1,2], [3,4], [4,5]] fold(nil, fluxie, (q,p,r), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] fold(nil, fluxie, (q), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[1,2], [3,4], [4,5]] fold(nil, fluxie, (q,_,r), nil)) should signal(Condition Error DestructuringMismatch)
+ )
+
+ it("should report a destructuring match error if recursive destructuring doesn't add upp",
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] fold(nil, fluxie, (q,(p)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] fold(nil, fluxie, (q,(p,r,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ fn([[[1,2],[1,2]], [[3,4],[1,2]], [[1,2],[4,5]]] fold(nil, fluxie, (q,(p,_,f)), nil)) should signal(Condition Error DestructuringMismatch)
+ )
)
describe("flatMap",
Please sign in to comment.
Something went wrong with that request. Please try again.