in reply to Functional Composition

The same as this, but very, very slowly!

c:\test>perl -E"say join'-',split'', reverse ucfirst 'hello';" o-l-l-e-H

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: Functional Composition
by billh (Pilgrim) on Dec 17, 2009 at 21:38 UTC

    You're right of course, but I chose perl builtins to keep the example short. If you consider that each of those 'fn's could be arbitrarily complex and require braces around their arguments that would become

    perl -E"say(join('-', split('', reverse(ucfirst('hello')))))"

    Plus I have to admit I kind of screwed up, "functional composition" really means "take two functions and produce a third", like

    compose(sub{ ucfirst $[0] }, sub { $_[0] . "\n" });

    would return a function that would ucfirst its arg and append a newline, whereas my code invokes the functions directly. Still, it looks pretty.

    Bill H
    perl -e 'print sub { "Hello @{[shift->()]}!\n" }->(sub{"World"})'