Questions
4.1 Must return?A simple, almost philosophical question: must pure functions always return something? Could you have a pure function that doesn’t return anything?
4.2 Well-specified return: What would have happened if we had added the return type definition to maxStrings()
?
const maxStrings = (a: string[]): string => a.sort().pop();
4.3 Go for a closure: As suggested in the Memoization section, use a closure to avoid needing a global cache
array for the optimized fib2()
function.
4.4 Minimalistic function: Functional programmers sometimes write code in a minimalistic way. Can you examine the following version of the Fibonacci function and explain whether it works, and if so, how?
// fibonacci.ts const fib3 = (n: number): number => n < 2 ? n : fib2(n - 2) + fib2(n - 1);
4.5 A cheaper way: The following version of the Fibonacci function is quite efficient, doesn’t require memoization or caching, and doesn’t require...