Curried Functions - nus-cs2030/2122-s1 GitHub Wiki

Currying

Currying is the transformation of a function with multiple arguments into a sequence of single-argument functions. For example, add(1,1) to add(1)(1)

Example of Currying

Example sourced from here

function isGreaterThan(a) {
  return function(b) {
    return b > a;
  }
}

//same as function above (this is the curried version)
const isGreaterThan = a => b => b > a

//calling of function changes from isGreaterThan(2, 5) -> isGreaterThan(2)(5)

Benefits

Simpler syntax

Facilitate function composition

Makes it possible to partially recompose the original values using decurrying