Chapter 7, Transforming Functions – Currying and Partial Application
7.1 Hard by hand: It would work; sum(3) returns a function with 3 already bound; sum(3)() returns the same, and sum(3)()(5) produces the result.
7.2 Sum as you will: The following sumMany() function does the job:
const sumMany = (total: number) => (value?: number) => value === undefined ? total : sumMany(total + value); sumMany(2)(2)(9)(6)(0)(-3)(); // 16
In JavaScript, the function poses no problem; with TypeScript, we’ll get an objection because it cannot determine that sumMany(2) is a function, not a number.
A small detail: can you fix the function so sumMany() will return 0?
7.3 Curry with eval? Let’s see this in JavaScript first:
// curryByEval.js
function curryByEval(fn) {
return eval(`${range(0, fn.length)
.map((i) => `x${i}`)
.join("=>")} => ${fn.name}(${range(0, fn.length...