On 5/17/06, Bob Ippolito <[EMAIL PROTECTED]> wrote:
> I don't think there's anything quite like this in MochiKit.
Function composition is a "classic" feature of functional programming,
I'm surprised it isn't already in MochiKit. :o)
> What are some use cases for this? What is construct typically named?
> I can't recall ever running across a library or language that has a
> composition function like that.
This would definately save multiple "maps" as Harald points out.
It's typically named "composition" or "function composition". They
syntax in haskell is f.g, and (o f g) in Lisp and its relatives,
because the mathematical notation for it is a small circle. They Dylan
programming language calls it "compose", which I think would be a good
choice for MochiKit.
Implementation should be easy, but bear in mind that it's common to
combine more than two functions, i.e. combine(f1,f2,f3,...,fN)(x) ==
f1(f2(f3(...fN(x)...))).
Using javascripts call method, the implementation would be simple:
function compose() {
var rcall = function(arg,f) {
return f.call(null, arg);
};
return partial(reduce, rcall, reversed(arguments));
}
The only downside here is that the resulting function only takes one
argument, even if the last function in the composition can accept
more.
Which reminds me, another common feature of functional programming
languages is reduceright - which produces a right leaning tree instead
of a left-leaning one:
reduceright(operator.divide, [1,2,3,4], 10) = (1 / (2 / (3 / (4/10))))
This would eliminate the need for the reversed function (rcall) and
the reversing of the arguments list.
And, since I'm started, it would be nice to have something like
Python's "apply" function as well, since using f.apply and f.call can
be cumbersome.
Arnar
ps. Bob: sorry for the double post, the first one was meant for the
list as well :o)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---