Skip to content

Commit

Permalink
feat(ch10): compose functions with a simple reducer, still right to left
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Jan 25, 2020
1 parent 686e507 commit 3489341
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import {add, addN, compose, filterRed, map, mapRedu} from "./10.1_abstraction_and_composition";
import {add, addN, compose, composeRight, filterRed, map, mapRedu} from "./10.1_abstraction_and_composition";

describe("Abstraction and Composition", () => {
it("should fix a parameter in a function", () => {
Expand Down Expand Up @@ -57,4 +57,15 @@ describe("Abstraction and Composition", () => {
expect(composedFn(5)).to.equal(21);
});

it("should compose functions with a simple reducer, still right to left", () => {

const add1 = x => x + 1;
const add5 = x => x + 5;
const multiplyWith3 = x => x * 3;

const composedFn = composeRight(add1, add5, multiplyWith3);

expect(composedFn(5)).to.equal(21);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export const filterRed = (fn, arr) => arr.reduce((acc, item) => {
}, []);

export const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);

export const composeRight = (...fns) => x => fns.reverse().reduce((y, f) => f(y), x);

0 comments on commit 3489341

Please sign in to comment.