Skip to content

Commit 0012272

Browse files
committed
Solutions
1 parent ab4be2f commit 0012272

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

source/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// (f . g)(x) = f(g(x))
2+
// compose(...fns) => x => y
3+
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
4+
5+
export const flying = o => {
6+
let isFlying = false;
7+
8+
return Object.assign({}, o, {
9+
fly () {
10+
isFlying = true;
11+
return this;
12+
},
13+
isFlying: () => isFlying,
14+
land () {
15+
isFlying = false;
16+
return this;
17+
}
18+
});
19+
};
20+
21+
export const quacking = quack => o => Object.assign({}, o, {
22+
quack: () => quack
23+
});
24+
25+
export const createDuck = quack => pipe(
26+
flying,
27+
quacking(quack)
28+
)({});

source/index.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import test from 'tape';
2+
3+
import { flying, createDuck } from './index';
4+
5+
// What is a functional mixin?
6+
/*
7+
flying(o) => m: o & {
8+
fly() => m,
9+
isFlying() => Boolean,
10+
land() => m
11+
}
12+
*/
13+
test('flying.fly() and .isFlying()', assert => {
14+
const msg = 'should set isFlying() to true';
15+
const o = flying({});
16+
17+
const actual = o.fly().isFlying();
18+
const expected = true;
19+
20+
assert.same(actual, expected, msg);
21+
assert.end();
22+
});
23+
24+
test('flying.land()', assert => {
25+
const msg = 'should set isFlying() to false';
26+
const o = flying({}).fly();
27+
assert.same(o.isFlying(), true, 'starts out flying');
28+
29+
const actual = o.land().isFlying();
30+
const expected = false;
31+
32+
assert.same(actual, expected, msg);
33+
assert.end();
34+
});
35+
36+
test('flying() mixin', assert => {
37+
const msg = 'should mix into existing object';
38+
const startingObject = { a: 'a' };
39+
40+
const actual = flying(startingObject).a;
41+
const expected = 'a';
42+
43+
assert.same(actual, expected, msg);
44+
assert.end();
45+
});
46+
47+
test('createDuck()', assert => {
48+
const msg = 'should have FLYING and quacking';
49+
const quack = 'QUACK!';
50+
const duck = createDuck(quack);
51+
52+
const actual = duck.fly().quack();
53+
const expected = quack;
54+
55+
assert.same(actual, expected, msg);
56+
assert.end();
57+
});
58+
59+
// When should you use functional mixins?
60+
// * state / data privacy

0 commit comments

Comments
 (0)