Curried functions in JavaScript (how to employ genericly, and invisibly)?

Giganews Newsgroups
Subject: Curried functions in JavaScript (how to employ genericly, and invisibly)?
Posted by:  svendtof…@svendtofte.com (svend)
Date: 22 Jul 2003

Hey there,

I was thinking about some curried functions in JavaScript, and I made
this:

<script>
function add(a, b, c) {
    if (arguments.length == 1) {
        return function (d,e) {return a + d + e}
    }
    if (arguments.length == 2) {
        return function (d) {return a + b + d}
    }
    return a + b + c;
}

t = add(1,2);
alert(t(3));

alert(add(1,2,3));
</script>

Which works, but it's ugly, to say the least. So I was wondering, if
there's a way, to create this, in a more generic way? I'm aware, that
it probably wont be SML like curried, which is totally transparent,
but is there anyway at all?

In a way, so that it works, as close to SML as possible, though I'm
willing to accept, some code will be needed?

Regards,
Svend

Replies