什么是头等公民职能? [英] What is a first class citizen function?
问题描述
什么是头等公民职能?
Java是否支持头等公民职能?
Does Java supports first class citizen function?
编辑:
如 Wikepedia 所述
函数编程风格的第一类函数是必需的
。
First class functions are a necessity for the functional programming style.
第一类函数还有其他用途吗?
Is there any other use of first class functions?
推荐答案
考虑的语言程序为一流 允许函数像任何其他值一样传递 。
A language that considers procedures to be "first-class" allows functions to be passed around just like any other value.
语言像Java 7(及更早版本)和C种类具有这种能力:C允许传递函数指针,但是你不能动态地定义这些语言中的函数并突然传递到其他地方。版本8之前的Java可以在某种程度上使用匿名类模拟,但它在技术上并不具有一流的功能。
Languages like Java 7 (and earlier) and C "kind of" have this capability: C allows function pointers to be passed around, but you can't dynamically define a function in those languages and suddenly pass that somewhere else. Java before version 8 can simulate this to a certain extent with anonymous classes, but it doesn't technically have first-class functions.
On另一方面,C ++,D,C#,Visual Basic .NET,Java 8+和函数式语言(如Scheme和Haskell) do 允许您传递变量等函数。例如,下面的代码返回一个函数,该函数将 addend
添加到其输入中:
On the other hand, C++, D, C#, Visual Basic .NET, Java 8+, and functional languages (like Scheme and Haskell) do allow you to pass around functions like variables. For example, the code below returns a function that adds addend
to its input:
写在D:
int delegate(int) makeAdder(int addend) //Returns a function
{
return delegate int(int x) //Long way
{
return x + addend; //Notice that addend came from _outside_ the function
};
return (int x) { return x + addend; }; //Short way
return x => addend + x; //Super-short way, introduced in D 2.058
}
用C#编写:
Func<int, int> MakeAdder(int addend) //Returns a function
{
return delegate(int x) //The long way. Note: Return type is implicitly 'int'
{
return x + addend;
};
return x => x + addend; //Short way: x "goes to" (x + addend); inferred types
}
用C ++编写:
#include <functional>
std::function<int(int)> make_adder(int addend)
{
return [=](int x)
{
return addend + x;
};
}
用Scala写的:
def makeAdder(addend: Int) = (x: Int) => addend + x
用Python编写:
def make_adder(addend):
def f(x):
return addend + x
return f
# or...
return lambda x: addend + x
用Erlang编写:
make_adder(Addend) ->
fun(X) -> Addend + X end.
用JavaScript编写:
Written in JavaScript:
function makeAdder(addend) {
return function(x) {
return addend + x;
};
}
用JavaScript编写(ES2015箭头函数语法):
Written in JavaScript (ES2015 arrow function syntax):
const makeAdder = addend => x => addend + x;
写在Scheme中:
(define (makeAdder addend)
(lambda (x)
(+ x addend)))
用Haskell编写:
Written in Haskell:
makeAdder :: Int -> (Int -> Int)
makeAdder addend = \x -> addend + x
用Visual Basic 2008编写:
Written in Visual Basic 2008:
Function MakeAdder(addend As Integer) As Func(Of Integer, Integer)
Return Function(x) (x + addend)
End Function
用Swift编写(详细和短手实现):
Written in Swift (both verbose and short-hand implementations):
func makeAdder(append: Int) -> (x: Int) -> Int {
return { (x: Int) -> Int in
return x + append
};
}
func makeAdder(append: Int) -> (Int) -> Int {
return {$0 + append};
}
(顺便说一句,lambda只是一个没有名字的函数Lambda只支持支持一流功能的语言。)
(By the way, a "lambda" is just a function without a name. Lambdas are only supported in languages that support first-class functions.)
这篇关于什么是头等公民职能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!