Last active
December 9, 2023 02:43
-
-
Save forivall/6636f8e032450168a61240527184879e to your computer and use it in GitHub Desktop.
Fastest and smallest once wrapper in js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const unit = <T>(value: T) => () => value; | |
const lazy = <T>(fn: () => T) => { | |
let f = (): T => (f = unit(fn()))(); | |
return () => f(); | |
}; | |
const lazyWithReset = <T>(fn: () => T) => { | |
let f: () => T; | |
const reset = () => { | |
f = () => (f = unit(fn()))(); | |
} | |
reset(); | |
return Object.assign(() => f(), { reset }); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment