[0,'',[],[:]].find()?:'not quite what you wanted here' [0,1,2].find()?:'nor in this case'
Actually, far as my experience can say, there's precisely one reason to have first/last, and that is that they won't throw, but return a null on an empty list. If they do not, there's no point in having those at all, for indices can be used instead just as well. All the best, OC > On 18 Oct 2018, at 7:52 PM, Mario Garcia <mario.g...@gmail.com> wrote: > > 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) > (<eric.mil...@thomsonreuters.com <mailto:eric.mil...@thomsonreuters.com>>) > 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: dev@groovy.apache.org <mailto:dev@groovy.apache.org> > 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 <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=> >