Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content