Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // code
- const add2 = a => b => a + b;
- const inc = add2(1);
- const compose2 = (f, g) => x => f(g(x));
- const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
- const trace = label => value => {
- console.log(`${label}::${value}`);
- return value;
- };
- module.exports = {
- add2,
- inc,
- compose2,
- compose,
- trace,
- };
- // tests
- const test = require('tape');
- const { add2, inc, compose2, compose, trace, words, reverse, unwords, reverseLine } = require('./index');
- test('add2:: Number -> Number -> Number', assert => {
- const msg = 'add2 should take two numbers and must return the sum';
- const actual = add2(1)(2);
- const expected = 3;
- assert.same(actual, expected, msg);
- assert.end();
- });
- test('inc:: Number -> Number', assert => {
- const msg = 'inc should increase a value';
- const actual = inc(1);
- const expected = 2;
- assert.same(actual, expected, msg);
- assert.end();
- });
- test('compose2:: (f, g) -> a -> b', assert => {
- const double = x => x * 2;
- const msg = 'should execute f after g';
- const doubleInc = compose2(double, inc);
- const actual = doubleInc(20);
- const expected = 42;
- assert.same(actual, expected, msg);
- assert.end();
- });
- test('compose:: (...Function) -> a -> b', assert => {
- const double = x => x * 2;
- const msg = 'should compose all functions';
- const doubleInc = compose(double, inc);
- const actual = doubleInc(20);
- const expected = 42;
- assert.same(actual, expected, msg);
- assert.end();
- });
- test('trace:: String -> a -> a', assert => {
- const msg = 'should print a value as `label::value`';
- const actual = trace('test')('myValue');
- const expected = 'myValue';
- assert.same(actual, expected, msg);
- assert.end();
- });
- const double = x => x * 2;
- const doubleInc = compose(
- trace('after double'),
- double,
- trace('after inc'),
- inc
- );
- const person = {
- lastname: "Fernandez",
- };
- const createUser = (name) => {
- let prefix = "Name::";
- return Object.assign(Object.create(person), { name }, {
- getName: () => `${prefix}${name}`,
- });
- };
- console.log(createUser('Miriam').getName());
- doubleInc(20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement