We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Currying

Function currying turns a function with multiple arguments into successive functions that each accept one argument.

For example, we can supply a shirt's unit price now and the quantity later:

func main() {
	shirtCost := curriedCost(20)
	fmt.Println(shirtCost(3))
	// prints 60

	fmt.Println(shirtCost(5))
	// prints 100
}

func cost(unitPrice, quantity int) int {
	return unitPrice * quantity
}

func curriedCost(unitPrice int) func(int) int {
	return func(quantity int) int {
		return cost(unitPrice, quantity)
	}
}

curriedCost(20) returns a closure that remembers the unit price and waits for a quantity. Calling shirtCost(3) supplies that quantity and calls cost(20, 3).

You can also call the returned function immediately:

cost(20, 3)        // returns 60
curriedCost(20)(3) // returns 60

Assignment

Textio needs loggers that remember which server an error came from.

Complete the getLogger function. It accepts a server label (a string).