Last active
October 31, 2015 21:25
-
-
Save sperand-io/6b5e0f77a3d0998bc256 to your computer and use it in GitHub Desktop.
<3 ES6
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
export default function applyFirst (fn, first) { | |
return (...args) => fn(first, ...args); | |
} |
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 variadic(fn) { | |
if (fn.length < 1) return fn; | |
return function() { | |
var ordinaryArgs = (1 <= arguments.length ? __slice.call(arguments, 0, fn.length - 1) : []), | |
restOfTheArgsList = __slice.call(arguments, fn.length - 1), | |
args = (fn.length <= arguments.length ? ordinaryArgs.concat([restOfTheArgsList]) : []); | |
return fn.apply(this, args); | |
} | |
}; | |
module.exports = function applyFirst(fn, first) { | |
return variadic(function (args) { | |
return fn.apply(this, [first].concat(args)) | |
}) | |
} |
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 applyFirst from './applyFirst' | |
function greet(me, you, them) { | |
return `Hello, ${ you } and ${ them }, my name is ${ me }`; | |
} | |
const chrisGreets = applyFirst(greet, 'Chris'); | |
chrisGreets('Nathan', 'everyone else'); | |
// => "Hello, Nathan and everyone else, my name is Chris" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment