@beenotung/tslib
Version:
utils library in Typescript
59 lines (58 loc) • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Lazy = void 0;
/**
* @description this class is not strict type, be ware of the type <A>
* */
class Lazy {
f;
constructor(f) {
this.f = f;
}
value() {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const res = this.f();
delete this.f;
this.value = () => res;
return res;
}
map(f) {
return new Lazy(() => f(this.value()));
}
add(b) {
return this.map(a => a + b);
}
minus(b) {
return this.map(a => a - b);
}
mult(b) {
return this.map(a => a * b);
}
rem(b) {
return this.map(a => a % b);
}
div(b) {
return this.map(a => a / b);
}
quot(b) {
return this.map(a => (a / b) | 0);
}
quotRem(b) {
return this.map((a) => {
return [(a / b) | 0, a % b];
});
}
and(b) {
return this.map(a => a && b);
}
or(b) {
return this.map(a => a || b);
}
not() {
return this.map(a => !a);
}
notnot() {
return this.map(a => !!a);
}
}
exports.Lazy = Lazy;