January 2019
Beginner
210 pages
4h 47m
English
In the previous chapters, we declared a higher-order function named compose, which allows us to compose two functions:
const compose = <T>(f: (x: T) => T, g: (x: T) => T) => (x: T) => f(g(x));
The compose function allowed us to demonstrate how function composition works, but it had some limitations. For example, the compose function only takes one generic type, parameter T, which means that we can only compose two unary functions, f and g, that take one argument of type T:
const trim = (s: string) => s.trim();const capitalize = (s: string) => s.toUpperCase();const trimAndCapitalize = compose(trim, capitalize);const result = trimAndCapitalize(" hello world ");console.log(result); // "HELLO WORLD"
In a real-world application, ...
Read now
Unlock full access