views:

500

answers:

6

Hi,

Is metaprogramming possible in javascript??

During my routine work, i happened to write the chained javascript function which is something like LINQ expression to query the JSON result.

var Result = from(obj1).as("x").where("x.id=5").groupby("x.status").having(count("x.status") > 5).select("x.status");

It works perfectly and give the expected result..

I was wondering this looks awsome if the code is written like this (in a more readable way)

var Result = from obj1 as x where x.status
groupby x.status having count(x.status)  > 5
select x.status;

is there a way to achieve this??

Cheers

Ramesh Vel

+10  A: 

No. JavaScript doesn't support this.

But this looks quite good too:

var Result =  from(obj1)
             .as("x")
             .where("x.id=5")
             .groupby("x.status")
             .having(count("x.status") > 5)
             .select("x.status");
Georg
A: 

Maybe you want something like JSONPath if you've got JSON data. I found this at http://www.json.org/. Lots of other tools linked to from there if it's not exactly what you need.

(this is being worked on as well: http://docs.dojocampus.org/dojox/json/query)

Glenn
+3  A: 

although not quite what you wanted, it is possible to write parsers in javascript, and just parse the query (stored as strings) and then execute it. e.g.,using libraries like http://jscc.jmksf.com/ (no doubt there are others out there) it shouldnt be too hard to implement.

but what you have in the question looks great already, i m not sure why you'd want it to look the way you suggested.

Chii
thanks for the reply Chii.. Actually i was just experimenting the different possibilities. i am with c# background, recentlty started in javascript and just wanted to explore the JS features... thats the reason.. And thanks for the link.. I have already look around the JS parsers like the one you have mentioned.. and i found http://www.codeplex.com/jsinq is useful.. thanks again..
Ramesh Vel
+1  A: 

Wat you want is to change the javascript parser into an SQL parser. It wasn't created to do that, the javascript syntax doesn't allow you to.

What you have is 90% like SQL (it maps straight onto it), and a 100% valid javascript, which is a great achievement. My answer to the question in the title is: YES, metaprogramming is possible, but NO it won't give you an SQL parser, since it's bound to use javascript grammar.

xtofl
+2  A: 

Most people insist on trying to metaprogram from inside their favorite language. That doesn't work if the language doesn't support metaprogramming well; other answers have observed that JavaScript does not.

A way around this is to do metaprogramming from outside the language, using program transformation tools. Such tools can parse source code, and carry out arbitrary transformations on it (that's what metaprogramming does anyway) and then spit the revised program.

If you have a general purpose program transformation system, that can parse arbitrary languages, you can then do metaprogramming on/with whatever language you like. See our DMS Software Reengineering Toolkit for such a tool, that has robust front ends for C, C++, Java, C#, COBOL, PHP, and ECMAScript and a number of other programming langauges, and has been used for metaprogramming on all of these.

In your case, you want to extend the JavaScript grammar with new syntax for SQL queries, and then transform them to plain JavaScript. (This is a lot like Intentional Programming) DMS will easily let you build a JavaScript dialect with additional rules, and then you can use its program transformation capabilities to produce the equivalent standard Javascript.

Having said, that, I'm not a great fan of "custom syntax for every programmer on the planet" which is where Intentional Programming leads IMHO.

This is a good thing to do if there is a large community of users that would find this valuable. This idea may or may not be one of them; part of the problem is you don't get to find out without doing the experiment, and it might fail to gain enough social traction to matter.

Ira Baxter
+2  A: 

Well, in your code sample:

var Result = from(obj1)
            .as("x")
            .where("x.id=5")
            .groupby("x.status")
            .having(count("x.status") > 5)
            .select("x.status");

The only problem I see (other than select used as an identifier) is that you embed a predicate as a function argument. You'd have to make it a function instead:

            .having(function(x){ return x.status > 5; })

JavaScript has closures and dynamic typing, so you can do some really nifty and elegant things in it. Just letting you know.

Joey Adams
thank Joey, thats a good idea.. :)
Ramesh Vel