Currying

Function currying is a concept from functional programming and involves partial application of functions. It allows a function with multiple arguments to be transformed into a sequence of functions, each taking a single argument.

Example:

func main() {
  squareFunc := selfMath(multiply)
  doubleFunc := selfMath(add)

  fmt.Println(squareFunc(5))
  // prints 25

  fmt.Println(doubleFunc(5))
  // prints 10
}

func multiply(x, y int) int {
  return x * y
}

func add(x, y int) int {
  return x + y
}

func selfMath(mathFunc func(int, int) int) func (int) int {
  return func(x int) int {
    return mathFunc(x, x)
  }
}

In the example above, the selfMath function takes in a function as its parameter and returns a function that itself returns the value of running that input function on its parameter.


Assignment

The Textio API needs a very robust error-logging system so we can see when things are going awry in the back-end system. We need a function that can create a custom "logger" (a function that prints to the console) given a specific formatter.

Complete the getLogger function. It should take as input a formatter function and return a new function. The new logger function takes as input two strings and passes them to the formatter, then prints the result. Keep the order of the strings.

Full program (solution included):


1

Step 1 — Function signature

  • getLogger receives a function (formatter) that takes two strings and returns a single string.

  • getLogger returns another function that takes two strings and prints (does not return) the formatted result.

2

Step 2 — Return an anonymous function (closure)

Return an anonymous function that captures formatter:

The returned function is a closure: it "remembers" the formatter passed to getLogger.

3

Step 3 — Usage

Example usage in the program:

circle-info

In plain English: getLogger builds a custom printing function. You supply a formatter, and it returns a logger that uses that formatter to print messages consistently.

Key concepts

Concept
Meaning

Higher-order function

A function that takes or returns another function (getLogger)

Anonymous function

A function with no name (func(first, second string) { ... })

Closure

When an inner function “remembers” variables from its outer scope (formatter)

Formatter

A function that defines how two strings are combined (e.g. colonDelimit, commaDelimit)


Links:

  • Currying: https://en.wikipedia.org/wiki/Currying

  • Partial application: https://en.wikipedia.org/wiki/Partial_application