Skip to content

Instantly share code, notes, and snippets.

@jfet97
Last active December 5, 2020 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfet97/ed4c7466555608dd0b114febc72bc33b to your computer and use it in GitHub Desktop.
Save jfet97/ed4c7466555608dd0b114febc72bc33b to your computer and use it in GitHub Desktop.
Functional Ambient in JS
function bind(ambient, name, value) {
return function lookup(n) {
if(n == name) {
return value
} else {
return ambient(n)
}
}
}
function generateAmbient() {
return bind(() => void 0)
}
const defaultAmbient = generateAmbient()
ambient1 = bind(defaultAmbient, "x", 5)
ambient2 = bind(ambient1, "y", 7)
ambient3 = bind(ambient2, "z", 4)
ambient3("x") // 5
ambient3("y") // 7
ambient3("z") // 4
ambient3("w") // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment