Skip to content

Commit

Permalink
feat(ch09): add function composition example
Browse files Browse the repository at this point in the history
to compose two functions only
  • Loading branch information
poulzinho committed Jan 4, 2020
1 parent 441a7bd commit 2065387
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ import {expect} from 'chai';
import {sum} from "./9.1_curry-and-fn-composition";

describe("Curry and Function Composition", () => {
it("should apply two functions in a curried form", ()=> {
it("should apply two functions in a curried form", () => {
expect(sum(2)(4)).to.equal(6);
});

it("should allow to use point-free style", () => {
// inc10 is a specialized version of sum with param 'a' fixed to 1
const inc10 = sum(10);
expect(inc10(5)).to.equal(15);
})
});

it("should allow function composition of two functions", () => {
const g = a => a + 1;
const f = b => b * 2;
const h = x => f(g(x));

const composeFn = (f, g) => x => f(g(x));

expect(h(30)).to.equal(62);
expect(composeFn(f,g)(30)).to.equal(62);
});
});
3 changes: 3 additions & 0 deletions src/ch-09_curry-and-fn-composition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ All curried fns return partial apps. However, not all partial apps result from c
- Define functions without declaring formal parameters
- function and arrow functions (=>) cannot be used
- instead call a function that returns a function with its own closure and fixed variable

## Why to curry
- For function composition

0 comments on commit 2065387

Please sign in to comment.