viernes, 9 de noviembre de 2007

Higher order functions

In mathematics, higher order functions are functions that map functions to functions. The typical example is the derivative, along with integration. If you've studied electromagnetism you'll have also encountered the divergence and curl operators in maxwell's equations.

In computer science, higher order functions are equivalent to those in mathematics, that is, they are functions that take functions as arguments or return functions as arguments. Depending on the language, higher order functions can be implemented "out of the box" or need some trick to achieve. For example, higher order functions are central to functional programming, whereas in Java you can only achieve the same effects through the creation of intermediate objects that contain the functions. As an excercise, here's the strategy used to achieve higher order function-like behaviour in some popular languages

Functional languages (eg Haskell): functions are first-class objects
Java: passing/returning objects with methods
C/C++: function pointers
php: passing/returning objects with methods, also eval
javascript: functions are first-class objects

The typical example of use of a higher order function is a generic sort function, where a user supplied comparator is used to achieve the desired result.

No hay comentarios: