Better Curry
=================
Because return function(){ return fn.apply(context, Array.prototype.slice.call(arguments)); }
isn't good enough, that's why better curry.
Forget Function.bind
and func.apply(context, arguments)
, performance matters! For a better curry!
You won't find any other curry module that can achieve those benchmarks.
Install
~ npm install better-curry
or
~ bower install better-curry
Works on the browser or on node.js (super duper performance on the latter)
API
BetterCurry.wrap(fn, [context], [len])
When the function have all arguments defined.
You can bind the new resulting function to a new context (change the this
inside the function).
function base(argument1, argument2){
return this.data + ' ' + argument1 + argument2;
}
var based = BetterCurry.wrap(base, {data: 'hurry'});
based('up','!');
If the function doesn't have defined parameters, you can still coerce
the function to adopt a length
function base(){
return Array.prototype.slice.call(arguments).join(' + ');
}
var based = BetterCurry.wrap(base, null, 3);
based('one', 'two', 'three', 'will be ignored');
Passing a len
of -1
will make sure it behaves like variadic (that is, uses fn.apply(context, Array.prototype.slice.call(arguments));
function base(){
return Array.prototype.slice.call(arguments).join(' + ');
}
var based = BetterCurry.wrap(base, null, -1);
based('one', 'two', 'three', 'wont be ignored','its','free for all');
BetterCurry.predefine(fn, args, [context], [len])
Predefine creates a function that, when executed, will have the
predefined arguments plus any arguments that you pass:
function base(argument){
return argument;
}
var based = BetterCurry.predefine(base, ['argument','will be ignored']);
based('this will be ignored as well');
Variadic work as well, just remember to pass the len -1
(or 8
if you think it will be that big...)
function base(){
return Array.prototype.slice.call(arguments).join(' + ');
}
var curried = BetterCurry.predefine(base, ['1','2','3','4'], null, -1);
curried('5');
curried = BetterCurry.predefine(base, ['1','2','3','4'], null, 5);
curried('5','6');
All -1
len are slower since it uses Function.apply
(many times slower than Function.call
and Array.prototype.slice
+ concat
)
BetterCurry.delegate(proto, target)
A minor rewrite of visionmedia's delegates but around 13% faster
var obj = {
request: {
_value: 1,
function1: function(){},
get value(){
return this._value;
},
set value(val){
this._value = val;
},
}
};
var delegated = BetterCurry.delegate(obj, 'request');
delegated
.method('function1')
.access('value')
.access({name: 'value', as: 'value2'})
.method({name: 'function1', as: 'function2', args:['arg1']});
obj = {
function1: function1(){},
function2: function1(arg1){},
value:
value2:
request: {}
};
Test
Just regular stuff (100% coverage by the way)
npm run test
npm run coverage
=============================== Coverage summary ===============================
Statements : 100% ( 94/94 ), 3 ignored
Branches : 100% ( 305/305 ), 5 ignored
Functions : 100% ( 28/28 )
Lines : 100% ( 94/94 )
================================================================================
Benchmark
Curious about performance? Get flabbergasted.
npm run benchmark
License
The MIT License (MIT)
Copyright (c) 2014 Paulo Cesar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.