liveBook · Manning

Currying

As explained in The Coder Cafe

Currying is the transformation of a function taking multiple arguments into a sequence of functions that each take one argument.

Currying transforms a function that accepts multiple arguments into a sequence of single-argument functions. Calling each function in turn produces the same result while making function composition more fluid. section 3.1.1 section 3.1.2

Currying differs from partial application in its purpose:

  • Currying reshapes f(x, y, z) into f(x)(y)(z), creating a chain of single-argument functions. section 3.4
  • Partial application fixes selected arguments to create a specialized function for reuse. section 3.1.2
  • Together, currying and partial application provide concise ways to break complex functions into smaller components. section 3.1.1 section 3.1.2

Curried addition

A curried three-argument addition function can be called as curriedAdd(1)(2)(3), producing 6. Fixing the first argument creates addFive, a reusable function that awaits the remaining arguments. section 3.1.1

def curriedAdd(x: Int)(y: Int)(z: Int): Int = x + y + z

Inspect the Scala-style definition showing one parameter list for each stage of the curried function. section 3.1.1

How The Coder Cafe covers it

How deep it goes
Defines the transformation, demonstrates sequential calls, and stops short of implementation mechanics.
Examples
Demonstrates curried addition, sequential calls, and fixing 5 to create addFive.
What you can do after
Apply currying to create readable specialized functions and chain multi-argument calls.
How it connects
Connects currying to partial application, function composition, functors, applicatives, and monads.

Also explained in

Compare how 10 books explain currying →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Coder Cafe. The section links point to the parts of the book it draws on.