Re: [nodejs] [ANN] missmatch - pattern matching for JavaScript

172 views
Skip to first unread message

Joshua Gross

unread,
Jul 21, 2012, 2:00:04 AM7/21/12
to nod...@googlegroups.com
Peter, this is real cool! Sadly I don't expect it to gain much traffic short-term because people don't know how awesome pattern-matching is. I'm pumped to have this available in JS though; will check it out seriously. 

Joshua Gross
Christian / SpanDeX, Inc. / BA of Computer Science, UW-Madison 2013



On 15 July 2012 08:21, pb82 <peter...@gmx.net> wrote:
Hello,

i've created a module named 'missmatch'. It allows to match arbitrarily nested JavaScript values against patterns and bind values in those patterns to variables (which can be used in a handler function). It's inspired by the built-in pattern matching of languages like Haskell or OCaml. It's written purely in JavaScript and can be used with node.js or in a browser. The pattern compiler does not use eval (or the Function constructor).

It exports three functions, match, matchJSON and compile (which lets you compile a pattern to a matching-function). There is a README and some usage examples on the repo:


It can be installed with:

npm install missmatch
(optional) npm test missmatch

Hope someone finds this useful :)

Regards,
Peter

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Nuno Job

unread,
Jul 21, 2012, 5:42:49 AM7/21/12
to nod...@googlegroups.com
Creating your own pattern matching language seems over kill. Also why not just do it in plain old javascript?


Not perfect, but it works.

Nuno

Mark Hahn

unread,
Jul 21, 2012, 3:10:29 PM7/21/12
to nod...@googlegroups.com
Looks awesome.  It is kind of regex on a higher level.  It would be cool if the pattern matching went all the way down to real regexes.

people don't know how awesome pattern-matching is 

I didn't before but I can definitely see how it will be useful.  I've done this the hard way many times.  One thing that pops to mind immediately is returns from APIs like from Amazon.

Creating your own pattern matching language seems over kill. 

I don't understand.  How can you match a pattern that isn't specified?  I tried to understand your code without luck, at least in my five minutes of trying.

Joshua Gross

unread,
Jul 21, 2012, 4:24:56 PM7/21/12
to nod...@googlegroups.com
I disagree that it's overkill. It's less than 1k lines (of heavily commented code), well tested, and you can "compile" it to save computation time. It works really well, too. +1

-- Joshua Gross
Christian / Web Development Consultant / BA Candidate of Computer Science, UW-Madison 2013

Nuno Job

unread,
Jul 21, 2012, 8:02:21 PM7/21/12
to nod...@googlegroups.com
1 thousands lines of code? wow.

I appreciate the work and thought put into this, but pattern does the same thing (and in my opinion in a more elegant way) in 30 loc. (i.e. https://github.com/dscape/p/blob/master/pattern.js)

i also have a hard time with people saying a string for pattern matching is elegant. wondering if anyone here ever used haskell :)

nuno

Joshua Gross

unread,
Jul 21, 2012, 8:33:32 PM7/21/12
to nod...@googlegroups.com
I have no idea what dscape/p/ is trying to achieve. Missmatch looks and feels more like Haskell or OCaml to me, though your comment on strings is fair. (if you can explain those 30 LOCs and why I'd enjoy using them, go for it :))

Also, UNDER 1k including HEAVY commenting, to be fair. 


-- Joshua Gross
Christian / Web Development Consultant / BA Candidate of Computer Science, UW-Madison 2013

Nuno Job

unread,
Jul 21, 2012, 8:42:16 PM7/21/12
to nod...@googlegroups.com, nod...@googlegroups.com
Check the samples. Maybe hof.js.

Read me is crap, but pull requests are welcomed!

If you want a real functional language that compiles to js check roy or fay.

Sent from my iPhone

Nuno Job

unread,
Jul 21, 2012, 8:45:08 PM7/21/12
to Nuno Job, nod...@googlegroups.com
The objective was to investigate if it was possible to implement pattern matching in js without changing the language (eg compiling strings)

Nuno

Sent from my iPhone

Marak Squires

unread,
Jul 21, 2012, 9:25:47 PM7/21/12
to nod...@googlegroups.com
Can you show me a real world example of when this would be useful?

The whole thing seems a bit overcomplicated for JS.

pb82

unread,
Jul 22, 2012, 8:20:39 AM7/22/12
to nod...@googlegroups.com
yes i have used pattern matching in Haskell and yes, it's a whole different story when it's built into the language. My intention was to come as close to that in JavaScript: usage should be easy and obvious. I have started with a pure JavaScript implementation (without compiling strings) but wasn't overwhelmed with the result. So i came to the conclusion that compiling strings is my way-to-go. That does of course not mean that there are no different or better solutions.

Tim Stewart

unread,
Jul 23, 2012, 10:35:26 PM7/23/12
to nod...@googlegroups.com
Well done Peter. Looks great.
Reply all
Reply to author
Forward
0 new messages