Skip to content

Instantly share code, notes, and snippets.

@freddi301
Created February 27, 2018 11:03
Show Gist options
  • Select an option

  • Save freddi301/0cda96506d82a00b01f488321c4632fe to your computer and use it in GitHub Desktop.

Select an option

Save freddi301/0cda96506d82a00b01f488321c4632fe to your computer and use it in GitHub Desktop.
singleton
export function singleton<T>(builder: () => T): () => T {
let getter = () => {
const value = builder();
getter = () => value;
return value;
}
return () => getter()
}
const x = singleton(() => { console.log("creating 5"); return 5 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment