Overview

Let’s take a look at what a first-class function is and why we use it in Golang.

What is a first-class function

First-Class Function refers to the concept of treating functions as first-class citizens in a programming language. This means that functions can be treated in the same way as other data types. This means that functions can be assigned to variables, passed as arguments to other functions, and returned by other functions.

Higher-order functions

A programming language that supports first-class functions naturally supports higher-order functions as well. Higher-order functions are functions that take other functions as parameters or return functions as results, and since functions must be treated as values, higher-order functions exist based on first-order functions.

List of languages that support first-class functions

Languages with these characteristics tend to adopt or support the functional programming paradigm.

  • Javascript
  • Python
  • Ruby
  • Scala
  • Haskell
  • Lisp
  • Elixir
  • Go
  • Swift
  • Rust

First-class function features

  • Reusability:** By storing functions in variables and allowing them to be passed to other functions, code can be reused more flexibly.
  • Abstraction:** With Higher-Order Functions, you can manipulate your code at a higher level of abstraction. This improves the readability and maintainability of your code.

Example of a first-order function in Golang

Assign to variable

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6    // assign a function to a variable
 7    add := func(x, y int) int {
 8        return x + y
 9    }
10
11    // call a function through a variable
12    result := add(3, 4)
13    fmt.Println(result) // output: 7
14}

Function that takes a function as an argument

 1package main
 2
 3import "fmt"
 4
 5// Define the type of function that takes an argument
 6func operate(a, b int, operation func(int, int) int) int {
 7    return operation(a, b)
 8}
 9
10func main() {
11    // addition function
12    add := func(a, b int) int {
13        return a + b
14    }
15
16    // multiply function
17    multiply := func(a, b int) int {
18        return a * b
19    }
20
21    // Execute addition and multiplication operations using the operate function
22    sum := operate(5, 2, add)
23    product := operate(5, 2, multiply)
24
25    fmt.Println("Sum:", sum) // Output: Sum: 7
26    fmt.Println("Product:", product) // Output: Product: 10
27}

The sort package, one of Golang’s default packages, implements functions that take advantage of these first-class features.

Sort package

 1package main
 2
 3import (
 4    "fmt"
 5    "sort"
 6)
 7
 8func main() {
 9    numbers := []int{4, 2, 3, 1, 5}
10
11    // The sort.Slice function uses a comparison function that takes a slice 
12	// and the indices of two elements in the slice as arguments.
13    // If the comparison function returns true, the order of the elements is preserved.
14    sort.Slice(numbers, func(i, j int) bool {
15        return numbers[i] < numbers[j]
16    })
17
18    fmt.Println(numbers) // print [1, 2, 3, 4, 5]
19}

In this example, the sort.Slice function takes an anonymous function as its second argument, which takes the indices of two elements in the slice as arguments and implements the comparison logic to determine the sort order. Passing a function as an argument to another function like this is a typical usage of first-class functions.

Summary

In this article, we’ve seen what a first-class function is and how you can benefit from using it through a Golang code example. Similar to closures , first-class functions are used as a key concept in functional programming.

Most modern programming languages support functional programming, and while it’s not exactly the same in Java, there was a process of introducing lambda expressions and functional interfaces after version 1.8 to follow the functional paradigm.

I haven’t used functional programming in the field, but I would like to study and post about it if I have time.