Thursday, April 23rd, 2009

myPatterns: XPath / RegEx for JS Objects

Category: JavaScript, Library

Nic Volanschi pointed us to myPatterns:

myPatterns is a new library adding custom notations for data structures in JS (and also C). It’s really useful for writing elegant programs using general pattern matching.

To explain myPatterns, let’s introduce a standard JavaScript object:

  1. {
  2.   name: { firstname: "John", lastname: "Smith" },
  3.   children: [
  4.     { firstname: "Eric", gender: "male", born: 1991 },
  5.     { firstname: "Deborah", gender: "female", born: 1996 }
  6.   ]
  7. }

Using myPattern, you can write a test to check if this object represents a person whose first child is a boy 18 years old:

  1. (s = match(x, "{name:{lastname: %l}, children:[{gender: 'male', born: %b} | %x]}"))
  2. && new Date().getFullYear() - s.b == 18

The website explains the pattern above:

In the above, the match() statement both checks whether the object has a certain form (e.g. that the children field is an array containing a first element of a given form), and returns a “substitution” object s having the following value: {l: “Smith”, b: 1991, x: [ {firstname: “Deborah” , born: 1996} ]}.

Furthermore, using your own notations, you could write the same condition more concisely, and with your own personal touch, for example:

  1. s = match(x, "< %first ~ %last: [boy(18) | %rest]>")

In the above, the object is noted in a more concise way, and the age of the first son is directly specified in the pattern, as if it was stored in the object, taking advantage of active patterns to compute the age on the fly.

Neat!

Posted by Ben Galbraith at 8:00 am
5 Comments

++---
2.6 rating from 38 votes

5 Comments »

Comments feed TrackBack URI

Did you mean XPath instead of XSL for the title? I can see XPath. I can’t see XSL.

Comment by RobKoberg — April 23, 2009

Looks like a cloning of XPath and LINQ…

Comment by ThomasHansen — April 23, 2009

Cool. More pattern matching for the masses!

2 Thomas Hansen

Pattern matching is a general term, and XPath is just one application of it. Don’t know about LINQ, though.

Comment by chiaroscuro — April 23, 2009

I don’t see any reason to learn to this, when XPath already works natively on many languages and seems to be cleaner?

Comment by paziek — April 24, 2009

2 paziek

XPath is verbose and limited to XML (which is an overkill for many applications). You can’t express certain patterns in XPath 1.0 (for example, try to match adjacent siblings). XPath 2.0 is more expressive, but it’s insanely complicated.

If you have worked with XML DOM, I think you will agree that it’s tedious and error-prone. (Consider comparing DOM traversal through DOM methods and via XPath.) If you can somehow map XML to native data structures and your language supports pattern matching, that’s a double win.

Guess what, this kind of thing is made available to you via MyPatterns and JSON. :)

Comment by chiaroscuro — April 24, 2009

Leave a comment

You must be logged in to post a comment.