Live data from Hacker News

JavaScript Pattern Matching Proposal

github.com

181–190 of 254 posts

Re: JavaScript Pattern Matching Proposal

#181

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.

What rankings have you been seeing? Javascript is on top of the list from what I've been seeing.

https://insights.stackoverflow.com/survey/2018/#technology

Re: JavaScript Pattern Matching Proposal

#182
post #181

Earlier quoted context omitted.

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.

What rankings have you been seeing? Javascript is on top of the list from what I've been seeing. https://insights.stackoverflow.com/survey/2018/#technology

Tiobe index for one. But I just searched Google and checked the top few.

Re: JavaScript Pattern Matching Proposal

#183

Earlier quoted context omitted.

It's certainly been my experience that there are a lot of JS developers who aren't like you, though I have met a few who are. But JS has traditionally been one of the most accessible languages. Before transpilers became common, view source allowed non-programmers to learn javascript by example. And because of it's use in the front-end/web, you saw a lot graphic designers and other non-programmers learn it to be able…

> Javascript also goes out of its way to be friendlier. While other languages complain when you try to add two different width integers, Javascript will happily add a number and a string together. It's something that drives those of us with a static typing background nuts, but makes the language more approachable overall. We have differing definitions of friendly. Since I understand types and operator overloading, I…

> I'd argue that lack of static typing makes a language tougher to learn rather than more approachable

I think it's a bit of a wolf in sheep's clothing. It makes it harder to master, but less intimidating when you're first starting out.

Also, I probably shouldn't have used the term static typing, since the issues I'm talking about are more associated with weak typing rather than dynamic typing.

Re: JavaScript Pattern Matching Proposal

#184

Looking 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.

Honestly I'm not sure the Javascript story here is much worse - Babel tends to allow you to opt in to proposed specs pretty early, without worrying about browser support because you're not shipping it as is.

The benefit is that your polyfill/conversion to something browsers support today is handled by someone else, and your actual code base can point a reader to the proposal to at least make some sense later on.

Not that I imagine many people would encourage using a stage 0 proposal, but it's probably a bit better than everyone writing their own adhoc implementation.

Re: JavaScript Pattern Matching Proposal

#185

Earlier quoted context omitted.

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.

Well said. Rambling Java code with little to no abstraction is its own kind of nightmare.

Some people are just terrified of any new abstractions, I guess, preferring to work with an endless series of tally marks, rather than these obfuscating “multiplication” and “exponent” complications (exaggerating to make a point - abstract != unintelligible).

Re: JavaScript Pattern Matching Proposal

#186
post #181

Earlier quoted context omitted.

What rankings have you been seeing? Javascript is on top of the list from what I've been seeing. https://insights.stackoverflow.com/survey/2018/#technology

Tiobe index for one. But I just searched Google and checked the top few.

The TIOBE index has always been a bit weird. If you read their note about their methodology, you'll see that it's measuring something kind of interesting, but not really what you'd call "the most used languages." For example, IIRC part of a language's TIOBE ranking is how many college courses use it.

Re: JavaScript Pattern Matching Proposal

#187
post #142

Earlier quoted context omitted.

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.

In Lisp pattern-based programming has been first implemented in 1962 in by D.Bobrow (METEOR). From then on there are many implementations of pattern matching in Lisp based software, from Planner, to rule-based systems, LISP70 (Tesler, ...)...

This makes me sad. As somebody who started a CS degree back in the early 80s (finishing in the late 80s), and who had a few glimpses of languages which didn’t suck, the switch down to “everything is an 8086 running C code, if not x86 assembler” was an incredibly destructive event in this industry.

I was going to specialize in AI in my major, but I guess it was about 25 years too early. But that’s another tangent.

Re: JavaScript Pattern Matching Proposal

#188
post #169

Earlier quoted context omitted.

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 somethin…

Wow, importing the whole lodash to use just one function? There’s es6 way of getting an array with a range of numbers:

    [...Array(100).keys()].map(v=>v+1)
A little bit longer but no dependencies.

Re: JavaScript Pattern Matching Proposal

#189
post #127

So the proposal is super confusing to me (but I hadn't seen match before). At first I thought it had something to do with regex. Essentially it's a switch variant that is (a) confusingly named 'match' (bad) (b) returns a value (good?) (c) and is actually a kind of function (wha?), since it (d) supports recursion, but (e) doesn't require break (good), (f) looks like a bag of inline functions but isn't (bad). How about…

Love this comment; reminds me of what I thought when I first encountered pattern matching. a. Yup, that's what it's called in OCaml, Scala, F#, etc. Might be a little confusing at first, but it's all about finding a "match" for your value, which is different from a guard in an "if" or a "switch". In an "if", you need to provide a boolean (or a value coercible to a boolean). In a "switch" you don't provide the boolean…

> In a "switch" you don't provide the boolean, but under the hood the switch is just iterating through and comparing your value to all cases: it computes the boolean for you by testing if two values are equal.

Switch can also be implemented as a jump table in some cases.

Re: JavaScript Pattern Matching Proposal

#190

Earlier quoted context omitted.

Let's see an example of your proposed alternative syntax.

Here you are: var x = match (response) { case { status:200 }: true; case { status: 404 }: new NotFoundError; case Number: Math.PI; case SomeClass: 1; case /^http/: "http error"; default: -1; };

A problem with that is that, in JavaScript, case clauses in switch statements fall through to the next clause, whereas they shouldn’t in match statements.

When reading the code, that means you need to hunt for the match or switch statement to know what happens at the end of a case clause.

IMO, if one is willing to correct historical errors, even if they are engrained, Apple’s Swift language makes the best choice here. It requires an explicit fallthrough to fall through to the following case, and goes even further than what you propose by only having a switch keyword (https://developer.apple.com/library/content/documentation/Sw...)

Post reply on HN