liveBook · Manning

Curry

As explained in Functional Programming in Scala, Second Edition

curry is a book-defined higher-order function that transforms a two-argument function into a function returning a one-argument function.

Currying transforms a two-argument function into a function that accepts one argument and returns another function. Its type, A => (B => C), means that supplying an A produces a function from B to C; the first argument is therefore partially applied. section 2.5

A curried function in Scala

def curry[A, B, C](f: (A, B) => C): A => (B => C) = a => b => f(a, b) The outer function receives a, and the returned function receives b. Together they apply both arguments to f. section 2.7 section 2.5

Currying belongs to a small group of related function transformations:

  • partial1 fixes one argument of a two-argument function and returns a one-argument function. section 2.5
  • uncurry reverses curry, converting A => B => C into (A, B) => C. section 2.5
  • compose combines functions by feeding one function’s output into another’s input. section 2.5
def curry[A, B, C](f: (A, B) => C): A => (B => C) =
  a => b => f(a, b)

Inspect the complete polymorphic Scala definition of curry. section 2.7

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Explains the type, works through the implementation, and contrasts curry with uncurry and composition.
Examples
Uses polymorphic Scala definitions for curry, uncurry, partial1, and compose.
What it assumes
Assumes function types, function literals, generic type parameters, and applying functions to arguments.
What you can do after
Build it from scratch in Scala by following the curried function type and returning nested function literals.
How it connects
Connects currying with partial application, uncurry, higher-order functions, and function composition.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.