4 ms·
Sadly, a little known construct from javascript that I've fallen in love with is the closure. I feel like it is an easier way of expressing the idea of a genera
by gravypod 11y ago
Sadly, a little known construct from javascript that I've fallen in love with is the closure. I feel like it is an easier way of expressing the idea of a generator.
var fib = (function () {
let current = 0;
let next = 1;
return function (increment) {
for (var i = 0; i < increment; i++) {
[current, next] = [next, current + next]
}
return current
};
})();
This is an example of an implementation of a Fibonacci sequence as shown in this post. The same optimizations still apply, but I think this is nicer (in my opinion) then using yield.
Edit: Altogether, good show. I enjoyed the post.
- bruxis 11y agoa little known construct from javascript [...] is the closure o_O
- gravypod 11y agoPlease see my response to cyphar.
- cyphar 11y agoClosures predate JavaScript by at least 30 years (it's a construct in Lambda calculus which was being discovered in the 60s). Lisp had closures before many of us were born.
- gravypod 11y agoBy from I mean easily represented within. Most people don't think of a function with an inherited scope as a closure since by the standard PLT people will tell you that Javascript does not provide "real" closures. I'm sure you know what I meant. It's the "Javascript Way" TM of doing this. My "little known" I mean that most programmers do not think of doing this out right. It took me seeing an amazing talk by the creator of JSON called "Javascript the Good Parts" to realize how powerful this construct was, and how Javascript made it so easy.
- onion2k 11y agoPeople who write JavaScript in the sense of "jQuery with a few native calls", embedded in a webpage, don't tend to use closures, so searching for JS examples doesn't often throw them up. That might give you the impression they're unusual, but if you look at any substantial app you'll see them a lot. In fact, you can take a function closure and turn it in to a JS module that can be used with asynchronous loaders and dependency injection without much extra work - http://ifandelse.com/its-not-hard-making-your-library-support-amd-and-commonjs/ http://ifandelse.com/its-not-hard-making-your-library-suppor...
- dionidium 11y agoI'm going to push back a little further on this. Closures are fundamental to JS and widely known about and used. Crockford, too, is famous in JS circles, as is the talk you mention.
- user51442 10y agoIt took quite a while for Lisp to get "proper" lexical closures - not until Scheme & CL in the 1980s really, see, for example: http://www.paulgraham.com/thist.html http://www.paulgraham.com/thist.html
- dvlsg 11y agoClosures aside, a somewhat significant difference between the two is that the generator would yield each value in the sequence, whereas yours would just return a single value from the sequence. Which might be perfect, depending on the use case, but they're not quite the same.
- symbiosis 10y agoThis is just custom iterator/generator. Saw many like this before Babel. The point is that ES2015 defines clear interface for iterating over iterables. With help of Symbol and *. If you would use this code in production, probably you would document it: ... returns iterator function. next = fib() next() // 0 next() // 1 next() // 1 ... But if you would use Symbol.iterator then you can do for (f of fib()) console.log(f) or use gathering, spreading etc..
- gravypod 10y agoWhile I do agree generators offer some amazing features, I think they also offer some complexity that we are currently not ready to handle. from a given for loop: for (x in function()) Please tell me when this for loop will end. You cannot. If you instead did... next = fib() while (true) { number = next(); } You are instantly presented for every case in which this loop will terminate. Now don't get me wrong, I DO fully agree that a generator is the correct method for doing this, I'm just pointing out another construct to allow you to do this.