groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Milles, Eric (TR Technology & Ops)" <>
Subject Re: DGM for first or default
Date Thu, 18 Oct 2018 18:27:26 GMT
I think first() exists so there is a semantic pair for functional programming: first()/head()
and tail()  or init() and last()

From: ocs@ocs <>
Sent: Thursday, October 18, 2018 1:20 PM
Subject: Re: DGM for first or default

Well I thought first is smart enough to return null for an empty list, same as my firstObject
in Cocoa does. If it throws, what's on earth point of having the thing at all? In that case
it can be replaced by list[0] without any drawback at all.

All the best,

On 18 Oct 2018, at 7:19 PM, Milles, Eric (TR Technology & Ops) <<>>

"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 <<>>
Sent: Thursday, October 18, 2018 12:07 PM
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 <<>>

-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,


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


Daniel Sun
Apache Groovy committer
Twitter: @daniel_sun

Sent from:<>

View raw message