Skip to content

Commit

Permalink
reduce with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Mar 25, 2012
1 parent b07d032 commit e27328b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/ajlogo.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,24 @@

return [];
});

topcontext.setProcedure('reduce', function(name, list) {
var proc = this.getProcedure(name);
var isproc = (proc.process != null);
var result = list[0];

for (var k = 1; k < list.length; k++)
{
var y = list[k];

if (isproc)
result = proc.process(this, [result, y]);
else
result = proc.apply(this, [result, y]);
}

return result;
});

topcontext.setProcedure('thing', function(name) {
return this.getVariable(name);
Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ assert.ok(ctx.getProcedure('ascii'));
assert.ok(ctx.getProcedure('char'));
assert.ok(ctx.getProcedure('filter'));
assert.ok(ctx.getProcedure('find'));
assert.ok(ctx.getProcedure('reduce'));

result = ajlogo.compileText('make "three 3');
(new ajlogo.CompositeExpression(result)).evaluate(ctx);
Expand Down Expand Up @@ -649,3 +650,7 @@ result = ajlogo.evaluateText('to oddp :x output remainder :x 2 end find "oddp [2
assert.ok(result instanceof Array);
assert.equal(0, result.length);

// reduce

assert.equal(10, ajlogo.evaluateText('reduce "sum [1 2 3 4]'));
assert.equal(10, ajlogo.evaluateText('to mysum :x :y output sum :x :y end reduce "mysum [1 2 3 4]'));

0 comments on commit e27328b

Please sign in to comment.