On 5/18/06, Bob Ippolito <[EMAIL PROTECTED]> wrote:
> > On 5/18/06, Arnar Birgisson <[EMAIL PROTECTED]> wrote:
> >> Think of composition as a pipeline of functions: compose(f1, f2, ...,
> >> fN) returns a function that puts it argument backwards through the
> >> pipline, applying fN, then fN-1 to the result etc.
> >
> > Sorry, correction: it doesn't go backwards, it goes forward.
> > compose(f1, f2, ..., fN)(x) == f1(f2(...fN(x)...))
>
> Still looks backwards to me, because fN(x) gets evaluated first.
I don't know what the he** I've been eating lately.. I repeated the
error in the correction :o(
It is compose(f1, f2, ..., fN)(x) == fN(fN-1(...f1(x)...))
Again, I'm sorry. This is a classic confusion-point in maths though.
> compose(f1, f2, .., fN) == compose(f1, compose(f2, ... fN)) and so on
Function composition is associative both ways, so
compose(f,g,h) = compose(f, compose(g,h)) = compose(compose(f,g), h)
> I guess an implementation would look something like this:
>
> compose = function () {
> var fnlist = [];
> for (var i = 0; i < arguments.length; i++) {
> fnlist.push(arguments[i]);
> }
> return function () {
> var args = arguments;
> for (var i = fnlist.length; i >= 0; i--) {
> args = [fnlist[i].apply(this, args)];
> }
> return args[0];
> };
> }
Would be, if not for my nonsense.. :o)
Just change the for loop to count up instead of down.
Arnar
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/mochikit
-~----------~----~----~----~------~----~------~--~---