Skip to content

Commit

Permalink
feat(ch09): use currying for specializing functions, example
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Jan 7, 2020
1 parent e3f1afb commit 79846dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export const trace = label => value => {
};

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

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

describe("Curry and Function Composition", () => {
Expand Down Expand Up @@ -70,5 +70,17 @@ describe("Curry and Function Composition", () => {
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');
});

it("should specialize functions by currying", () => {
const array = [1, 2, 3, 4, 5];
const isEven = x => x % 2 === 0;

const stripe = n => isEven(n) ? 'even' : 'odd';

const stripeAll = map(stripe);
const striped = stripeAll(array);

expect(striped).to.deep.equal(['odd', 'even', 'odd', 'even', 'odd']);
})
});

0 comments on commit 79846dd

Please sign in to comment.