Skip to content

Commit

Permalink
feat: add reduce function
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Oct 19, 2018
1 parent 2649463 commit d70ae16
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/__tests__/builtins.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import builtIns from '../builtins'

const { projection, join, map, sortBy, filter, get, assign, reverse } = builtIns
const {
projection,
join,
map,
sortBy,
filter,
get,
assign,
reverse,
reduce
} = builtIns

describe('built ins', () => {
describe('projection', () => {
Expand Down Expand Up @@ -46,6 +56,13 @@ describe('built ins', () => {
})
})

describe('reduce', () => {
it('returns correct value', () => {
const x = (a, b) => a * b + 1; // eslint-disable-line
expect(reduce([x, 4])([1, 2])).toEqual(11)
})
})

describe('sortBy', () => {
it('returns correct value', () => {
const id = a => a; // eslint-disable-line
Expand Down
5 changes: 4 additions & 1 deletion src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ export default {
): {[string]: mixed} {
return Object.assign({}, context, { [variable]: value })
},
reverse: (input: Array<mixed>): Array<mixed> => input.slice().reverse()
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)
}

0 comments on commit d70ae16

Please sign in to comment.