Skip to content

Commit

Permalink
feat(ch06): add functors example
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Dec 29, 2019
1 parent af3e8ad commit 43ada3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/ch-06_what-is-fp/6.1_what-is-fp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const double = x => x * 2;
const doublePoints = x => x.points * 2;

export const incItem = x => ({...x, val: x.val + 1});

export const doubleItem = x => ({...x, val: x.val * 2});

export const doubleMap = numbers => numbers.map(double);
export const doubleMapRecords = records => records.map(doublePoints);
12 changes: 11 additions & 1 deletion src/ch-06_what-is-fp/6.1_what-is-fp.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assert, expect} from "chai";
import {doubleItem, doubleMap, incItem} from "./6.1_what-is-fp";
import {doubleItem, doubleMap, doubleMapRecords, incItem} from "./6.1_what-is-fp";

describe("What is Functional Programming", () => {
it("should avoid mutating shared state", () => {
Expand Down Expand Up @@ -34,4 +34,14 @@ describe("What is Functional Programming", () => {
it("should encourage the usage of mappers", () => {
expect(doubleMap([2, 3, 5])).to.deep.equal([4, 6, 10]);
});

it("should encourage the usage of functors", () => {
const records = [
{name: 'foo', points: 2},
{name: 'bar', points: 3},
{name: 'baz', points: 5},
];

expect(doubleMapRecords(records)).to.deep.equal([4, 6, 10]);
})
});

0 comments on commit 43ada3a

Please sign in to comment.