Good point OC:
[0,'',[],[:]].find()?:'not quite what you wanted here'
[0,1,2].find()?:'nor in this case'
The more I think on this the more I think is an interesting topic. I fully
understand your frustration with first(), but apart from the example with
Cocoa you mentioned, looking in the JVM it
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
To: dev@groovy.apache.org
Subject: Re: DGM for first or default
Well I thought
[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
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,
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)
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
"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
ouch.. true! if so:
list ? list.first() : defaultValue
p
On Thu, Oct 18, 2018 at 7:07 PM ocs@ocs 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
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
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
> wrote:
>
> -1, it can be easily done as:
> list.first() ?: defaultValue
... this won't work
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
-1, it can be easily done as:
list.first() ?: defaultValue
p
On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun 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
>
+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
I see there are the following DGMs for getting first element of a "collection":
static T first(T[] self)
static T first(List self)
static T first(Iterable self)
Is there a simple sequence for getting the first element or a default value if
the "collection" is empty? If not, may I
14 matches
Mail list logo