News for hackers. Stuff that matters.
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by gravypod on Monday May 16, 2016 @11:14PM (#11711164)
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.

Re:Things I Learned Writing a Fibonacci Generator in JavaScript by bruxis on Monday May 16, 2016 @11:42PM (#11711232)
a little known construct from javascript [...] is the closure

o_O

Re:Things I Learned Writing a Fibonacci Generator in JavaScript by gravypod on Tuesday May 17, 2016 @12:08AM (#11711297)
Please see my response to cyphar.
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by cyphar on Monday May 16, 2016 @11:57PM (#11711274)
Closures 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.
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by gravypod on Tuesday May 17, 2016 @12:08AM (#11711294)
By 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.

Re:Things I Learned Writing a Fibonacci Generator in JavaScript by dionidium on Tuesday May 17, 2016 @02:36AM (#11711712)
I'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.
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by onion2k on Tuesday May 17, 2016 @01:48AM (#11711562)
People 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-suppor...
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by user51442 on Tuesday May 17, 2016 @07:06AM (#11712569)
It 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

Re:Things I Learned Writing a Fibonacci Generator in JavaScript by symbiosis on Tuesday May 17, 2016 @06:46AM (#11712497)
This 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..
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by gravypod on Tuesday May 17, 2016 @03:35PM (#11716628)
While 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.

Re:Things I Learned Writing a Fibonacci Generator in JavaScript by dvlsg on Tuesday May 17, 2016 @02:38AM (#11711717)
Closures 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.
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by PeCaN on Tuesday May 17, 2016 @03:31PM (#11716596)
No, that would return all of them in sequence via successive calls to the returned function. It's just a ghetto generator anyway.
Re:Things I Learned Writing a Fibonacci Generator in JavaScript by gravypod on Tuesday May 17, 2016 @03:35PM (#11716633)
Please see my response to symbiosis