First Class Function at Five Levels

1. Child:

Imagine you have a toy that you can play with in many ways. You can give it to your friends to play, or you can store it in a toy box, or you can even put other toys inside it. Just like that toy, in programming, a “first-class function” is a function (which is like a small machine) that can be used in many ways. You can store it, pass it to other functions, or even use functions inside of other functions!

2. Teenager:

You know how in some video games, certain items or characters have more abilities or privileges than others? They can be used in more ways, like being combined with other items, or given as gifts to other players. First-class functions in programming are like those special game items. They can be stored in variables, used as arguments in other functions, and returned from other functions.

3. College student (not a CS major):

First-class functions in programming languages mean that functions are treated just like any other data. That means you can do anything with a function that you can do with other kinds of data, like numbers or strings. You can store functions in variables, put them in arrays, pass them as arguments to other functions, and even have functions that produce new functions as their results.

4. Computer Science Undergraduate:

First-class functions are a feature of a programming language that supports building abstractions by using functions as values that can be manipulated like other types of values. They can be assigned to variables, stored in data structures, passed as arguments, and returned as values from other functions. Higher-order functions, which accept other functions as parameters or return functions as results, are a direct outcome of first-class function support.

5. Grad student/Colleague:

First-class functions refer to a feature of a programming language where functions are treated as first-class citizens, meaning they are entities that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as any other variable in the programming language. Languages with first-class functions allow for functional programming style, enabling the creation of higher-order functions, closures, and currying. JavaScript is an example of such a language, where functions can be assigned to variables, stored in objects or arrays, passed as arguments, and returned from other functions.

Richard Feynman Explanation

Imagine we live in a world where everything is a citizen. In this world, people, dogs, cats, and even robots all have the same rights and privileges. They can all own property, ride the bus, vote in elections, you name it. They are all considered ‘first class citizens.’

Now, in the world of programming, specifically in languages like JavaScript, we have something called ‘first class functions.’ Just like the citizens in our imaginary world, functions in JavaScript enjoy the same rights and privileges as other elements like numbers or strings. They can be assigned to variables, put into arrays, passed as arguments to other functions, and even returned as values from other functions.

So, when you hear ‘first class functions,’ just remember the world where people, dogs, cats, and robots all can do the same things. In JavaScript, that’s what functions can do! They’re not second class or third class, they’re first class - with all the rights and privileges that come with that status.

First Class Function in Scheme

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. In simple terms, it’s when functions in a language are treated like any other variable.

Scheme, a dialect of Lisp, is one of such languages that support first-class functions. Here is an example:

1
2
(define (apply-twice f x)
  (f (f x)))

In this example, apply-twice is a higher-order function that takes another function f and an argument x as input. It applies the function f to x twice. The ability to use functions as values like this is a result of Scheme’s support for first-class functions.

We can use this function as follows:

1
2
(define (add3 x) (+ x 3))
(display (apply-twice add3 10))

Here, we’ve defined a function add3 that adds 3 to its input, and we’re using it as an argument to apply-twice. This will add 3 to 10 twice, resulting in 16.

This is an example of how first-class functions can be used to write very flexible and powerful code. They are a key feature of functional programming languages like Scheme.

First Class Function in JavaScript

Just like Scheme, JavaScript also treats functions as first-class citizens. Here is an equivalent example in JavaScript:

1
2
3
4
5
6
7
8
9
function applyTwice(f, x) {
  return f(f(x));
}

function add3(x) {
  return x + 3;
}

console.log(applyTwice(add3, 10)); // Outputs: 16

In this JavaScript code, applyTwice is a higher-order function that takes a function f and a value x as arguments. It applies the function f to the value x twice. The add3 function is then passed as an argument to applyTwice, demonstrating that in JavaScript, functions can be treated just like any other value and passed as arguments to other functions.

First Class Function in Ruby

Ruby also treats functions as first-class citizens. But, in Ruby, functions are typically treated as methods of objects. Here is an equivalent example in Ruby, using the lambda function:

1
2
3
4
5
apply_twice = -> (f, x) { f.call(f.call(x)) }

add3 = -> (x) { x + 3 }

puts apply_twice.call(add3, 10) # Outputs: 16

In this Ruby code, apply_twice is a lambda that takes a lambda f and a value x as arguments. It applies the lambda f to the value x twice. The add3 lambda is then passed as an argument to apply_twice, demonstrating that in Ruby, lambdas can be treated just like any other value and passed as arguments to other lambdas.

First Class Function in Python

Python is another language that treats functions as first-class citizens. Here is an equivalent example in Python:

1
2
3
4
5
6
7
def apply_twice(f, x):
    return f(f(x))

def add3(x):
    return x + 3

print(apply_twice(add3, 10))  # Outputs: 16

In this Python code, apply_twice is a higher-order function that takes a function f and a value x as arguments. It applies the function f to the value x twice. The add3 function is then passed as an argument to apply_twice, demonstrating that in Python, functions can be treated just like any other value and passed as arguments to other functions.

First Class Function in Kotlin

Kotlin is a statically-typed language that runs on the JVM and fully supports the functional programming style with first-class functions. Let’s demonstrate it using an equivalent example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fun applyTwice(f: (Int) -> Int, x: Int): Int {
    return f(f(x))
}

fun add3(x: Int): Int {
    return x + 3
}

fun main() {
    println(applyTwice(::add3, 10))  // Outputs: 16
}

In this Kotlin code, applyTwice is a higher-order function that takes a function f and a value x as parameters. It applies the function f to the value x twice. The add3 function is then passed as an argument to applyTwice using ::add3 (the double colon operator is used to pass the reference to the function). This demonstrates that in Kotlin, functions can be treated just like any other value and passed as arguments to other functions.