Last active
December 25, 2015 15:39
-
-
Save ThomasBurleson/6999692 to your computer and use it in GitHub Desktop.
AS3 Implementation of `curry` function to pre-capture target function arguments left-to-right. Only when are the arguments have been captured does the target function get invoked with the accumulated argument values.
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
package utils.function | |
{ | |
public function curry(func:Function, ... args:Array):* | |
{ | |
var arity:int = func.length; | |
var currying:Function = function(func:Function, arity:int, args:Array):* | |
{ | |
return function(... moreArgs:Array):* { | |
if(moreArgs.length + args.length < arity) | |
{ | |
return currying(func, arity, args.concat(moreArgs)); | |
} | |
return func.apply(this, args.concat(moreArgs)); | |
} | |
} | |
return currying(func, arity, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a sample usage of
curry()
: