All quizzesHard
Advanced Functional Patterns
Preview — 3 of 10 questions
Which of the following best describes a transducer in JavaScript?
javascript
const numbers = [1, 2, 3, 4, 5];
const result = numbers
.filter(x => x % 2 === 0) // [2, 4] - intermediate array
.map(x => x * 2) // [4, 8] - another intermediate array
.reduce((sum, x) => sum + x, 0); // 12
// Two intermediate arrays createdAA function that reduces an array to a single value.
BA higher-order function that transforms a reducer function to improve performance.
CA function that maps an array to a new array without side effects.
DA function that composes multiple reducers together.
What is the main advantage of lazy evaluation in functional programming?
javascript
function getExpensiveValue() {
console.log("Computing...");
return Array.from({ length: 1000000 }, (_, i) => i);
}
function process(data) {
const result = data.filter(x => x > 100); // Processes all 1M items
return result.slice(0, 5); // But only returns first 5
}
process(getExpensiveValue()); // Wastes computationAIt allows immediate execution of all function calls.
BIt defers computation until the result is actually needed, improving performance.
CIt makes debugging easier by executing all functions eagerly.
DIt prevents all side effects in JavaScript.
Which of the following optimizations can improve the performance of a recursive function?
javascript
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
fibonacci(40); // Very slow! Calculates fibonacci(39), fibonacci(38), etc. multiple timesAMemoization
BUsing a for-loop instead of recursion
CCalling the function synchronously in a loop
DIncreasing the recursion depth
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.