Skip to content

Instantly share code, notes, and snippets.

@Akira-Hayasaka
Last active October 7, 2020 05:22
Show Gist options
  • Save Akira-Hayasaka/839ab2c2c701919b8427df808134cabd to your computer and use it in GitHub Desktop.
Save Akira-Hayasaka/839ab2c2c701919b8427df808134cabd to your computer and use it in GitHub Desktop.
currying & Function Composition (aka HOC)
// curried functions, a.k.a HOC
const witha = (extra) => (component) => (props) => {
console.log("witha", extra);
console.log("witha", component);
console.log("witha", props);
return () => component(props);
};
const withb = (extra) => (component) => (props) => {
console.log("withb", extra);
console.log("withb", component);
console.log("withb", props);
return () => component(props);
};
// dummy component
const component = (props) => {
console.log("render component with", props);
};
// partial application
let hoca = witha("extraaa")(component);
let hocb = withb("extrabb")(hoca);
let compo = hocb("shared props");
// rendering
compo()();
console.log("");
// one line
withb("extrabb")(witha("extraaa")(component))("shared props")()();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment