Skip to content

Instantly share code, notes, and snippets.

@evanrs
Created February 22, 2016 20:18
Show Gist options
  • Save evanrs/faa4b705697136850cda to your computer and use it in GitHub Desktop.
Save evanrs/faa4b705697136850cda to your computer and use it in GitHub Desktop.
Method to create function trees that curry to their parent.
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