Computer Science
First Class Functions
First-class functions refer to the ability of a programming language to treat functions as first-class citizens. This means that functions can be assigned to variables, passed as arguments to other functions, and returned as values from functions. This feature is essential for functional programming paradigms and enables higher-order functions.
Written by Perlego with AI-assistance
Related key terms
1 of 5
3 Key excerpts on "First Class Functions"
- eBook - ePub
Functional Programming in Go
Apply functional techniques in Golang to improve the testability, readability, and security of your code
- Dylan Meeus(Author)
- 2023(Publication Date)
- Packt Publishing(Publisher)
2
Treating Functions as First-Class Citizens
As we established in the previous chapter, the core part of our functional programs will be functions. In this chapter, we are going to cover exactly why functions are powerful in languages that treat them as first-class citizens . Go has functions as first-class citizens out of the box, meaning we get this functionality by default. More and more languages are choosing this approach. In this chapter, we are going to see how this will allow us to create interesting constructs, which will improve the readability and test ability of our code.Concretely, we are going to cover the following topics:- Benefits of first-class functions
- Defining types for functions
- Using functions like objects
- Anonymous functions versus named functions
- Storing functions in data types or structs
- Creating a function dispatcher using all the previous
Technical requirements
All the examples for this chapter can be found at https://github.com/PacktPublishing/Functional-Programming-in-Go./tree/main/Chapter2 . For this example, any Go version will workBenefits of first-class functions
Before we talk about “first-class functions,” let’s first define what it means for anything to be called “first-class” in programming language design. When we talk about a “first-class citizen,” we mean an entity (object, primitive, or function) for which all the common language operations are available. These are operations such as assignment, passing it to a function, returning from a function, or storing it in another data type such as a map.Looking at this list, we can see how all of those operations typically apply to the structs that we are defining in our language. Objects and primitives can be passed around between functions. They are often returned as the results of a function and we definitely assign them to variables. When we say that functions are first-class citizens, you can simply think of this as treating functions like objects. Their equivalence will help us create all future constructs in this book. They will lead to improved testability , such as by allowing us to mock functions of a struct, and improved readability , such as by removing large switch cases for a single function dispatcher. - eBook - ePub
- Marcin Moskala, Igor Wojda(Authors)
- 2017(Publication Date)
- Packt Publishing(Publisher)
Functions as First-Class Citizens
In the previous chapter, we saw how Kotlin features relate to OOP. This chapter will introduce advanced functional programming features that were previously not present in standard Android development. Some of them were introduced in Java 8 (in Android through the Retrolambda plugin), but Kotlin introduces many more functional programming features.This chapter is about high-level functions and functions as first-class citizens. Most of the concepts are going to be familiar to readers who have used functional languages in the past. In this chapter, we will cover the following topics:- Function types
- Anonymous functions
- Lambda expressions
- Implicit name of a single parameter in a lambda expression
- Higher-order functions
- Last lambda in argument convention
- Java Single Abstract Method (SAM ) lambda interface
- Java methods with Java Single Abstract Method on parameters usage
- Named parameters in function types
- Type aliases
- Inline functions
- Function references
Passage contains an image
Function type
Kotlin supports functional programming, and functions are first-class citizens in Kotlin. A first-class citizen, in a given programming language, is a term that describes an entity that supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, and assigned to a variable. The sentence "a function is a first-class citizen in Kotlin " should then be understood as: it is possible in Kotlin to pass functions as an argument, return them from functions, and assign them to variables - eBook - PDF
Functional Programming in C#
Classic Programming Techniques for Modern Projects
- Oliver Sturm(Author)
- 2011(Publication Date)
- Wiley(Publisher)
Higher order functions are also important in functional programming. Higher order functions are those that take other functions as parameters or return other functions as their results. Many programming languages have some support for this capability. Even C has a syntax to define a type of a function or, in C terms, to refer to the function through a function pointer. Obviously this enables C programmers to pass around such function pointers or to return them from other functions. Many C libraries contain functions, such as those for searching and sorting, that are implemented as higher order functions, taking the essential data-specific comparison functions as parameters. Then again, C doesn’t have any support for anonymous functions — that is, functions created on-the-fly, in-line, like lambda expressions, or for related concepts such as closures. Other examples of language capabilities that help define functional programming are explored in the following chapters in this book. For some programmers, functional programming is a natural way of telling the computer what it should do, by describing the properties of a given problem in a concise language. You might have heard the saying that functional programming is more about telling computers what the problem is they should be solving, and not so much about specifying the precise steps of the solution. This saying is a result of the high level of abstraction that functional programming provides. Referential transparency means that the only responsibility of the programmer is the specification of functions to describe and solve a given set of problems. On the basis of that specification, the computer can then decide on the best evaluation order, potential parallelization opportunities, or even whether a certain function needs to be evaluated at all. For some other programmers, functional programming is not the starting point. They come from a procedural, imperative, or perhaps object oriented background.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.


