Currying

Currying is an FP technique that allows us to use partial application without having to worry about partial application while we write our functions. Currying is the process of taking a function that takes multiple arguments and transforming it into a chain of unary functions:

function curry2<T1, T2, T3>(fn: (a: T1, b: T2) => T3) {
    return (a: T1) => (b: T2) => fn(a, b); 
} 

The preceding function is a higher-order function that allows us to abstract our functions from the partial application functionality:

function add(a: number, b: number) { 
    return a + b; 
} 
 
const curriedAdd = curry2(add); 
const add5 = curriedAdd(5); 
const addResult = add5(5); 
console.log(addResult); // 10 

The curry2 function allows us to transform a binary function ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.