Earlier quoted context omitted.
I totally get what it does, even if I hadn't seen it before. What I don't get is (a) the use of the word 'match' (that's unfamiliarity) and (b) the proposed syntax which looks like an absolute mess, something worthy of PHP, and the fact that we end up with something that isn't a function but is recursive. Or something. 'x => x = 1' in Javascript, is an expression that resolves to a function. I do understand the diffe…
Apparently they're overloading the meaning of the => "fat arrow" in the proposed syntax. I agree it's confusing because it's already used with lambdas in case of JS, but many languages with pattern matching do use it (eg. Scala, Rust). Other languages like Haskell and F# use the thin arrow -> instead. Pattern matching can be exhaustive or non-exhaustive . Basically, if a match construct is going to be an expression i…
JavaScript Pattern Matching Proposal
211–220 of 254 posts
Re: JavaScript Pattern Matching Proposal
#212Earlier quoted context omitted.
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.
> we believe that the number of bugs is proportional to the number of lines of code Why would you believe that? And if you do believe that why don't you use a code golfing language?
Should someone now have to point out to you that a million-line function is worse than a ten-line function for calculating fizzbuzz? Is that meaningful discourse in your book?
Re: JavaScript Pattern Matching Proposal
#213Earlier quoted context omitted.
This is a Stage 0 proposal. Basically it means somebody with a connection to the TC39 (the committee in charge of the JavaScript standard) has thought this was a good enough idea to put together into a proposal. In terms of chances of standardization, that's a step up from "Hey, I have a great idea", buy there are plenty of Stage 0 proposals that go nowhere because they lose steam. I think the metric they look for is…
Even Stage 4 isn't granted it will end up on the standard, some proposals have died on Stage 4.
The closest thing I can think of is ES6 modules, because the module loader spec wasn't finished, but the syntax still lived on.
Now, stuff getting kicked down from Stage 3, that has happened.
Re: JavaScript Pattern Matching Proposal
#214Earlier quoted context omitted.
I think that this is not always guaranteed to work, because {first: $, middle: $, last: $} is the same as {first: $, last: $, middle: $} ... so calling Object.keys() is not necessarily going to use a consistent ordering. I think that is why they have to use the more verbose syntax in the other library. I think you could do something like: {first: $.0, middle: $.1, last: $.2}, (f, m, l) => ... pretty easily, though.
I believe in es6 Object literals are guaranteed to keep their ordering, and Object.keys() will iterate through them in the order defined.
Re: JavaScript Pattern Matching Proposal
#215Earlier quoted context omitted.
I think you're a super anomalous member of any programming language's community. I doubt even one programmer in a thousand has written F# professionally (not being hipster here — I haven't done it myself).
a LOT of function programmers end up doing a lot of javascript work. I don't think he is odd in any way.
Re: JavaScript Pattern Matching Proposal
#216I would prefer `match { ... } (value)`, with the `match` keyword essentially creating a function that does the matching when it invoked. Minor seeming change, but then you can think of match as being a generalization of arrow functions instead of a special new language construct (i.e., `(...args) => ...` is just shorthand for `match { ...args => ... }`) I can understand why though they went with similar syntax to a s…
Re: JavaScript Pattern Matching Proposal
#217Looking at this as someone who's been writing a lot of ClojureScript lately, I'm reminded of how nice it is to be writing in a Lisp: if I felt this was the right syntactic construct for a common-enough problem in my codebase, I'd write a macro for it and get on with my life without having to wait for it to trickle through committees, compilers, and browser implementations.
Re: JavaScript Pattern Matching Proposal
#218 let visFilter = 'foo';
const newState = match (action) {
{type: 'set-visibility-filter', filter: visFilter} => console.log(visFilter)
}
What happens?Is it checking if action.type === 'set-visibility-filter' && action.filter === 'foo' or is it destructuring the value of action.filter into visFilter?
Re: JavaScript Pattern Matching Proposal
#219If I do the following: let visFilter = 'foo'; const newState = match (action) { {type: 'set-visibility-filter', filter: visFilter} => console.log(visFilter) } What happens? Is it checking if action.type === 'set-visibility-filter' && action.filter === 'foo' or is it destructuring the value of action.filter into visFilter?
Re: JavaScript Pattern Matching Proposal
#220Earlier quoted context omitted.
Yeah, this applies to all that old Lisp features.
AFAIK pattern-matching is not a historical Lisp feature, it's usually been limited to simple destructuring. In the modern acception of tree patterns (as opposed to text patterns aka regular expressions), I guess it comes from ML (and possibly prolog but prolog's unification goes even further?): it doesn't look like ISWIM had tree patterns and I can't find older references.
Aside from full packages like what lispm mentioned, implementing pattern matching (and later, full Prolog-style unification) in Lisp is a very common exercise in beginner Lisp textbooks, going back decades.