no-context / moo

Optimised tokenizer/lexer generator! 🐄 Uses /y for performance. Moo.
BSD 3-Clause "New" or "Revised" License
833 stars 66 forks source link

Replace itt example with modern js example #147

Closed AlansCodeLog closed 4 years ago

AlansCodeLog commented 4 years ago

I noticed the link to the itt package in the readme doesn't work. It's not wrong though, the author just seems to have deleted the repo from github. Not sure what happened, but mapping an array to an object can be easily done with modern js without adding dependencies, so I just replaced the example.

nathan commented 4 years ago

You replaced the wrong example; your code should replace the one under "Keyword Types," not the one under "Iteration." You could also just change the itt link to npm.im/itt.

If you want to replace the iteration example with modern JS, you'd have to do something like this:

function* ahead(n, xs) {
  const it = xs[Symbol.iterator]()
  let value, done, buf = []
  while (buf.length < n && ({value, done} = it.next()) && !done) {
    buf.push(value)
  }
  while (!done && ({value, done} = it.next()), !done ? buf.push(value) : buf.length) {
    yield buf
    buf = buf.slice(1)
  }
}
for (const [here, next] of ahead(1, lexer)) ...

which is pretty much just a golfier version of the source code for lookahead().

There's actually a typo in the iteration example anyway (it should be of, not =).

Also IMHO "modern javascript" sounds pretentious; you can just say

Object.fromEntries can help you construct repetitive keyword objects:

Object.fromEntries(['class', 'def', 'if'].map(k => ['kw-' + k, k]))

and anyone with a head on her shoulders can translate that into reduce if she wants to support older environments.

AlansCodeLog commented 4 years ago

Oops, sorry about that! Must of searched for itt and pasted into the wrong place. I meant to replace the keyword example. I've updated the commit with your suggestions. Also updated the remaining itt link which I did not see was there and fixed the equal sign in that example. I think in that case where the alternative is not so short, relying on a package is not a bad idea.

Any reason why you removed it from github? It's just it gives the feeling of the package being unmaintained (given the npm page still links to the non-existent repo).