January 2019
Beginner
210 pages
4h 47m
English
Many functional programming languages feature lazy-evaluated APIs. The idea behind lazy evaluation is that operations are not computed until doing so can no longer be postponed. The following example declares a function that allows us to find an element in an array. When the function is invoked, we don't filter the array. Instead, we declare a proxy and a handler:
function lazyFind<T>(arr: T[], filter: (i: T) => boolean): T { let hero: T | null = null; const proxy = new Proxy( {}, { get: (obj, prop) => { console.log("Filtering..."); if (!hero) { hero = arr.find(filter) || null; } return hero ? (hero as any)[prop] : null; } } ); return proxy as any;}
It is only later, when one of the properties in the result is accessed, that the ...
Read now
Unlock full access