Created
February 22, 2016 20:18
-
-
Save evanrs/faa4b705697136850cda to your computer and use it in GitHub Desktop.
Method to create function trees that curry to their parent.
This file contains 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 characters
import assign from "lodash/assign"; | |
import functions from "lodash/functions"; | |
export function recurry (fn, fns={}) { | |
fn = assign(fn, fns); | |
return function () { | |
if (arguments.length < fn.length) { | |
const bound = fn.bind(null, ...arguments); | |
functions(fn).map((key) => | |
bound[key] = recurry(fn[key])(...arguments)); | |
return bound | |
} | |
return fn(...arguments); | |
} | |
} | |
// example | |
export const findRequest = | |
recurry ( | |
function findRequest (state, query) { | |
return find(selectRequestHistory(state), query); | |
}, | |
{ byId: (state, id) => findRequest(state, { id }) | |
} | |
) | |
const findResource = | |
recurry( | |
function findResource (state, query) { | |
return find(selectResources(state), query) | |
}, | |
{ byType: (state, type) => | |
findResource(state, { type }) | |
} | |
) | |
export const filterResources = | |
recurry( | |
function filterResources (state, query) { | |
return filter(selectResources(state), query) | |
}, | |
{ byType: (state, type) => | |
filterResources(state, { type }) | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment