Skip to content

Commit

Permalink
Merge pull request #6 from vinniegarcia/develop
Browse files Browse the repository at this point in the history
compose and sequence as applications of accumulate and wrap
  • Loading branch information
vinniegarcia committed Sep 7, 2015
2 parents d0d387b + 07bbb83 commit 8853665
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hoff",
"version": "1.2.0",
"version": "1.3.1",
"description": "A collection of higher order functions you may find useful",
"main": "dist/index.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/index.es6
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

import 'babel/polyfill';

export accumulate from './lib/accumulate';
export compose from './lib/compose';
export curry from './lib/curry';
Expand All @@ -8,4 +10,4 @@ export lift from './lib/lift';
export not from './lib/not';
export partial from './lib/partial';
export partition from './lib/partition';
export sequence from './lib/sequence';
export sequence from './lib/sequence';
5 changes: 3 additions & 2 deletions src/lib/accumulate.es6
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const accumulate = (fn, iv) => (...args) => args.reduce(fn, iv);
const accumulate = (reducer, iv) => (...args) => args.reduce(reducer, iv);
export const reverse = (reducer, iv) => (...args) => args.reduceRight(reducer, iv);

export default accumulate;
export default accumulate;
7 changes: 5 additions & 2 deletions src/lib/compose.es6
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// function composition
// compose(f, g)(x) = g(f(x))
'use strict';
import wrap from './wrap';
import unwrap from './unwrap';
import identity from './identity';
import {reverse} from './accumulate';

const compose = (...funcs) => (...args) => unwrap(funcs.reduceRight((current, next) => [next(...current)], args));
const compose = (...funcs) => (...args) => unwrap(reverse(wrap, args)(...funcs));

export default compose;
export default compose;
2 changes: 1 addition & 1 deletion src/lib/lift.es6
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import identity from './identity';

const lift = (fn, lifter = identity) => (...args) => fn(...args.map(lifter));

export default lift;
export default lift;
7 changes: 5 additions & 2 deletions src/lib/sequence.es6
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// function sequencing
// compose(f, g)(x) = f(g(x))
'use strict';
import wrap from './wrap';
import unwrap from './unwrap';
import identity from './identity';
import accumulate from './accumulate';

const sequence = (...funcs) => (...args) => unwrap(funcs.reduce((current, next) => [next(...current)], args));
const sequence = (...funcs) => (...args) => unwrap(accumulate(wrap, args)(...funcs));

export default sequence;
export default sequence;
2 changes: 1 addition & 1 deletion src/lib/unwrap.es6
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
// return first element of an array
// without any array packing
const unwrap = ([first,]) => first;
const unwrap = ([first,...rest]) => first;

export default unwrap;
2 changes: 1 addition & 1 deletion src/lib/wrap.es6
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const wrap = (fn, n) => fn(n);
const wrap = (n, fn) => [fn(...n)];

export default wrap;
2 changes: 2 additions & 0 deletions src/test/compose.es6
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

import test from 'tape';
import compose from '../lib/compose';
import {add, dub, sq} from './util/simpleFunctions';
Expand Down
7 changes: 4 additions & 3 deletions src/test/wrap.es6
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import test from 'tape';
import wrap from '../lib/wrap';
import unwrap from '../lib/unwrap';
import partial from '../lib/partial';
import {add} from './util/simpleFunctions';

Expand All @@ -11,11 +12,11 @@ test('Wrap test', (t) => {
const a = '3',
b = '4',
addStrings = add(a,b),
numify = partial(wrap, Number),
addNums = add(numify(a), numify(b));
numify = (n) => wrap(n, Number),
addNums = add(unwrap(numify(a)), unwrap(numify(b)));

t.is(addStrings, '34', 'strings were added');
t.is(numify(a), 3, 'wrap converted to Number');
t.same(numify(a), [3], 'wrap converted to Array<Number>');
t.is(addNums, 7, 'conversion was successful');

});

0 comments on commit 8853665

Please sign in to comment.