Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Higher-order function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Defunctionalization==== [[Defunctionalization]] can be used to implement higher-order functions in languages that lack [[first class function|first-class functions]]: <syntaxhighlight lang="cpp"> // Defunctionalized function data structures template<typename T> struct Add { T value; }; template<typename T> struct DivBy { T value; }; template<typename F, typename G> struct Composition { F f; G g; }; // Defunctionalized function application implementations template<typename F, typename G, typename X> auto apply(Composition<F, G> f, X arg) { return apply(f.f, apply(f.g, arg)); } template<typename T, typename X> auto apply(Add<T> f, X arg) { return arg + f.value; } template<typename T, typename X> auto apply(DivBy<T> f, X arg) { return arg / f.value; } // Higher-order compose function template<typename F, typename G> Composition<F, G> compose(F f, G g) { return Composition<F, G> {f, g}; } int main(int argc, const char* argv[]) { auto f = compose(DivBy<float>{ 2.0f }, Add<int>{ 5 }); apply(f, 3); // 4.0f apply(f, 9); // 7.0f return 0; } </syntaxhighlight> In this case, different types are used to trigger different functions via [[function overloading]]. The overloaded function in this example has the signature <code>auto apply</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)