What is a first class citizen function?

半腔热情 提交于 2019-12-17 10:23:40

问题


What is a first class citizen function?

Does Java supports first class citizen function?

Edit:
As mention on Wikepedia

First class functions are a necessity for the functional programming style.

Is there any other use of first class functions?


回答1:


A language that considers procedures to be "first-class" allows functions to be passed around just like any other value.

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 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:

Written in 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
}

Written in 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
}

Written in C++:

#include <functional>

std::function<int(int)> make_adder(int addend)
{
    return [=](int x)
    {
        return addend + x;
    };
}

Written in Scala:

def makeAdder(addend: Int) = (x: Int) => addend + x

Written in Python:

def make_adder(addend):
    def f(x):
        return addend + x
    return f
    # or...
    return lambda x: addend + x

Written in Erlang:

make_adder(Addend) ->
    fun(X) -> Addend + X end.

Written in JavaScript:

function makeAdder(addend) {
    return function(x) {
        return addend + x;
    };
}

Written in JavaScript (ES2015 arrow function syntax):

const makeAdder = addend => x => addend + x;

Written in Scheme:

(define (makeAdder addend)
  (lambda (x)
    (+ x addend)))

Written in Haskell:

makeAdder :: Int -> (Int -> Int)
makeAdder addend = \x -> addend + x

Written in Visual Basic 2008:

Function MakeAdder(addend As Integer) As Func(Of Integer, Integer)
    Return Function(x) (x + addend)
End Function

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};
}

(By the way, a "lambda" is just a function without a name. Lambdas are only supported in languages that support first-class functions.)




回答2:


A first class function can be passed around. A typical example is the map function. Here is an example in Scala that squares the elements of a list:

val square = (x:Int) => x*x

val squaredList = List(1,2,3,4).map(square _)
//--> List(1,4,9,16)

The square function is here an argument to the map method, which applies it to every element. If you want to do something like this in Java, you have to use a method wrapped in a class, something like this:

interface F<A,B>{ B apply(A a); }

static <A,B> List<B> map(List<A> list, F<A,B> f) {
  List<B> result = new ArrayList<B>();
  for(A a:list) result.add(f.apply(a));
  return result;   
}

//we have to "wrap" the squaring operation in a class in order to make it a function
F<Integer,Integer> square = new F<Integer,Integer>(){ 
  Integer apply(Integer a) { return a*a; }
}

List<Integer> ints = Arrays.<Integer>asList(1,2,3,4);
List<Integer> squares = map(ints, square);

Looking at this you can see that you can get the same task somehow done in Java, but with more overhead, and without "native" support by the language, but by using a workaround (wrapper classes). So Java doesn't support first class functions, but can "simulate" them.

Hopefully Java 8 will support first class functions. If you want to have some support for this now, look at http://functionaljava.org/ or http://functionalj.sourceforge.net/ , or have a look at the Scala language.




回答3:


The Wikipedia definition is pretty good—it's a function that can be passed around like any other piece of data. Java does not support them. The closest it has is Runnable and Callable objects.




回答4:


Let us consider the example of functional programming paradigm in which functions are the first class citizens. When we say functions are the first class citizens, we can do the following things with the function...

  • Function can be assigned to a variable
  • Function can be stored in a data structure
  • Function can be passed around as an argument to other functions
  • Function can be returned from the functions

In functional programming languages, it is possible to do the above mentioned things.

Now, let us try to answer the question, whether java supports first class citizen functions (or) not.

In java, methods are equivalent of functions. It is not possible to do any of the above with methods. But all of the above are possible with java objects. So, objects are the first class citizens in java. Admittedly, java8 supports passing of methods (method behavior, to be precise) to other methods using functional interfaces and lambda expressions. But that does not mean that java has functions as first class citizens.

The ability to do above things such as passing around functions, returning functions from functions is very powerful and useful. This is because, it allows us to pass around the behavior not just the data.




回答5:


No, you cannot assign a method to a variable or pass it as an argument to another method for example.

Instead you can use interfaces to wrap the intended behaviour, or reflection to reify methods.




回答6:


Functions are first class citizen means you can pass function anywhere as if it's a variable.

From Scala

def isOdd(in: Int) = in % 2 == 1
val n = (1 to 10).toList
n.filter(isOdd)

see here: isOdd is a function. passed as if it's a variale.

Objects are first class citizen in Java. A first class citizen is the one that can be passed anywhere. The parallel is from a first class citizen of country are allowed almost everywhere.

Read:

  • When is a feature considered a “First class citizen” in a programming language/platform?
  • First-class object
  • First-class function


来源:https://stackoverflow.com/questions/5178068/what-is-a-first-class-citizen-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!