Skip to content

Instantly share code, notes, and snippets.

@monochromer
Last active July 31, 2019 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monochromer/db4f8105f52e9254908e to your computer and use it in GitHub Desktop.
Save monochromer/db4f8105f52e9254908e to your computer and use it in GitHub Desktop.
curry and partial. каррирование и частичное применение
// https://www.youtube.com/watch?v=ND8KQ5xjk7o
const partial = (fn, ...args) => (...rest) => fn(...args.concat(rest))
// 1
const curry = fn => (...args) =>
fn.length > args.length
? curry(fn.bind(null, ...args));
: fn(...args);
// 2
const curry = (fn, ...par) => {
const curried = (...args) =>
fn.length > args.length
? curry(fn.bind(null, ...args))
: fn(...args);
return par.length ? curried(...par) : curried;
};
/**
* создание каррированной функции,
* используя gist - https://gist.github.com/monochromer/c95ac295f2a9ae7c231f
* @param {string} name - имя функции
* @param {Function} func - определение функции
*/
Function.method('curry', function ( ) {
var slice = Array.prototype.slice,
args = slice.apply(arguments),
that = this;
return function ( ) {
return that.apply(null, args.concat(slice.apply(arguments)));
};
});
function curry(f, ...first) {
return (...second) => f(...first, ...second)
}
// https://medium.com/devschacht/tom-harding-curry-on-wayward-son-293d1c4f455f
const uncurryish = f => {
if (typeof f !== 'function')
return f // Needn't curry!
return (... xs) => uncurryish(
xs.reduce((f, x) => f(x), f)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment