views:

693

answers:

2

What's the best way to implement a classic curry function in actionscript with a nice syntax?

I've tried:

Function.prototype.curry = function()
{
return "helloWorld";
}

trace((function():void {}).curry());

...approach but that didn't work.

I guess I'm stuck with a ugly approach such as:

FunctionUtils.curry(fp, ... args)

???

+1  A: 

Ended up with (heavily inspired by dojo's implementation):

public static 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);
}

Request in the comments section to show an example of how to use this:

function foo(i:int, j:int):void
{
   trace(i+j);
}

function bar(fp:Function):void
{
   fp(2);
}

bar(FunctionUtils.curry(foo, 1)); //trace==3

Silly example, I know, but curry:ing is extremely useful. Have a look at http://www.svendtofte.com/code/curried_javascript/ for theory.

Niels Bosma
Now could you give me an example of where you would use this curry function? (It would help with my understanding functional programming more)
defmeta
+5  A: 

I must admit I've never understood the difference between "curry" and "partial". I use the following function to do more or less what you want to do:

package {
  public function partial( func : Function, ...boundArgs ) : Function {
    return function( ...dynamicArgs ) : * {
      return func.apply(null, boundArgs.concat(dynamicArgs))
    }
  }
}

Usage examples:

var multiply : Function = function( a : Number, b : Number ) : Number { return a * b; }
var multiplyByFour : Function = partial(multiply, 4);

trace(multiplyByFour(3)); // => 12
Theo
Which argument is replaced with 4? a or b?
JoeCoder
b, arguments are bound from left to right.
Theo