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

2 Key excerpts on "First Class Functions"

  • Functional Programming in Go
    eBook - ePub

    Functional Programming in Go

    Apply functional techniques in Golang to improve the testability, readability, and security of your code

    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 work

    Benefits 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.
  • Android Development with Kotlin
    • 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
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.