In .NET, there is the Lazy<T>
type which for example is useful for implementing lazy loading and caching. I'm unaware of an equivalent solution for TypeScript, so I rolled my own.
export interface Factory<TResult> { () : TResult; }
export class Lazy<T> {
factoryOutput : T;
isValueSet : boolean;
constructor(private factory : Factory<T>) { }
get value() {
if (!this.isValueSet) {
this.factoryOutput = this.factory();
this.isValueSet = true;
}
return this.factoryOutput;
}
}
Having to implement it myself makes me wonder:
Lazy<T>
in TypeScript?You don't need a class. You can just use a function with a closure.
function lazy<T>(factory: () => NonNullable<T>) {
let value: T | undefined;
return () => value ?? (value = factory());
}
Use it like this:
var lazyHello = lazy(() => {
console.debug("hello factory called");
return "hello";
});
var lazyWorld = lazy(() => {
console.debug("world factory called");
return "world";
});
for(var counter = 0; counter < 5; counter++) {
console.debug(lazyHello());
console.debug(lazyWorld());
}
The factory will only be called once.
This can be simply done by declaring a Lazy<T>
type as a function return a value with type T
, like this:
type Lazy<T> = () => T;
You can then continue to implement lazy data types, this could be a LazyList
implementation (supporting an infinite amount of elements):
type LazyList<T> = {
head: Lazy<T>,
tail: Lazy<LazyList<T>>
} | null
where null represents an empty list.
Then you can create a range
function:
function range(min: number = 0, max: number = Infinity) {
if (max <= min) return null;
else return {
head: () => head;
tail: () => range(min+1, max);
}
}
This list type is similar to the one in Haskell, where every list is structured like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With