About LINQ-like DSL for Groovy

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

About LINQ-like DSL for Groovy

Daniel Sun
Hi all,

       Although Groovy and Java 8+ can handle data structure easily, it is still less easier and readable than LINQ IMHO.
       I tried to implement it at the language level and found too many keywords will be introduced to Groovy, so I think LINQ-like DSL will be more feasible. e.g.

        def nums = [0, 1, 2, 3]

        // command chain expression can not span rows, so we can use closure to wrap expressions  
        def numQuery =  linq {    // numQuery is an Iterator<Integer>
            from num of nums       // in is a keyword of groovy, so use of instead. Maybe some other word is better?
            where num % 2 == 0
            select num
         }

         for (num in numQuery) { /* do with the result */ }

Note: linq, from, of, where, select are all methods and others are their parameters. Beside the basic use case, joining, grouping etc. are all in the TODO list :)

        The DSL will be a big work, it will last long. Before I find some time to working on it, I wish you like the LINQ-like DSL idea, although it is not mature enough for the time being ;)


Cheers,
Daniel.Sun
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About LINQ-like DSL for Groovy

Guillaume Laforge
Administrator
I did something similar in the Gaelyk DSL for accessing the datastore. Our command chain expressions make things pretty readable and SQL like. Now it would be good to also see how it looks like for non trivial examples, like complex where clauses, sub-queries and the like. 

Le 16 juil. 2017 4:02 PM, "Daniel Sun" <[hidden email]> a écrit :
Hi all,

       Although Groovy and Java 8+ can handle data structure easily, it is
still less easier and readable than LINQ IMHO.
       I tried to implement it at the language level and found too many
keywords will be introduced to Groovy, so I think LINQ-like DSL will be more
feasible. e.g.

        def nums = [0, 1, 2, 3]

        // command chain expression can not span rows, so we can use closure
to wrap expressions
        def numQuery =  linq {    // numQuery is an Iterator<Integer>
            from num of nums       // in is a keyword of groovy, so use of
instead. Maybe some other word is better?
            where num % 2 == 0
            select num
         }

         for (num in numQuery) { /* do with the result */ }

Note: linq, from, of, where, select are all methods and others are their
parameters. Beside the basic use case, joining, grouping etc. are all in the
TODO list :)

        The DSL will be a big work, it will last long. Before I find some
time to working on it, I wish you like the LINQ-like DSL idea, although it
is not mature enough for the time being ;)


Cheers,
Daniel.Sun



--
View this message in context: http://groovy.329449.n5.nabble.com/About-LINQ-like-DSL-for-Groovy-tp5742132.html
Sent from the Groovy Dev mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About LINQ-like DSL for Groovy

Daniel Sun
Hi  Guillaume,

      Looking forward to your sharing experiences with us ;)
      I create a JIRA issue to record ideas:
https://issues.apache.org/jira/browse/GROOVY-8258

Cheers,
Daniel.Sun
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About LINQ-like DSL for Groovy

Guillaume Laforge
Administrator
You can see some examples here:

But it's just concatenations of AND's.

In Grails, they went further, allowing combinations of clause conditions:

Guillaume


On Sun, Jul 16, 2017 at 6:26 PM, Daniel Sun <[hidden email]> wrote:
Hi  Guillaume,

      Looking forward to your sharing experiences with us ;)
      I create a JIRA issue to record ideas:
https://issues.apache.org/jira/browse/GROOVY-8258

Cheers,
Daniel.Sun



--
View this message in context: http://groovy.329449.n5.nabble.com/About-LINQ-like-DSL-for-Groovy-tp5742132p5742135.html
Sent from the Groovy Dev mailing list archive at Nabble.com.



--
Guillaume Laforge
Apache Groovy committer & PMC Vice-President
Developer Advocate @ Google Cloud Platform

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About LINQ-like DSL for Groovy

Daniel Sun
nice :)
Loading...