Last active
October 15, 2018 14:01
-
-
Save akramsaouri/20f8769806bb251c4375bb811d94a8d7 to your computer and use it in GitHub Desktop.
Lazy evaluation in JavaScript
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
function Let(f) { | |
let called = false | |
let v = null | |
return function () { | |
if(called) return v | |
console.log('evaluating.') | |
v = f() | |
called = true | |
return v | |
} | |
} | |
const n = Let(function() { return 23 }) | |
console.log(n()) | |
console.log(n() + 42) | |
console.log(n() - 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A close inmplementation in Ruby