Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a first class citizen function?

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?

like image 872
Alpine Avatar asked Mar 03 '11 08:03

Alpine


People also ask

What is considered first-class citizen?

Noun. first-class citizen (plural first-class citizens) A member of a class of individuals that receive fair treatment. (programming, languages) Synonym of first-class object.

How do you define first-class citizens in functional programming languages?

In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities.

What operations can be performed on a first-class object?

In short, anything is a first-class object if it acts in the language as a state manipulation sort of object or type of object. Simply something you can operate on and pass around statements and evaluate in expressions at the same time.

Are functions first-class citizens in go?

In programming language design, first-class citizens (that is, types, objects, entities, or values) in a given programming language can assign functions to variables, or use functions directly as parameters or return values of other functions.


2 Answers

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.)

like image 117
user541686 Avatar answered Oct 02 '22 02:10

user541686


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.

like image 36
Nagakishore Sidde Avatar answered Oct 02 '22 02:10

Nagakishore Sidde