Talk:Function composition

Is this task subject to the "limitation" of First-class functions, ruling out C (or Fortran, or...), or we can accomplish the task implementing just a function that returns f(g(x)), having as argument f, g and x? --ShinTakezou 15:17, 3 March 2009 (UTC)

Hi, the limit is you have to create a function of f and g that returns another function. It is that other function, when applied to x would be the same as doing f(g(x)). If you look at the Python example, function compose returns function sin_cos. it is then sin_cos(x) that is equivalent to sin(cos(x)). In short, you need to create function compose. Thanks. --Paddy3118 02:54, 4 March 2009 (UTC)
I.e. this is possible only for languages that have First-class functions (by the way, it seems like this task is already covered by showing that the language has first class functions in First-class functions task page) --ShinTakezou 11:27, 4 March 2009 (UTC)
Thats right, it is another aspect of first class functions but there is no need to show functions as members of other collection types. Some languages may be able to do this and not First Class Functions. --Paddy3118 15:48, 4 March 2009 (UTC)
Let me show another doubt of mine. What a function is for a language, is definible inside the same language; C can deal naturally with pointers, and we pass function to e.g. other functions by pointer (reference?); so foo(bar) call foo with argument bar (by the way, "calling a subroutine" for compiled languages means always to know its address, even at the end of the games a run-time, such the one of Objective-C when "finding" the code for a selector, compute the address of other compiled code...), and "foo" is the function (which C handles by pointer). So this task, if the right constraint of First-class functions is dropped (exactly the first: "New functions can be created from others at run time"), can be implemented in C. If it is not droppable, languages can accomplish this task iff they "have" first class functions. Am I reasonably right? --ShinTakezou 16:23, 4 March 2009 (UTC)


IF you can create a version of compose that works for function pointers when you have an arbitrary set of two functions to compose then why not put it down? I add the extra restriction because if you had n sets of f and g functions to compose, i.e.
 composeN = compose(fN, gN) for N in 0..N-1 
Then any of the composeN should be able to be used after all have been created. I can think of a naive implementation using function pointers where the last call to compose would reset the actions of all the composeN - this is not what is meant. My C is a little rusty but I guess if you could do something like:

<lang c> fg = compose(&f, &g)

 (*fg)(x) /* Where (*fg)(x) == f(g(x)) and another call to compose would leave this one alone */</lang>
--Paddy3118 19:27, 4 March 2009 (UTC)
Hm, I was thinking about other (not portable...) methods; I will try... ;) --ShinTakezou 22:15, 4 March 2009 (UTC)
Spoon! I think you beated me:D At the beginning I was following a method similar to yours (except that I called function capsule something similar to what you called functor). But I was dissatisfied since this works only for function with double arg and returning double; I was thinking about a way of composing functions not dependently by the type of the value passed/returned, I thought to encapsulate the function in a function (!) which wants void * as input and ret value, and then it can be "dereferenced" and casted properly by the programmer (and a macro facility)... I was experimenting different methods (also using inline assembly:D) ... at the moment, failing:(... so I calm down unless your solution is marked as not ok for the task :D. Interesting. --ShinTakezou 23:26, 4 March 2009 (UTC)
Yea, void * would work. I didn't use it because then I would have to allocate and de-allocate doubles all over the place. But feel free to change it. --Spoon! 00:01, 5 March 2009 (UTC)

Spoon, Shintakezou; I think the C solution should stay, as it does show the kind of hoops you would have to go through to implement this task in C. It also helps to explain the second paragraph in First-class function: Availability, on why they don't normally include C in the list of FP languages and mention the limitations of function pointers. --Paddy3118 23:53, 4 March 2009 (UTC)

J

Function composition is fundamental in J. J has a rich set of composition primitives and syntax to combine functions in multiple, interesting ways.

In the following examples, f and g are functions, x and y are variables (data). The syntax f y means the function f applied to one argument, y (unary), whereas x f y means the function f applied to (between) two arguments, x and y (binary).

Here's a selection of J's composition options:

Composition: Unary interpretation: fg y Binary interpretation: x fg y Notes
@ f(g y) f(x g y) f applied to each output of g independently
@. To be discussed
@: f(g y) f(x g y) f applied to all outputs of g simultaneously
& f(g y) (g x)f(g y) f applied between each output of g on x and y pairwise
&. To be discussed
&.: To be discussed
&: f(g y) (g x)f(g y) f applied between all outputs of g on x and y in toto
. To be discussed
.. ((f y) + (g x)f(g y))/2 N/A Given hf..g, the resulting function, h, is even in the sense that (h y) = (h -y) for any y ; its graph is reflected in the vertical axis.
.: ((f y) - (g x)f(g y))/2 N/A Given hf.:g, the resulting function, h, is odd in the sense that (h y) = (-h -y) for any y ; its graph is reflected in the origin.
: f y x g y Allows the unary and binary definitions of a function to be specified independently.
:. To be discussed
:: try { f y } catch { g y } try { x f y } catch { x g y } Given hf::g, if f returns a valid value without error, then the result of h is the result of f; else, the result of h is the result of g. These succinct, functional exception handlers can be chained.
hook To be discussed
fork To be discussed

Still to discuss

@. = functional selection (f`g@.h)
&. = under, this is a good one.
&.: = &.: is to &. as &: is to &.
. = f.g is defined in terms of a recursive expansion by minors along the first column when unary, and as a generalized inner product when binary.
:. = related to &., another good one.
hook = an implicit composition of 2 functions
fork = an implicit composition of 3 functions
Return to "Function composition" page.