DGM for first or default

classic Classic list List threaded Threaded
19 messages Options
12
Reply | Threaded
Open this post in threaded view
|

DGM for first or default

Milles, Eric (TR Tech, Content & Ops)

I see there are the following DGMs for getting first element of a "collection":

  static <T> T first(T[] self)

  static <T> T first(List<T> self)

  static <T> T first(Iterable<T> self)


Is there a simple sequence for getting the first element or a default value if the "collection" is empty?  If not, may I propose the addition of the following DGMs:

  static <T> T first(T[] self, T defaultValue)

  static <T> T first(List<T> self, T defaultValue)

  static <T> T first(Iterable<T> self, T defaultValue)

  static <T> T first(T[] self, Closure<T> defaultSupplier)
  static <T> T first(List<T> self, Closure<T> defaultSupplier)

  static <T> T first(Iterable<T> self, Closure<T> defaultSupplier)



Eric M.

Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

Daniel.Sun
+0 from me.
P.S. we should add similar DGM for `last` too?

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me 
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

Paolo Di Tommaso
-1, it can be easily done as: 

list.first() ?: defaultValue


p

On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun <[hidden email]> wrote:
+0 from me.
P.S. we should add similar DGM for `last` too?

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

mauromol
In reply to this post by Milles, Eric (TR Tech, Content & Ops)
Isn't it equivalent to something like this?

def elem = collection?.first()?: defaultValue
def elem = collection?.first()?: defaultSupplier()

Mauro

Il 18/10/2018 17:39, Milles, Eric (TR Technology & Ops) ha scritto:

I see there are the following DGMs for getting first element of a "collection":

  static <T> T first(T[] self)

  static <T> T first(List<T> self)

  static <T> T first(Iterable<T> self)


Is there a simple sequence for getting the first element or a default value if the "collection" is empty?  If not, may I propose the addition of the following DGMs:

  static <T> T first(T[] self, T defaultValue)

  static <T> T first(List<T> self, T defaultValue)

  static <T> T first(Iterable<T> self, T defaultValue)

  static <T> T first(T[] self, Closure<T> defaultSupplier)
  static <T> T first(List<T> self, Closure<T> defaultSupplier)

  static <T> T first(Iterable<T> self, Closure<T> defaultSupplier)



Eric M.


Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

ocs@ocs
In reply to this post by Paolo Di Tommaso
Myself, I am not a huge fan of adding not-often-needed functionalities (and actually would add almost none of those discussed lately); nevertheless...

On 18 Oct 2018, at 6:48 PM, Paolo Di Tommaso <[hidden email]> wrote:

-1, it can be easily done as: 
list.first() ?: defaultValue

... this won't work in case the first object is a Groovy False (e.g., an empty string, or a plethora of others).

All the best,
OC



p

On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun <[hidden email]> wrote:
+0 from me.
P.S. we should add similar DGM for `last` too?

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

ocs@ocs
In reply to this post by Milles, Eric (TR Tech, Content & Ops)
P.S. Oh, and when I am writing anyway — please, do not abuse the “overloaded” methods which differ just by their argument lists (myself, I consider them always at best suspicious; mostly plain wrong). Instead of the suggestion below, if something like that is accepted, it would be much better to change the method name appropriately (say, e.g., firstWithDefault, firstWithSupplier). Or perhaps named arguments might be (consistently!) used for these things (e.g., “first(default:value)” or “first(supplier:value)”).

Consider e.g., an array of closures, of which one would want to get the first one with a default closure... with the definitions suggested below that's going to be fun!

All the best,
OC

On 18 Oct 2018, at 5:39 PM, Milles, Eric (TR Technology & Ops) <[hidden email]> wrote:

I see there are the following DGMs for getting first element of a "collection":

  static <T> T first(T[] self)
  static <T> T first(List<T> self)
  static <T> T first(Iterable<T> self)


Is there a simple sequence for getting the first element or a default value if the "collection" is empty?  If not, may I propose the addition of the following DGMs:

  static <T> T first(T[] self, T defaultValue)
  static <T> T first(List<T> self, T defaultValue)

  static <T> T first(Iterable<T> self, T defaultValue)

  static <T> T first(T[] self, Closure<T> defaultSupplier)
  static <T> T first(List<T> self, Closure<T> defaultSupplier)

  static <T> T first(Iterable<T> self, Closure<T> defaultSupplier)


Eric M.

Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

Milles, Eric (TR Tech, Content & Ops)
In reply to this post by ocs@ocs

"list?.first() ?: defaultValue" is not the equivalent.  If the collection is empty, first() throws an IndexOutOfBoundsException is thrown.  That's why I'm asking if there is a simple equivalent.  I suppose this is the equivalent now that I think about it:


list ? list.first() : defaultValue




From: ocs@ocs <[hidden email]>
Sent: Thursday, October 18, 2018 12:07 PM
To: [hidden email]
Subject: Re: DGM for first or default
 
