|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
For function composition in mathematics, see function composition.
In computer science, function composition (not to be confused with object composition) is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of the composed function is passed to the composing one via a parameter. Because of this similarity, the syntax in program code tends to closely follow that in mathematics. As a subroutine, the feature appears in most programming languages. For example, suppose we have two arithmetic functions f and g, as in z = f(y) and y = g(x). One way of composing these two functions would be to first compute y, and then to compute z from y, as in y = g(x) followed by z = f(y). In a programming context, the only obvious difference from mathematics involves notation. Here is the same example but implemented in the C programming language:
float foo (float x){
float y, z;
y=g(x);
z=f(y);
return z;
}
One could get the same result with the one line composition in mathematics f(g(x)), by the corresponding one line function in C:
float foo (float x) { return f(g(x));}
Despite differences in length, these two implementations compute the same result (providing their data types are the same). The longer first implementation is known as the "single-assignment" form of function composition. This form is useful in the areas of parallel programming and embedding logic onto field programmable gate array devices (see Hammes, et. al). The shorter second example is known as "sequential composition" (Abadi and Lamport pg 96), since the result of the second function depends on the result of the first. Another type of functional composition known as "parallel composition" (Pierce and Turner pg 2) (Abadi and Lamport pg 96), enables a developer to compose two or more functions so that each runs in parallel on its own separate computer. The second implementation requires only one line of code and is colloquially referred to as a "highly composed" form. Readability and hence maintainability is one advantage of highly composed forms, since they require fewer lines of code, minimizing a program's "surface area" (Cox pp 15-17). DeMarco empirically verifies an inverse relationship between surface area and maintainability (DeMarco pp 133-135). On the other hand, it may be possible to overuse highly composed forms. A nesting of too many functions may have the opposite effect, making the code less maintainable. A related issue is the dilemma of whether to compose (put together) or "factor" (break apart) functions for maintainability and code reuse. In a functional programming language, such as Haskell, function composition can be expressed rather naturally. The example given above becomes: f . g using the composition operator (.) :: (b -> c) -> (a -> b) -> a -> c, which can be read as f after g or g composed with f. The composition operator itself can be defined in Haskell using a lambda expression as: f . g = \x -> f (g x) In javascript we can see that the operator effectively takes two functions f and g, and produces a function: function o(f, g) { return function(x) { return f(g(x)); } } In a stack-based language, functional composition is even more natural: it is performed by concatenation, and is usually the primary method of program design. The above example in Forth: g f Research SurveyNotions of composition, including the principle of compositionality and composability, are so ubiquitous that numerous strands of research have separately evolved. The following is a sampling of the kind of research in which the notion of composition is central.
References
See also |
| Ja np. kupiłam taki oryginalny prezent mojemu misiowi. • a • Komunikatory Internetowe • Turystyka • Warzywa • Wzgórza • Najnowsze Darmowe mp3 do pobrania • Wojna Światowa • Reklama • Photoshop • tektura • Malta Poznan to galeria oraz Malta Poznan to centrum rozrywki • piosenki h • piosenki i • 9 piosenki All Right Reserved © 2007, Designed by Stylish Blog. |