Last active
August 29, 2015 14:24
-
-
Save jeffpeterson/b3ca8998c4f1fabdcd1a to your computer and use it in GitHub Desktop.
Revisions
-
jeffpeterson renamed this gist
Jul 10, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jeffpeterson renamed this gist
Jul 10, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ export default spool // thread first (left) argument export function spool(s, r) { return _spool(s, r, (r, args) => [r, ...args]) } -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' ? merge(a, b) : b; } -
jeffpeterson revised this gist
Jul 10, 2015 . 4 changed files with 34 additions and 36 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,16 @@ 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"} 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') } 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) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ import mori from 'mori' import {spoor} from 'spool' const m = spoor(mori) // m().vector(1,2,3,4,5).map(x => x + 1).intoArray().__r // [2,3,4,5,6] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,11 @@ // thread first (left) argument export default function spool(s, r) { 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]) } 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 => _set(s, r) } function _set(s, r) { if (r === undefined) return s const o = Object.create(s) o.__r = r return o } -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 10 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(s, r) { 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]); } function _spool(shape, __r, fn) { const s = {__r}; for (const k in shape) { s[k] = function(...args) { return _set(this, shape[k](...fn(this.__r, args))) }; } return r => { return _set(s, r); } } function _set(s, r) { if (r === undefined) return s; var o = Object.create(s); o.__r = r; return o; } -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.__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]) -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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}; -
jeffpeterson revised this gist
Jul 10, 2015 . 2 changed files with 22 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ import mori from 'mori'; import {spoor} from 'spool'; export default const m = spoor(mori); // m().vector(1,2,3,4,5).map(x => x + 1).intoArray().__r // [2,3,4,5,6] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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).__r // -> 16 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) { var o = Object.create(s); o.__r = r; return o; } -
jeffpeterson revised this gist
Jul 10, 2015 . 2 changed files with 13 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()() // [2,3,4,5,6] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)() // -> 16 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); } } function _spool(s, r) { const f = () => r f.__proto__ = s; f.__r = r; return f; } -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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] -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ import spool from 'spool'; // var req = client().databases(1).collections(2).items(3).get() // client(req).post() export default const client = spool({ databases, -
jeffpeterson revised this gist
Jul 10, 2015 . 2 changed files with 73 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ const math = spool({ math(5).add(12).sub(1).__r // -> 16 export function spool(shape, init) { const s = {__r: init}; for (const k in shape) { -
jeffpeterson revised this gist
Jul 10, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, init) { const s = {__r: init}; for (const k in shape) { const fn = shape[k]; @@ -26,3 +25,4 @@ function spool(shape) { return o; } } -
jeffpeterson created this gist
Jul 10, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } }