Skip to content

Commit

Permalink
feat(ch09): add inverse trace and flip
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Jan 13, 2020
1 parent ca602ba commit d9aeafe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export const trace = label => value => {
return value;
};

export const traceInv = value => label => {
console.log(`${label}: ${value}`);
return value;
};

export const pipe = (...fns) => x => fns.reduce((acum, f) => f(acum), x);

export const map = fn => mappable => mappable.map(fn);

export const flip = fn => a => b => fn(b)(a);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assert, expect} from 'chai';
import {compose, map, pipe, sum, trace} from "./9.1_curry-and-fn-composition";
import {compose, flip, map, pipe, sum, trace, traceInv} from "./9.1_curry-and-fn-composition";
import {spy} from "sinon";

describe("Curry and Function Composition", () => {
Expand Down Expand Up @@ -92,5 +92,23 @@ describe("Curry and Function Composition", () => {
const doubled = doubleAll(array);

expect(doubled).to.deep.equal([2, 4, 6, 8, 10]);
})
});

it("should flip the order of two parameters in an inverse trace function", () => {
const f = a => a * 2;
const g = b => b + 1;

const flippedTrace = flip(traceInv);

const h = pipe(
g,
flippedTrace('after g'),
f,
flippedTrace('after f'),
);

expect(h(30)).to.equal(62);
assert(console.log.calledWith('after g: 31'), 'It is not after g: 31');
assert(console.log.calledWith('after f: 62'), 'It is not after f: 62');
});
});

0 comments on commit d9aeafe

Please sign in to comment.