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

[jira] [Comment Edited] (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=17318526#comment-17318526 ] 

Eric Milles edited comment on GROOVY-10025 at 4/10/21, 3:15 PM:
----------------------------------------------------------------

In the case of {{findAll}} followed by {{collect}}, you can use {{findResults}}:
{code:groovy}
def prices = stocks.findResults { if (it.market == 'NASDAQ') getPriceFor(it) }
{code}

The ternary operator has been around to provide an expression solution.
{code:groovy}
def pillToTake = (candidate.name == 'Neo' ? 'red' : 'blue')
{code}

You can use command syntax to build a solution similar to what you proposed as long as you stay away from language keywords.
{code:groovy}
def ifX(@NamedParams([@NamedParam("then"), @NamedParam("orElse")]) Map branches, boolean condition) {
  //...
}
def pillToTake = ifX candidate.name == 'Neo', then: 'red', orElse: 'blue'
{code}

There are ways to write this that use chaining so you don't need colons or commas.

The {{switch}} expression should be coming soon to maintain Java compatibility.


was (Author: emilles):
In the case of {{findAll}} followed by {{collect}}, you can use {{findResults}}:
{code:groovy}
def prices = stocks.findResults { if (it.market == 'NASDAQ') getPriceFor(it) }
{code}

The ternary operator has been around to provide an expression solution.
{code:groovy}
def pillToTake = (candidate.name == 'Neo' ? 'red' : 'blue')
{code}

You can use command syntax to build a solution similar to what you proposed as long as you stay away from language keywords.
{code:groovy}
def ifX(@NamedParams([@NamedParam("then"), @NamedParam("orElse")]) Map branches, boolean condition) {
  //...
}
ifX candidate.name == 'Neo', then: 'red', orElse: 'blue'
{code}

There are ways to write this that use chaining so you don't need colons or commas.

The {{switch}} expression should be coming soon to maintain Java compatibility.

> "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)