Skip to content

Instantly share code, notes, and snippets.

@jeffpeterson
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save jeffpeterson/b3ca8998c4f1fabdcd1a to your computer and use it in GitHub Desktop.

Select an option

Save jeffpeterson/b3ca8998c4f1fabdcd1a to your computer and use it in GitHub Desktop.

Revisions

  1. jeffpeterson renamed this gist Jul 10, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jeffpeterson renamed this gist Jul 10, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion spool.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    export default spool

    // thread first (left) argument
    export default function spool(s, r) {
    export function spool(s, r) {
    return _spool(s, r, (r, args) => [r, ...args])
    }

  4. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion client.js
    Original file line number Diff line number Diff line change
    @@ -69,5 +69,5 @@ function _merge(a, b) {
    if (!a) return b
    if (!b) return a

    return typeof a === 'object' && typeof b === 'object' ? b : merge(a, b)
    return typeof a === 'object' && typeof b === 'object' ? merge(a, b) : b;
    }
  5. jeffpeterson revised this gist Jul 10, 2015. 4 changed files with 34 additions and 36 deletions.
    30 changes: 15 additions & 15 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    import spool from 'spool';
    import spool from 'spool'

    // var req = client().databases(1).collections(2).items(3).get()
    // client(req.__r).post().__r // {"path":["/api/v1","databases",1,"collections",2,"items",3],"method":"POST"}

    export default const client = spool({
    const client = spool({
    databases,
    collections,
    items,
    get,
    post,
    put,
    }, {});
    }, {})

    function collections(r, id) {
    return path(r, ['collections', id])
    @@ -25,49 +25,49 @@ function items(r, id) {
    }

    function get(r, id) {
    return method(r, 'GET');
    return method(r, 'GET')
    }

    function post(r, id) {
    return method(r, 'POST');
    return method(r, 'POST')
    }

    function put(r, id) {
    return method(r, 'PUT');
    return method(r, 'PUT')
    }


    /////////////////////////////////////////////

    function method(r, method) {
    return merge(r, {method});
    return merge(r, {method})
    }

    function path(r, p) {
    return nest(r, {path: p});
    return nest(r, {path: p})
    }

    function headers(r, headers) {
    return merge(r, {headers});
    return merge(r, {headers})
    }

    function nest(...rs) {
    return {
    ...merge(...rs),
    path: [].concat(...rs.map(r => r.path).filter(p => p)),
    };
    }
    }

    function merge(...rs) {
    return rs.reduce((o, r) => {
    for (const k in r) o[k] = _merge(o[k], r[k])
    return o;
    }, {});
    return o
    }, {})
    }

    function _merge(a, b) {
    if (!a) return b;
    if (!b) return a;
    if (!a) return b
    if (!b) return a

    return typeof a === 'object' && typeof b === 'object' ? b : merge(a, b);
    return typeof a === 'object' && typeof b === 'object' ? b : merge(a, b)
    }
    8 changes: 8 additions & 0 deletions math.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    import spool from './spool'

    const math = spool({
    add(x, n) { return x + n },
    sub(x, n) { return x - n },
    })

    math(5).add(12).sub(1).__r // -> 16
    6 changes: 3 additions & 3 deletions mori-chain.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import mori from 'mori';
    import {spoor} from 'spool';
    import mori from 'mori'
    import {spoor} from 'spool'

    export default const m = spoor(mori);
    const m = spoor(mori)

    // m().vector(1,2,3,4,5).map(x => x + 1).intoArray().__r // [2,3,4,5,6]
    26 changes: 8 additions & 18 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,11 @@
    const math = spool({
    add(x, n) { return x + n },
    sub(x, n) { return x - n },
    })

    math(5).add(12).sub(1).__r // -> 16

    // thread first (left) argument
    export default function spool(s, r) {
    return _spool(s, r, (r, args) => [r, ...args]);
    return _spool(s, r, (r, args) => [r, ...args])
    }

    // thread last (right) argument
    export function spoor(s, r) {
    return _spool(s, r, (r, args) => [...args, r]);
    return _spool(s, r, (r, args) => [...args, r])
    }

    function _spool(shape, __r, fn) {
    @@ -21,18 +14,15 @@ function _spool(shape, __r, fn) {
    for (const k in shape) {
    s[k] = function(...args) {
    return _set(this, shape[k](...fn(this.__r, args)))
    };
    }
    }

    return r => {
    return _set(s, r);
    }
    return r => _set(s, r)
    }

    function _set(s, r) {
    if (r === undefined) return s;
    var o = Object.create(s);
    o.__r = r;
    return o;
    if (r === undefined) return s
    const o = Object.create(s)
    o.__r = r
    return o
    }

  6. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 10 additions and 17 deletions.
    27 changes: 10 additions & 17 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -6,40 +6,33 @@ const math = spool({
    math(5).add(12).sub(1).__r // -> 16

    // thread first (left) argument
    export default function spool(shape, __r) {
    const s = {__r};

    for (const k in shape) {
    s[k] = function(...args) {
    return _spool(this, shape[k](this.__r, ...args))
    };
    }

    return r => {
    return _spool(s, r);
    }
    export default function spool(s, r) {
    return _spool(s, r, (r, args) => [r, ...args]);
    }

    // thread last (right) argument
    export function spoor(shape, __r) {
    export function spoor(s, r) {
    return _spool(s, r, (r, args) => [...args, r]);
    }

    function _spool(shape, __r, fn) {
    const s = {__r};

    for (const k in shape) {
    s[k] = function(...args) {
    return _spool(this, shape[k](...args, this.__r))
    return _set(this, shape[k](...fn(this.__r, args)))
    };
    }

    return r => {
    return _spool(s, r);
    return _set(s, r);
    }
    }

    function _spool(s, r) {
    function _set(s, r) {
    if (r === undefined) return s;
    var o = Object.create(s);
    o.__r = r;
    return o;
    }


  7. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -36,6 +36,7 @@ export function spoor(shape, __r) {
    }

    function _spool(s, r) {
    if (r === undefined) return s;
    var o = Object.create(s);
    o.__r = r;
    return o;
  8. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import spool from 'spool';

    // var req = client().databases(1).collections(2).items(3).get()
    // client(req).post()
    // client(req.__r).post().__r // {"path":["/api/v1","databases",1,"collections",2,"items",3],"method":"POST"}

    export default const client = spool({
    databases,
    @@ -10,7 +10,7 @@ export default const client = spool({
    get,
    post,
    put,
    });
    }, {});

    function collections(r, id) {
    return path(r, ['collections', id])
  9. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ const math = spool({

    math(5).add(12).sub(1).__r // -> 16

    // thread first (left) argument
    export default function spool(shape, __r) {
    const s = {__r};

    @@ -19,6 +20,7 @@ export default function spool(shape, __r) {
    }
    }

    // thread last (right) argument
    export function spoor(shape, __r) {
    const s = {__r};

  10. jeffpeterson revised this gist Jul 10, 2015. 2 changed files with 22 additions and 9 deletions.
    6 changes: 3 additions & 3 deletions mori-chain.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import mori from 'mori';
    import spool from 'spool';
    import {spoor} from 'spool';

    export default const m = spool(mori);
    export default const m = spoor(mori);

    // m().vector(1,2,3,4,5).map(x => x + 1).intoArray()() // [2,3,4,5,6]
    // m().vector(1,2,3,4,5).map(x => x + 1).intoArray().__r // [2,3,4,5,6]
    25 changes: 19 additions & 6 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@ const math = spool({
    sub(x, n) { return x - n },
    })

    math(5).add(12).sub(1)() // -> 16
    math(5).add(12).sub(1).__r // -> 16

    function spool(shape, __r) {
    export default function spool(shape, __r) {
    const s = {__r};

    for (const k in shape) {
    @@ -19,11 +19,24 @@ function spool(shape, __r) {
    }
    }

    export function spoor(shape, __r) {
    const s = {__r};

    for (const k in shape) {
    s[k] = function(...args) {
    return _spool(this, shape[k](...args, this.__r))
    };
    }

    return r => {
    return _spool(s, r);
    }
    }

    function _spool(s, r) {
    const f = () => r
    f.__proto__ = s;
    f.__r = r;
    return f;
    var o = Object.create(s);
    o.__r = r;
    return o;
    }


  11. jeffpeterson revised this gist Jul 10, 2015. 2 changed files with 13 additions and 12 deletions.
    2 changes: 1 addition & 1 deletion mori-chain.js
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,4 @@ import spool from 'spool';

    export default const m = spool(mori);

    // m().vector(1,2,3,4,5).map(x => x + 1).intoArray().__r // [2,3,4,5,6]
    // m().vector(1,2,3,4,5).map(x => x + 1).intoArray()() // [2,3,4,5,6]
    23 changes: 12 additions & 11 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -3,26 +3,27 @@ const math = spool({
    sub(x, n) { return x - n },
    })

    math(5).add(12).sub(1).__r // -> 16
    math(5).add(12).sub(1)() // -> 16

    export function spool(shape, init) {
    const s = {__r: init};
    function spool(shape, __r) {
    const s = {__r};

    for (const k in shape) {
    const fn = shape[k];
    s[k] = function(...args) {
    return _spool(this, fn(this.__r, ...args))
    return _spool(this, shape[k](this.__r, ...args))
    };
    }

    return r => {
    return _spool(s, r);
    }

    function _spool(s, r) {
    var o = Object.create(s);
    o.__r = r;
    return o;
    }
    }

    function _spool(s, r) {
    const f = () => r
    f.__proto__ = s;
    f.__r = r;
    return f;
    }


  12. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions mori-chain.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    import mori from 'mori';
    import spool from 'spool';

    export default const m = spool(mori);

    // m().vector(1,2,3,4,5).map(x => x + 1).intoArray().__r // [2,3,4,5,6]
  13. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion client.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import spool from 'spool';

    // client().databases(1).collections(2).items(3).get()
    // var req = client().databases(1).collections(2).items(3).get()
    // client(req).post()

    export default const client = spool({
    databases,
  14. jeffpeterson revised this gist Jul 10, 2015. 2 changed files with 73 additions and 1 deletion.
    72 changes: 72 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    import spool from 'spool';

    // client().databases(1).collections(2).items(3).get()

    export default const client = spool({
    databases,
    collections,
    items,
    get,
    post,
    put,
    });

    function collections(r, id) {
    return path(r, ['collections', id])
    }

    function databases(r, id) {
    return path(r, ['databases', id])
    }

    function items(r, id) {
    return path(r, ['items', id])
    }

    function get(r, id) {
    return method(r, 'GET');
    }

    function post(r, id) {
    return method(r, 'POST');
    }

    function put(r, id) {
    return method(r, 'PUT');
    }


    /////////////////////////////////////////////

    function method(r, method) {
    return merge(r, {method});
    }

    function path(r, p) {
    return nest(r, {path: p});
    }

    function headers(r, headers) {
    return merge(r, {headers});
    }

    function nest(...rs) {
    return {
    ...merge(...rs),
    path: [].concat(...rs.map(r => r.path).filter(p => p)),
    };
    }

    function merge(...rs) {
    return rs.reduce((o, r) => {
    for (const k in r) o[k] = _merge(o[k], r[k])
    return o;
    }, {});
    }

    function _merge(a, b) {
    if (!a) return b;
    if (!b) return a;

    return typeof a === 'object' && typeof b === 'object' ? b : merge(a, b);
    }
    2 changes: 1 addition & 1 deletion spool.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ const math = spool({

    math(5).add(12).sub(1).__r // -> 16

    function spool(shape, init) {
    export function spool(shape, init) {
    const s = {__r: init};

    for (const k in shape) {
  15. jeffpeterson revised this gist Jul 10, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,12 @@

    const math = spool({
    add(x, n) { return x + n },
    sub(x, n) { return x - n },
    })

    math(5).add(12).sub(1).__r // -> 16

    function spool(shape) {
    const s = {};
    function spool(shape, init) {
    const s = {__r: init};

    for (const k in shape) {
    const fn = shape[k];
    @@ -26,3 +25,4 @@ function spool(shape) {
    return o;
    }
    }

  16. jeffpeterson created this gist Jul 10, 2015.
    28 changes: 28 additions & 0 deletions spool.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@

    const math = spool({
    add(x, n) { return x + n },
    sub(x, n) { return x - n },
    })

    math(5).add(12).sub(1).__r // -> 16

    function spool(shape) {
    const s = {};

    for (const k in shape) {
    const fn = shape[k];
    s[k] = function(...args) {
    return _spool(this, fn(this.__r, ...args))
    };
    }

    return r => {
    return _spool(s, r);
    }

    function _spool(s, r) {
    var o = Object.create(s);
    o.__r = r;
    return o;
    }
    }