Functional Programming With Javascript: Pipe, Go

Pipe

test('pipe', () => {  const squareOnlyOdds = pipe(    (collection) => filter(collection, value => value % 2),    (collection) => map(collection, value => value * value),  );  expect(squareOnlyOdds([1, 2, 3, 4, 5, 6])).toStrictEqual([1, 9, 25]);});
// functions/pipe/index.jsconst reduce = require('../reduce');
function pipe(...fns) { return (collection) => reduce(fns, (prevResult, fn) => fn(prevResult), collection);}module.exports = pipe;

Go

test('go', () => {  const squaredOdds = go(    [1, 2, 3, 4, 5, 6],    (collection) => filter(collection, value => value % 2),    (collection) => map(collection, value => value * value),  );  expect(squaredOdds).toStrictEqual([1, 9, 25]);});
const pipe = require('../pipe');function go(collection, ...fns) {  return pipe(...fns)(collection);}module.exports = go;

Use curring

test('pipe', () => {  const squareOnlyOdds = pipe(    filter(value => value % 2),    map(value => value * value),  );  expect(squareOnlyOdds([1, 2, 3, 4, 5, 6])).toStrictEqual([1, 9, 25]);});test('go', () => {  const squaredOdds = go(    [1, 2, 3, 4, 5, 6],    filter(value => value % 2),    map(value => value * value),  );  expect(squaredOdds).toStrictEqual([1, 9, 25]);});

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store