I think first() exists so there is a semantic pair for functional programming: 
first()/head() and tail()  or init() and last()

________________________________
From: ocs@ocs <o...@ocs.cz>
Sent: Thursday, October 18, 2018 1:20 PM
To: dev@groovy.apache.org
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,
OC

On 18 Oct 2018, at 7:19 PM, Milles, Eric (TR Technology & Ops) 
<eric.mil...@thomsonreuters.com<mailto:eric.mil...@thomsonreuters.com>> wrote:

"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 <o...@ocs.cz<mailto:o...@ocs.cz>>
Sent: Thursday, October 18, 2018 12:07 PM
To: dev@groovy.apache.org<mailto:dev@groovy.apache.org>
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 
<paolo.ditomm...@gmail.com<mailto:paolo.ditomm...@gmail.com>> 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 
<sun...@apache.org<mailto:sun...@apache.org>> 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<https://urldefense.proofpoint.com/v2/url?u=http-3A__blog.sunlan.me_&d=DwMFAg&c=4ZIZThykDLcoWk-GVjSLmy8-1Cr1I4FWIvbLFebwKgY&r=tPJuIuL_GkTEazjQW7vvl7mNWVGXn3yJD5LGBHYYHww&m=eCM29fHJoKqW_CdKJO2GxdVR6VMqldnDZ9NQgYxSo08&s=Kuqjwc4Pu38Y9C4Zooo3uBjDkGvyna3lloonS2m7GTE&e=>
Twitter: @daniel_sun

--
Sent from: 
http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html<https://urldefense.proofpoint.com/v2/url?u=http-3A__groovy.329449.n5.nabble.com_Groovy-2DDev-2Df372993.html&d=DwMFAg&c=4ZIZThykDLcoWk-GVjSLmy8-1Cr1I4FWIvbLFebwKgY&r=tPJuIuL_GkTEazjQW7vvl7mNWVGXn3yJD5LGBHYYHww&m=eCM29fHJoKqW_CdKJO2GxdVR6VMqldnDZ9NQgYxSo08&s=J5vmRmzvL66tJOtxSbSidNjQHcCHKNV3t2A0OHPCgDY&e=>

Reply via email to