Pattern matching?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Pattern matching?

Henrik Sommerland
Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!
--
Henrik "TheGrandmother" Sommerland

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

Sebastian Malton
Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.

Sebastian 

Sent: June 30, 2017 9:55 AM
Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!
--
Henrik "TheGrandmother" Sommerland

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

Henrik Sommerland
In reply to this post by Henrik Sommerland
Yes :) 

This is roughly what I had in mind:

match obj with 
  {a, b : 5} -> return a + b
  {a, c : {d, e : true}} -> return a + d
  'rof' -> return 'lol'

Where the first clause matches if obj has the property a and the property b that has the value 5.
The second clause works the same but recursively.
And the third simply checks if obj is equal to 'rof'.


Den 30 juni 2017 16:46 skrev "Sebastian Malton" <[hidden email]>:
Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.

Sebastian 

Sent: June 30, 2017 9:55 AM
Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!
--
Henrik "TheGrandmother" Sommerland


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

Sebastian Malton
A question though, would this be equivalent to == or === matching for specific values? 

Namely does {b: 5} match with a passed in object that has {b:'5'}?

Sebastian 

Sent: June 30, 2017 11:22 AM
Subject: Re: Pattern matching?

Yes :) 

This is roughly what I had in mind:

match obj with 
  {a, b : 5} -> return a + b
  {a, c : {d, e : true}} -> return a + d
  'rof' -> return 'lol'

Where the first clause matches if obj has the property a and the property b that has the value 5.
The second clause works the same but recursively.
And the third simply checks if obj is equal to 'rof'.


Den 30 juni 2017 16:46 skrev "Sebastian Malton" <[hidden email]>:
Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.

Sebastian 

Sent: June 30, 2017 9:55 AM
Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!
--
Henrik "TheGrandmother" Sommerland


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

Henrik Sommerland
In reply to this post by Henrik Sommerland
I would pick === without a doubt.
One may consider providing some form of optional syntax to do this like {b ~: 5} but I think that the safe way should be the default way.
If the decision is made to support both strict comparisons and the silly ones (I'm a bit biased :p ) there could beseveral ways to fix this.

Den 30 juni 2017 17:26 skrev "Sebastian Malton" <[hidden email]>:
A question though, would this be equivalent to == or === matching for specific values? 

Namely does {b: 5} match with a passed in object that has {b:'5'}?

Sebastian 

Sent: June 30, 2017 11:22 AM
Subject: Re: Pattern matching?

Yes :) 

This is roughly what I had in mind:

match obj with 
  {a, b : 5} -> return a + b
  {a, c : {d, e : true}} -> return a + d
  'rof' -> return 'lol'

Where the first clause matches if obj has the property a and the property b that has the value 5.
The second clause works the same but recursively.
And the third simply checks if obj is equal to 'rof'.


Den 30 juni 2017 16:46 skrev "Sebastian Malton" <[hidden email]>:
Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.

Sebastian 

Sent: June 30, 2017 9:55 AM
Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!
--
Henrik "TheGrandmother" Sommerland


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

kai zhu
In reply to this post by Sebastian Malton

-1.  will pattern matching code written by the vast number of beginner javascript programmers be readable/maintainable? i personally think not.  yet another duplicative feature (that can probably be done more maintainably by switch statements or the all-powerful String.prototype.replace) to further muddle the confusing state of "best coding-practices" for javascript.

-kai

On Jun 30, 2017 10:46 PM, "Sebastian Malton" <[hidden email]> wrote:
>
> Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.
>
> Sebastian 
>
> From: [hidden email]
> Sent: June 30, 2017 9:55 AM
> To: [hidden email]
> Subject: Pattern matching?
>
> Why is there no support for pattern matching in JavaScript?
> I would guess that it has been proposed before, and if so why hasn't it been included?
>
> I have been thinking about it and I think it would integrate nicely into JavaScript.
> If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.
>
> Peace!
> --
> Henrik "TheGrandmother" Sommerland
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

Andreas Rossberg-4
In reply to this post by Henrik Sommerland
On 30 June 2017 at 15:55, Henrik Sommerland <[hidden email]> wrote:
Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Long ago there was a proposal:



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

Jordan Harband
If it only does `===`, then it's not much better than switch.

It would be interesting to see a pattern matching proposal that was extensible/customizable, perhaps via a well-known Symbol method.

On Fri, Jun 30, 2017 at 9:06 AM, Andreas Rossberg <[hidden email]> wrote:
On 30 June 2017 at 15:55, Henrik Sommerland <[hidden email]> wrote:
Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Long ago there was a proposal:



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Pattern matching?

Alex Deas
In reply to this post by Sebastian Malton
RE: Pattern matching? I like pattern matching but don't think it would work well for ES, although I'd love to see a proposal that proves me wrong. My main concerns would be:
  • What are you matching on? Constructor, `instanceof`, duck typing, prototype etc
  • What happens when one type can be coerced to another.
  • What happens in the case that the matching identifier doesn't exist? - such as with `Object.create(null, {})`

Alex


-----Original message-----
From: Sebastian Malton
Sent: Monday, July 3 2017, 12:44 pm
To: Henrik Sommerland; es-discuss
Subject: Re: Pattern matching?

Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.

Sebastian 

Sent: June 30, 2017 9:55 AM
Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!
--
Henrik "TheGrandmother" Sommerland
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pattern matching?

Isiah Meadows-2
For those of you still wanting to follow the current state of things, check out the proposal's repo [1], and the issue tracking my alternate proposal [2].


-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com

On Mon, Jul 3, 2017 at 9:03 AM, Alex Deas <[hidden email]> wrote:
I like pattern matching but don't think it would work well for ES, although I'd love to see a proposal that proves me wrong. My main concerns would be:
  • What are you matching on? Constructor, `instanceof`, duck typing, prototype etc
  • What happens when one type can be coerced to another.
  • What happens in the case that the matching identifier doesn't exist? - such as with `Object.create(null, {})`

Alex


-----Original message-----
From: Sebastian Malton
Sent: Monday, July 3 2017, 12:44 pm
To: Henrik Sommerland; es-discuss
Subject: Re: Pattern matching?

Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.

Sebastian 

Sent: June 30, 2017 9:55 AM
Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript?
I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript.
If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!
--
Henrik "TheGrandmother" Sommerland
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Loading...