You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (Jira)" <ji...@apache.org> on 2021/04/10 10:07:00 UTC

[jira] [Commented] (GROOVY-10025) "Assimilate" expressions from other languages (Scala, Python...)

    [ https://issues.apache.org/jira/browse/GROOVY-10025?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17318467#comment-17318467 ] 

Daniel Sun commented on GROOVY-10025:
-------------------------------------

As for the list comprehension, Groovy 4 supports it via GINQ:

{code:groovy}
def prices = GQL { from stock in stocks where stock.market == "NASDAQ" select getPriceFor(stock) }
{code}

See https://docs.groovy-lang.org/docs/next/html/documentation/#_querying_collections_in_sql_like_style

> "Assimilate" expressions from other languages (Scala, Python...)
> ----------------------------------------------------------------
>
>                 Key: GROOVY-10025
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10025
>             Project: Groovy
>          Issue Type: Improvement
>          Components: Compiler
>    Affects Versions: 4.0.0-alpha-2
>            Reporter: Dario Arena
>            Priority: Minor
>              Labels: expression, for, if, semantic, statement, switch-statement, syntax
>
> In some other languages (Scala, Python, Haskell, Java 14 switches) every statement like if..else, for, switch... is indeed an expression that returns a value (usually the last one computed is implicitly returned as in Groovy).
> Would it be possible to include something like conditional expression, for comprehensions and switch expressions as a native groovy feature? I think it would simplify writing DSLs and maybe in some cases making groovy code neater and readable
> Using closures it is possible to write "conditional expressions" without using the conditional operator, something like:
> {quote}{{def pillToTake = \{ if(candidate.name == "Neo") "red" else "blue" }.call()}}{quote}
> but maybe if groovy supports expression this could be written as
> {quote}{{def pillToTake = if candidate.name == "Neo" then "red" else "blue"}}{quote}
> or maybe using switch expression to include more complex cases
> {quote}def trainingSchedule = switch(date) \{
>     case \{ isHoliday(it) }: return ['Rest']   
>     case  \{ date.dayOfWeek in [MONDAY, FRIDAY] }: return ['Run 15m', '40 Push-Ups', '20 Crunches']   
>     case  \{date.dayOfWeek.is(WEDNESDAY) }: return ['Run 10m', '50 Tractions', '30 Squats']   
>     default: return ['Run 30m']
> }{quote}
> I've not really dug deep in how the groovy compiler works but maybe recognizing these expression, "wrapping" them in a closure and calling that closure could be done automatically?
> For comprehensions maybe are a little bit more tricky to implement as a native groovy feature and using .collect(..) the code is equally readable, but maybe in some cases one could prefer to use a different construct
> Actual groovy code:
> {quote}def prices = stocks.filter \{ it.market == "NASDAQ"}.collect\{ getPriceFor(it) \}{quote}
> Using for comprehension:
> {quote}def prices = [getPriceFor(stock) for stock in stocks if stock.market == "NASDAQ"]{quote}
> It is just a matter of preference which one is more readable but this could simplify the "flow" of the code in some cases.
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)