Skip to content

Commit 16ff591

Browse files
committed
Added reduce function
1 parent a8773a7 commit 16ff591

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

specs/lisp-spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ describe('lispjs', function () {
230230
assert.deepEqual(lisp.run(prog), [1, 3]);
231231
});
232232

233+
it('supports reduce', function () {
234+
var prog = [
235+
// Define some collection
236+
['define', 'coll', ['list', 1, 2, 3]],
237+
238+
// Sum numbers
239+
['reduce', 'coll', '+', 0]
240+
];
241+
242+
assert.equal(lisp.run(prog), 6);
243+
});
244+
233245
it('supports the Y-combinator', function () {
234246
var prog = [
235247
// Define the Y-combinator

src/env.js

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ var buildEnv = [
112112
['cons', ['car', 'coll'], ['filter', 'pred', ['cdr', 'coll']]],
113113
['filter', 'pred', ['cdr', 'coll']]]]],
114114

115+
// Reduce a collection
116+
['defun', 'reduce', ['coll', 'f', 'acc'],
117+
['if', ['empty', 'coll'],
118+
'acc',
119+
['reduce', ['cdr', 'coll'], 'f', ['f', 'acc', ['car', 'coll']]]]],
120+
115121
// Create a lexical scope with bound arguments and execute body in context
116122
['defmacro', 'let', ['bindings', 'body'],
117123
['concat',

0 commit comments

Comments
 (0)