Guest User

Untitled

a guest
Jan 24th, 2020
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // code
  2. const add2 = a => b => a + b;
  3.  
  4. const inc = add2(1);
  5.  
  6. const compose2 = (f, g) => x => f(g(x));
  7.  
  8. const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
  9.  
  10. const trace = label => value => {
  11.     console.log(`${label}::${value}`);
  12.     return value;
  13. };
  14.  
  15.  
  16. module.exports = {
  17.     add2,
  18.     inc,
  19.     compose2,
  20.     compose,
  21.     trace,
  22. };
  23.  
  24.  
  25. // tests
  26. const test = require('tape');
  27. const { add2, inc, compose2, compose, trace, words, reverse, unwords, reverseLine } = require('./index');
  28.  
  29. test('add2:: Number -> Number -> Number', assert => {
  30.     const msg = 'add2 should take two numbers and must return the sum';
  31.     const actual = add2(1)(2);
  32.     const expected = 3;
  33.  
  34.     assert.same(actual, expected, msg);
  35.     assert.end();
  36. });
  37.  
  38. test('inc:: Number -> Number', assert => {
  39.     const msg = 'inc should increase a value';
  40.     const actual = inc(1);
  41.     const expected = 2;
  42.  
  43.     assert.same(actual, expected, msg);
  44.     assert.end();
  45. });
  46.  
  47. test('compose2:: (f, g) -> a -> b', assert => {
  48.     const double = x => x * 2;
  49.     const msg = 'should execute f after g';
  50.     const doubleInc = compose2(double, inc);
  51.     const actual = doubleInc(20);
  52.     const expected = 42;
  53.  
  54.     assert.same(actual, expected, msg);
  55.     assert.end();
  56. });
  57.  
  58. test('compose:: (...Function) -> a -> b', assert => {
  59.     const double = x => x * 2;
  60.     const msg = 'should compose all functions';
  61.     const doubleInc = compose(double, inc);
  62.     const actual = doubleInc(20);
  63.     const expected = 42;
  64.  
  65.     assert.same(actual, expected, msg);
  66.     assert.end();
  67. });
  68.  
  69. test('trace:: String -> a -> a', assert => {
  70.     const msg = 'should print a value as `label::value`';
  71.     const actual = trace('test')('myValue');
  72.     const expected = 'myValue';
  73.  
  74.     assert.same(actual, expected, msg);
  75.     assert.end();
  76. });
  77.  
  78. const double = x => x * 2;
  79. const doubleInc = compose(
  80.     trace('after double'),
  81.     double,
  82.     trace('after inc'),
  83.     inc
  84. );
  85.  
  86. const person = {
  87.     lastname: "Fernandez",
  88. };
  89.  
  90. const createUser = (name) => {
  91.     let prefix = "Name::";
  92.     return Object.assign(Object.create(person), { name }, {
  93.         getName: () => `${prefix}${name}`,
  94.     });
  95. };
  96.  
  97. console.log(createUser('Miriam').getName());
  98.  
  99. doubleInc(20);
RAW Paste Data