Last active
July 31, 2019 11:29
-
-
Save monochromer/db4f8105f52e9254908e to your computer and use it in GitHub Desktop.
curry and partial. каррирование и частичное применение
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
// 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; | |
}; |
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
/** | |
* создание каррированной функции, | |
* используя 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))); | |
}; | |
}); |
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
function curry(f, ...first) { | |
return (...second) => f(...first, ...second) | |
} |
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
// 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