Skip to content

Commit

Permalink
fix(reduce-function): make reduce actually usable
Browse files Browse the repository at this point in the history
Reduce has to take a curried function so that it can be easily used
  • Loading branch information
kantord committed Oct 22, 2018
1 parent c370308 commit 02672e0
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
88 changes: 88 additions & 0 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ exports[`interpreter correct target code null 1`] = `"(function(_) { return (fun

exports[`interpreter correct target code null: true 1`] = `"(function(_) { return (function(input) { return [null,true]})})"`;

exports[`interpreter correct target code reduce ($a => $b => $a + $b): 0 1`] = `"(function(_) { return (function(input) { return _.reduce([((function(a) {_ = _.assign('a', a, _); return (function(b) {_ = _.assign('b', b, _); return _.get('a')+_.get('b')})})),0])(input)})})"`;

exports[`interpreter correct target code sortBy \\.age | map \\.name 1`] = `"(function(_) { return (function(input) { return (function (input) {return _.map((function(input) {return input.name}))(input)})(_.sortBy((function(input) {return input.age}))(input))})})"`;

exports[`interpreter correct target code true 1`] = `"(function(_) { return (function(input) { return true})})"`;
Expand Down Expand Up @@ -4541,6 +4543,92 @@ Object {
}
`;

exports[`interpreter correct target tree reduce ($a => $b => $a + $b): 0 1`] = `
Object {
"status": true,
"value": Object {
"name": "functionCall",
"value": Object {
"left": Object {
"name": "identifier",
"value": "reduce",
},
"right": Object {
"name": "tuple",
"value": Array [
Object {
"name": "parentheses",
"value": Object {
"end": Object {
"column": 28,
"line": 1,
"offset": 27,
},
"name": "lambda",
"start": Object {
"column": 9,
"line": 1,
"offset": 8,
},
"value": Object {
"definition": Object {
"end": Object {
"column": 28,
"line": 1,
"offset": 27,
},
"name": "lambda",
"start": Object {
"column": 15,
"line": 1,
"offset": 14,
},
"value": Object {
"definition": Object {
"name": "binaryOperation",
"value": Array [
Object {
"name": "variable",
"value": "$a",
},
Object {
"end": Object {
"column": 25,
"line": 1,
"offset": 24,
},
"name": "primitive",
"start": Object {
"column": 24,
"line": 1,
"offset": 23,
},
"value": "+",
},
Object {
"name": "variable",
"value": "$b",
},
],
},
"variable": "b",
},
},
"variable": "a",
},
},
},
Object {
"name": "primitive",
"value": "0",
},
],
},
},
},
}
`;

exports[`interpreter correct target tree sortBy \\.age | map \\.name 1`] = `
Object {
"status": true,
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/builtins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('built ins', () => {

describe('reduce', () => {
it('returns correct value', () => {
const x = (a, b) => a * b + 1; // eslint-disable-line
const x = a => b => a * b + 1; // eslint-disable-line
expect(reduce([x, 4])([1, 2])).toEqual(11)
})
})
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ const tests = [
sourceCode: `map $foo => [$foo]`,
input: [1, 2],
output: [[1], [2]]
},
{
sourceCode: `reduce ($a => $b => $a + $b): 0`,
input: [1, 2, -1, 3],
output: 5
}
]

Expand Down
7 changes: 4 additions & 3 deletions src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ export default {

reverse: (input: Array<mixed>): Array<mixed> => input.slice().reverse(),

reduce: ([f, x]: [(mixed) => mixed, mixed]): ((Array<mixed>) => mixed) => (
input: Array<mixed>
): mixed => input.reduce(f, x),
reduce: ([f, x]: [(mixed) => mixed => mixed, mixed]): ((
Array<mixed>,
) => mixed) => (input: Array<mixed>): mixed =>
input.reduce((a: mixed, b: mixed): mixed => f(a)(b), x),

length: (input: Array<mixed>): number => input.length,

Expand Down

0 comments on commit 02672e0

Please sign in to comment.