Skip to content

Instantly share code, notes, and snippets.

@nobitagit
Last active June 6, 2018 21:20
Show Gist options
  • Select an option

  • Save nobitagit/99c427e5ec161e0d48263e5be5849e65 to your computer and use it in GitHub Desktop.

Select an option

Save nobitagit/99c427e5ec161e0d48263e5be5849e65 to your computer and use it in GitHub Desktop.

Revisions

  1. nobitagit revised this gist Jun 6, 2018. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions numlen.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    const len = num => {
    const isNum = !isNaN(parseFloat(num)) && isFinite(num);
    if (!isNum) {
    return new Error('not a number');
    }
    const str = num.toString().replace('.', '');

    return str.length;
    }

    const assertions = [
    len(4) === 1,
    len(44) === 2,
    len(444) === 3,
    len(4444) === 4,
    len(2222.11) === 6,
    len("abc") instanceof Error,
    ];

    assertions.map(console.assert);

  2. nobitagit created this gist Jun 6, 2018.
    33 changes: 33 additions & 0 deletions pipe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // goal is to make this work
    pipe(
    function a(c) {
    return c + 1;
    },
    a => a + 1,
    )(3); // 5

    // 1st stage, nope...
    const pipe = (...args) => {
    return args.map(fn => fn())
    }

    // 2nd stage, not quite..
    const pipe = (...args) => {
    return args.reduce((acc, fn) => {
    const ret = fn(acc);
    return ret;
    }, null);
    }

    // 3rd stage, it works!
    const pipe = (...args) => arg => {
    return args.reduce((acc, fn) => {
    const ret = fn(acc);
    return ret;
    }, arg);
    }

    // 4th stage, cleanup
    const pipe = (...args) => arg => {
    return args.reduce((acc, fn) => fn(acc), arg);
    }