higher-order function first-order function
2016-11-04 18:10
169 查看
Higher-order function
From Wikipedia, the free encyclopediaNot to be confused with Functor (category theory).
This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. Please help to improve this article by introducing more precise citations. (September 2013) (Learn how and when to remove this template message) |
science, a higher-order function (also functional, functional form or functor) is a function that
does at least one of the following:
takes one or more functions as arguments (i.e., procedural
parameters),
returns a function as its result.[disputed – discuss]
All other functions are first-order functions. In mathematics higher-order functions are also termed operators or functionals.
The differential operator in calculus is
a common example, since it maps a function to its derivative, also a function.
In the untyped lambda calculus,
all functions are higher-order; in a typed lambda calculus, from which most functional
programming languages are derived, higher-order functions that take one function as argument are values with types of the form {\displaystyle
(\tau _{1}\to \tau _{2})\to \tau _{3}}
.
General examples[edit]
The mapfunction,
found in many functional programming languages, is one example of a higher-order function. It takes as arguments a function f and a list of elements, and as the result, returns a new list with f applied to each element from the list. Another
very common kind of higher-order function in those languages which support them are sorting functions which take a comparison function as a parameter, allowing the programmer to separate the sorting algorithm from the comparisons of the items being sorted.
The C standard function
qsortis
an example of this.
Other examples of higher-order functions include fold, function
composition, and integration.
Direct support[edit]
The examples are not intended to compare and contrast programming languages, but to serve as examples of higher-order function syntaxIn the following examples, the higher-order function
twicetakes a function, and applies the function
to some value twice. If
twicehas to be applied several times for the same
fit
preferably should return a function rather than a value. This is in line with the "don't
repeat yourself" principle.
Python[edit]
Further information: Python (programming language)>>> def twice(function): ... return lambda x: function(function(x)) >>> def f(x): ... return x + 3 >>> g = twice(f) >>> print g(7) 13
JavaScript[edit]
Further information: JavaScriptvar twice = function(f, v) { return f(f(v)); }; var f = function(v) { return v + 3; }; console.log(twice(f, 7)); // 13
Go[edit]
Further information: Go (programming language)func twice(f func(int) int, v int) int { return f(f(v)) } func main() { f := func(v int) int { return v + 3 } twice(f, 7) // returns 13 }
相关文章推荐
- python higher-order function demo
- The study of Higher-order function in Python(20170913)
- python——高阶函数(Higher-order function)
- ocx function and the order of BEGIN_DISPATCH_MAP
- A missing vtable usually means the first non-inline virtual member function has no definition
- qemu-2.4.0编译时出错:error: ‘XFS_IOC_ZERO_RANGE’ undeclared (first use in this function)
- First chance exception & R6025-pure virtual function call的问题 Opencv
- oracle中查询最近的一条记录(FIRST_VALUE() OVER(PARTITION BY ORDER BY ))
- order by nulls first、order by nulls last 转(光辉大帝)
- first order system analysis 自控原理 一阶系统的matlab分析
- NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
- errno.h中定义的常量undeclared (first use in this function)
- Function to Convert Only First Letter Capital of each word in a sentence
- Creating and changing a production order from ABAP FUNCTION 'CLOI_CHANGES_UPL_31'
- (NOT CLEAR)Question 44: What member function of std::fstream could a C++ developer invoke in order to change the target output f
- JavaScript Functional Programming:高阶函数 Higher order functions
- error: ‘IPPROTO_RAW’ undeclared (first use in this function)
- a missing vtable usually means the first non-inline virtual member function has no definition.
- First Lua function running in C
- C编译时`true' undeclared (first use in this function)