Skip to content

Commit 88d6f2a

Browse files
committed
Add pipe() solution
1 parent 9ee6f60 commit 88d6f2a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

source/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ export const trace = label => value => {
1414
};
1515

1616
export const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
17+
18+
export const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x);

source/test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { inc } from './index';
77
import { compose2 } from './index';
88
import { trace } from './index';
99
import { compose } from './index';
10+
import { pipe } from './index';
1011

1112
// What is currying?
1213
// add2(a) => b => Number
@@ -77,7 +78,7 @@ test('trace(label) => v => v, effects(log to console)', assert => {
7778

7879
// How does currying help with function composition?
7980
// compose(...fns: [...Functions]) => Function
80-
test('compose(...fns: [...Functions]) => Function', assert => {
81+
test('compose(...fns: [...Function]) => Function', assert => {
8182
const msg = 'should take any number of functions and return their composition';
8283

8384
const g = n => n + 1;
@@ -96,3 +97,21 @@ test('compose(...fns: [...Functions]) => Function', assert => {
9697
assert.same(actual, expected, msg);
9798
assert.end();
9899
});
100+
101+
test('pipe(...fns: [...Function]) => Function', assert => {
102+
const msg = 'should take any number of functions and return their composition';
103+
104+
const g = n => n + 1;
105+
const f = n => n * 2;
106+
107+
const h = pipe(
108+
f,
109+
g
110+
);
111+
112+
const actual = h(20);
113+
const expected = 41;
114+
115+
assert.same(actual, expected, msg);
116+
assert.end();
117+
});

0 commit comments

Comments
 (0)