[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=17318539#comment-17318539 ] Dario Arena commented on GROOVY-10025: -- [~abdulsubahan] GINQ is very nice, i didn't know that this was coming in Groovy 4. There is even an entry in the changelog under "New Features" but i missed it. :P (maybe when Groovy 4 will come out it would need more "highlighting"). [~emilles] These are all very good ways to implement the "every statement is indeed an expression". I was thinking however of a more direct support from the language itself without doing the extra step of implementing these constructs. Maybe this could be an idea to consider for a future version of Groovy (5.0) > "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)
[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=17318526#comment-17318526 ] Eric Milles commented on GROOVY-10025: -- 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)
[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=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)