Live data from Hacker News

JavaScript Pattern Matching Proposal

github.com

171–180 of 254 posts

Re: JavaScript Pattern Matching Proposal

#171

I think it's good to note that this proposal is not far along the process. Pattern matching, if it clears TC39 (a tall order, to be honest), won't land in a spec for half a decade at least I would bet. So, keep the feedback coming, and don't worry that you'll have to learn this syntax tomorrow. It's still very early, and I wouldn't be surprised if there are at least a couple major revisions before anything like this…

> if it clears TC39 (a tall order, to be honest), won't land in a spec for half a decade at least I would bet.

Given current speed of iteration I think you can cut that in half. The key will be implementations - Babel plugins help with that.

Re: JavaScript Pattern Matching Proposal

#172

I think it's good to note that this proposal is not far along the process. Pattern matching, if it clears TC39 (a tall order, to be honest), won't land in a spec for half a decade at least I would bet. So, keep the feedback coming, and don't worry that you'll have to learn this syntax tomorrow. It's still very early, and I wouldn't be surprised if there are at least a couple major revisions before anything like this…

> if it clears TC39 (a tall order, to be honest), won't land in a spec for half a decade at least I would bet. Given current speed of iteration I think you can cut that in half. The key will be implementations - Babel plugins help with that.

The current speed of iteration means you get updates in smaller batches rather than more throughput. Still important, but it's not like we're letting proposals bake less. Note how so far we've only gotten two big new features post-ES6 - async functions and async generators. Further, some features (e.g. decorators, bind operator) have had babel plugins for years and yet hasn't shipped in a spec.

Re: JavaScript Pattern Matching Proposal

#173
post #143

Earlier quoted context omitted.

I don't think this snarky remark is warranted. I think the point was that in Lisp-style languages (such as Clojure/ClojureScript) new features such as pattern matching can easily be added without "changing the language", as libraries. This is how core.async was added to Clojure, to pick a non-trivial example. Or more to the point, how core.match works. Additions such as those do not have to be one lonely programmer's…

Yes, but I think the other point is well-made, as well: languages that allow this can be very difficult to work in if you have to maintain a large app with multiple contributors over a long period of time.

The same can be said for function calls, with the indirection they create hiding details of broken and side-effecting implementations.

None the less, the opposite is also true. Languages that have macro facilities can aid in writing more legible code. (See `threading` in clojure), or the `loop` macro and regular expression macros in common lisp.

Re: JavaScript Pattern Matching Proposal

#174
post #43

Earlier quoted context omitted.

In fairness I think that would look a bit more like this: const getDate = (date) => { switch (date) { case 1: return 'mon' case 2: return 'tues' default: return 'wed' } } let date = getDate(1) Which is, of course, still less terse. What I normally use when I have cases like this is an object-as-a-map. IE: const dates = { 1: 'mon', 2: 'tues' } let day = dates[3] || 'wed';

By the way, I don't think it is a good idea to write a function as a constant. Please compare this function getDate(date) { .. } to this: const getDate = (date) => { ... } The first version is more readable. We instantly see that it is a function and in a second case it looks like a constant at first. Also without the equal and arrow sign it looks simpler.

But function getDate(date) is hoisted. The const version isn't.

Re: JavaScript Pattern Matching Proposal

#175

I can only see this being a foot gun. Deep equality testing of objects is going to encourage the use of getters. Getters with side effects (no way to prevent them) will basically ruin your day if you try to use pattern matching with them. Additionally, this would be the first "native" way to do deep equality testing of objects. I can see it being abused to do simple one-off checks that could otherwise have been done…

If it is five lines of code vs. one line of code, and we believe that the number of bugs is proportional to the number of lines of code, irrespective of which language we are programming in, then this would be an obvious benefit.

And it isn't four lines saved, but four lines times the number of uses in the entire code base.

Re: JavaScript Pattern Matching Proposal

#176
post #169

Earlier quoted context omitted.

Shameless plug but here is my pattern machter [1] :D Example: // simple factorial const factorial = n => match(n) .when(0, 1) .otherwise(n => n * factorial(n - 1)); // walking a tree class Tree { constructor(left, right) { this.left = left; this.right = right; } } class Node { constructor(value) { this.value = value; } } const T = (l, r) => new Tree(l, r); const N = v => new Node(v); const walkT = t => match(t) .when…

Method-based syntax for pattern matching is easier to read and grok (IMO), but it lacks the dynamic capabilities of data-oriented syntax (in Kasai, patterns can be built/manipulated at runtime). It's interesting to compare the two approaches.

If you mean by method-based pattern matching that you evaluate a function for each 'case'. My lib can do that do:

    const match = require('pmatch-js')
    const _ = require('lodash')

    const fizzbuzz = x => match(x)
      .when(a => a % 3 == 0 && a % 5 == 0, 'fizzbuzz')
      .when(a => a % 5 == 0, 'buzz')
      .when(a => a % 3 == 0, 'fizz')
      .otherwise(a => a)

    console.log(
      _.range(1, 101).map(fizzbuzz).join(' ')
    )
But maybe you mean something totally different :-)

EDIT: Ah I think I know what you mean. Your lib takes an array of tuples to define the patterns. Sorry for the confusion.

Re: JavaScript Pattern Matching Proposal

#177

Earlier quoted context omitted.

With JavaScript being the most used language in the world I highly doubt the old stereotypes still apply

Is it the most used language in the world? Seems like I keep hearing comments like that, but the rankings I've seen don't list it that way.

It's the most visibly used language. But the world runs on a ton of code that isn't the web.

Re: JavaScript Pattern Matching Proposal

#178
post #6

Pattern matching seems to be very trendy/popular right now. It's a big jump from JS, but https://reasonml.github.io has very nice pattern matching that you can use, and it integrates with the JS ecosystem very nicely.

IIRC, a big blocker to adopting ReasonML for a lot of people is its current lack of async / await support: https://github.com/facebook/reason/issues/1321

Re: JavaScript Pattern Matching Proposal

#179

Earlier quoted context omitted.

With JavaScript being the most used language in the world I highly doubt the old stereotypes still apply

Is it the most used language in the world? Seems like I keep hearing comments like that, but the rankings I've seen don't list it that way.

At least according to Github: https://octoverse.github.com/ - it's in its own class compared to the others: 2.3m to the next most popular (1m, python)

Re: JavaScript Pattern Matching Proposal

#180

Earlier quoted context omitted.

With JavaScript being the most used language in the world I highly doubt the old stereotypes still apply

Is it the most used language in the world? Seems like I keep hearing comments like that, but the rankings I've seen don't list it that way.

According to Stackoverflow's developer survey it is [1]. Doesn't surprise me since developing for web means JavaScript.

Curious to know what rankings you are referring to though.

[1]: https://insights.stackoverflow.com/survey/2018/#technology

Post reply on HN