Myself, I am not a huge fan of adding not-often-needed functionalities (and actually would add almost none of those discussed lately); nevertheless...

On 18 Oct 2018, at 6:48 PM, Paolo Di Tommaso <[hidden email]> wrote:

-1, it can be easily done as: 
list.first() ?: defaultValue

... this won't work in case the first object is a Groovy False (e.g., an empty string, or a plethora of others).

All the best,
OC



p

On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun <[hidden email]> wrote:
+0 from me.
P.S. we should add similar DGM for `last` too?

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

Milles, Eric (TR Tech, Content & Ops)

Is it still valuable to have DGMs -- whether named "first()" or "firstOrDefault()" or whatever?  Content assist does not propose "list ? list.first() : defaultValue".  I suppose I'd need to create a code template to get that proposal in the IDE.


Are there any other small idioms like this that anyone has added as a template to improve the editing experience?


From: Milles, Eric (TR Technology & Ops)
Sent: Thursday, October 18, 2018 12:19:42 PM
To: [hidden email]
Subject: Re: DGM for first or default
 

"list?.first() ?: defaultValue" is not the equivalent.  If the collection is empty, first() throws an IndexOutOfBoundsException is thrown.  That's why I'm asking if there is a simple equivalent.  I suppose this is the equivalent now that I think about it:


list ? list.first() : defaultValue




From: ocs@ocs <[hidden email]>
Sent: Thursday, October 18, 2018 12:07 PM
To: [hidden email]
Subject: Re: DGM for first or default
 
Myself, I am not a huge fan of adding not-often-needed functionalities (and actually would add almost none of those discussed lately); nevertheless...

On 18 Oct 2018, at 6:48 PM, Paolo Di Tommaso <[hidden email]> wrote:

-1, it can be easily done as: 
list.first() ?: defaultValue

... this won't work in case the first object is a Groovy False (e.g., an empty string, or a plethora of others).

All the best,
OC



p

On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun <[hidden email]> wrote:
+0 from me.
P.S. we should add similar DGM for `last` too?

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

Paolo Di Tommaso
In reply to this post by ocs@ocs
ouch.. true! if so: 

list ? list.first() : defaultValue


p
 







On Thu, Oct 18, 2018 at 7:07 PM ocs@ocs <[hidden email]> wrote:
Myself, I am not a huge fan of adding not-often-needed functionalities (and actually would add almost none of those discussed lately); nevertheless...

On 18 Oct 2018, at 6:48 PM, Paolo Di Tommaso <[hidden email]> wrote:

-1, it can be easily done as: 
list.first() ?: defaultValue

... this won't work in case the first object is a Groovy False (e.g., an empty string, or a plethora of others).

All the best,
OC



p

On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun <[hidden email]> wrote:
+0 from me.
P.S. we should add similar DGM for `last` too?

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Reply | Threaded
Open this post in threaded view
|

Re: DGM for first or default

Mario Garcia
In reply to this post by Milles, Eric (TR Tech, Content & Ops)
Eric you can use `find`:

list.find() ?: defaultValue

The method find with no arguments takes the first element, and if the collection is empty or null it will return null and you won't get an IndexOutOfBounds

Regards
Mario

El jue., 18 oct. 2018 a las 19:32, Milles, Eric (TR Technology & Ops) (<[hidden email]>) escribió:

Is it still valuable to have DGMs -- whether named "first()" or "firstOrDefault()" or whatever?  Content assist does not propose "list ? list.first() : defaultValue".  I suppose I'd need to create a code template to get that proposal in the IDE.


Are there any other small idioms like this that anyone has added as a template to improve the editing experience?


From: Milles, Eric (TR Technology & Ops)
Sent: Thursday, October 18, 2018 12:19:42 PM
To: [hidden email]
Subject: Re: DGM for first or default
 

"list?.first() ?: defaultValue" is not the equivalent.  If the collection is empty, first() throws an IndexOutOfBoundsException is thrown.  That's why I'm asking if there is a simple equivalent.  I suppose this is the equivalent now that I think about it:


list ? list.first() : defaultValue




From: ocs@ocs <[hidden email]>
Sent: Thursday, October 18, 2018 12:07 PM
To: [hidden email]
Subject: Re: DGM for first or default
 
Myself, I am not a huge fan of adding not-often-needed functionalities (and actually would add almost none of those discussed lately); nevertheless...

On 18 Oct 2018, at 6:48 PM, Paolo Di Tommaso <[hidden email]> wrote:

-1, it can be easily done as: 
list.first() ?: defaultValue

... this won't work in case the first object is a Groovy False (e.g., an empty string, or a plethora of others).

All the best,
OC



p

On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun <[hidden email]> wrote:
+0 from me.
P.S. we should add similar DGM for `last` too?

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

12