You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gary Gregory <ga...@gmail.com> on 2018/01/10 16:45:55 UTC

[collections] New iterator/iterable methods

Hi All,

I plan on adding methods like:
- IteratorUtils.first(Iterator)
- IterableUtils.first(Iterable)

Gary

Re: [collections] New iterator/iterable methods

Posted by Claude Warren <cl...@xenei.com>.
I agree.  I think it might be valuable to suggest a peek() like method for
iterators but that is a different kettle of fish.

Jena has an ExtendedIterator that provides some extra functionality (like
mapping and filtering as well as converting to lists or sets).  In a class
like that might be handy to have a peek() method.

BTW: I submitted the ExtendedIterator and associated classes to collections
some time back but they were not accepted.  However, I am certain we could
pull them across if we desired to do so.

On Fri, Jan 12, 2018 at 7:40 PM, Matt Sicker <bo...@gmail.com> wrote:

> This use case reminds me of how both Scala and Kotlin have structured their
> collections classes. Here are some comparisons.
>
> Kotlin adds a .first() extension function for multiple collection classes:
> <
> https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html
> >
>
> Do note the lack of a .first() on Iterator.
>
> Next, we see in Scala: <
> http://www.scala-lang.org/api/2.12.3/scala/collection/Iterator.html>
>
> In this API, the method would be named "head" instead, but as you can see,
> it's missing here. However: <
> http://www.scala-lang.org/api/2.12.3/scala/collection/Iterable.html#head:A
> >.
>
> I think the idea here is that it's safe to provide a head/first function
> for reusable collections, but providing such a thing for an Iterator
> doesn't make much sense since it'll consume the first element. I've seen a
> related concept with iteratees/enumeratees for a less destructive way to
> iterate over resources in functional programming, but that's a bit harder
> to express in Java syntax AFAIK.
>
> On 12 January 2018 at 12:05, Gary Gregory <ga...@gmail.com> wrote:
>
> > first(Iterator/Iterable) is shorthand for get(Iterator/Iterable, 0), so
> the
> > Javadocs is much like get().
> >
> > My current use case is to replace:
> >
> > aSet.iterator().next()
> >
> > with:
> >
> > first(aSet)
> >
> > Gary
> >
> > On Fri, Jan 12, 2018 at 12:43 AM, Claude Warren <cl...@xenei.com>
> wrote:
> >
> > > actually last() would probably be more useful.  I still don't see the
> > need
> > > for first() when next() will suffice, unless first() is changing the
> > return
> > > value when there is no first().
> > >
> > > As clarification what happens if I call:
> > >
> > > Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> > > x = IteratorUtil.first( iterator );
> > > y = IteratorUtil.first( iterator );
> > >
> > > Is the value of y intended to be "a" or "b"?  I assume "b"
> > >
> > > how does this differ from:
> > >
> > > Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> > > x = iterator.next();
> > > y = iterator.next();
> > >
> > > If I add another call so I have:
> > >
> > > Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> > > x = IteratorUtil.first( iterator );
> > > y = IteratorUtil.first( iterator );
> > > z = IteratorUtil.first( iterator );
> > >
> > > what happens on the 3rd call?
> > >
> > > I know I get an exception in the standard case.
> > >
> > > Claude
> > >
> > > On Fri, Jan 12, 2018 at 1:21 AM, sebb <se...@gmail.com> wrote:
> > >
> > > > On 12 January 2018 at 00:51, Gary Gregory <ga...@gmail.com>
> > > wrote:
> > > > > On Thu, Jan 11, 2018 at 5:23 PM, sebb <se...@gmail.com> wrote:
> > > > >
> > > > >> On 11 January 2018 at 15:22, Gary Gregory <garydgregory@gmail.com
> >
> > > > wrote:
> > > > >> > Hi,
> > > > >> >
> > > > >> > Some APIs, either due to age or design, deal out an Iterator and
> > > > nothing
> > > > >> > else. And sometimes, all I care about (in tests, for example, or
> > if
> > > > the
> > > > >> > list is a set of aliases) is the first object.
> > > > >> >
> > > > >> > The method IteratorUtils.first(Iterator) is a shorthand for
> > > > >> > IteratorUtils.get(Iterator, 0).
> > > > >>
> > > > >> The code method is only shorter by one character and that is a
> > space.
> > > > >>
> > > > >> This will just add unnecessary code and maintenance costs.
> > > > >>
> > > > >
> > > > > I do not look at it that way. I see it at providing an abstraction
> > that
> > > > > says "give me the first element" instead of "give me the 0th
> > element".
> > > > Code
> > > > > reads better that way. IMO.
> > > >
> > > > So add a comment.
> > > > Or write your own wrapper.
> > > >
> > > > > What maintenance costs are referring to here?
> > > >
> > > > All code needs updating from time to time.
> > > >
> > > > Remember JUnit3? Javadoc pre-8?
> > > >
> > > > Are you sure that the initial commit will be perfect?
> > > > Or will the Javadoc need adjusting?
> > > >
> > > > Will there be questions asking whether first includes null or not?
> > > > Such queries may occur even if the Javadoc is perfect.
> > > >
> > > > And of course it takes a tiny bit longer to run the tests, and the
> > > > test logs are bigger.
> > > >
> > > > And it's a bit harder to navigate the source.
> > > >
> > > > etc.
> > > >
> > > >
> > > >
> > > > > Gary
> > > > >
> > > > >
> > > > >
> > > > >>
> > > > >> > I do not plan to add last(), the obvious sibling to such a
> method,
> > > > YAGNI
> > > > >> > for now.
> > > > >> >
> > > > >> > Gary
> > > > >> >
> > > > >> >
> > > > >> > On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
> > > > >> >
> > > > >> >> Also, what is the use case for such methods?
> > > > >> >> How many will there be - i.e. do you plan to add .last,
> .second,
> > > > >> .random?
> > > > >> >>
> > > > >> >> I'm not keen on methods that save a few lines of code unless
> > > there's
> > > > a
> > > > >> >> common use case and the behaviour is obvious/unambiguous from
> the
> > > > >> >> name.
> > > > >> >>
> > > > >> >> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com>
> > > wrote:
> > > > >> >> > does first return the first object or the first non-null
> > object?
> > > > >> >> >
> > > > >> >> > If the first object how do you distinguish between first()
> > > > returning a
> > > > >> >> null
> > > > >> >> > object and there being an empty container?
> > > > >> >> > If the first non-null object how do you determine that nulls
> > were
> > > > >> >> skipped?
> > > > >> >> >
> > > > >> >> > Keep in mind that the Optional implementation in Java8 will
> > throw
> > > > an
> > > > >> >> > exception if it is constructed with a null object.
> > > > >> >> >
> > > > >> >> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <
> > > > garydgregory@gmail.com
> > > > >> >
> > > > >> >> > wrote:
> > > > >> >> >
> > > > >> >> >> Hi All,
> > > > >> >> >>
> > > > >> >> >> I plan on adding methods like:
> > > > >> >> >> - IteratorUtils.first(Iterator)
> > > > >> >> >> - IterableUtils.first(Iterable)
> > > > >> >> >>
> > > > >> >> >> Gary
> > > > >> >> >>
> > > > >> >> >
> > > > >> >> >
> > > > >> >> >
> > > > >> >> > --
> > > > >> >> > I like: Like Like - The likeliest place on the web
> > > > >> >> > <http://like-like.xenei.com>
> > > > >> >> > LinkedIn: http://www.linkedin.com/in/claudewarren
> > > > >> >>
> > > > >> >> ------------------------------------------------------------
> > > > ---------
> > > > >> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > > >> >> For additional commands, e-mail: dev-help@commons.apache.org
> > > > >> >>
> > > > >> >>
> > > > >>
> > > > >> ------------------------------------------------------------
> > ---------
> > > > >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > > >> For additional commands, e-mail: dev-help@commons.apache.org
> > > > >>
> > > > >>
> > > >
> > > > ------------------------------------------------------------
> ---------
> > > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > > For additional commands, e-mail: dev-help@commons.apache.org
> > > >
> > > >
> > >
> > >
> > > --
> > > I like: Like Like - The likeliest place on the web
> > > <http://like-like.xenei.com>
> > > LinkedIn: http://www.linkedin.com/in/claudewarren
> > >
> >
>
>
>
> --
> Matt Sicker <bo...@gmail.com>
>



-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: [collections] New iterator/iterable methods

Posted by Matt Sicker <bo...@gmail.com>.
This use case reminds me of how both Scala and Kotlin have structured their
collections classes. Here are some comparisons.

Kotlin adds a .first() extension function for multiple collection classes: <
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html>

Do note the lack of a .first() on Iterator.

Next, we see in Scala: <
http://www.scala-lang.org/api/2.12.3/scala/collection/Iterator.html>

In this API, the method would be named "head" instead, but as you can see,
it's missing here. However: <
http://www.scala-lang.org/api/2.12.3/scala/collection/Iterable.html#head:A>.

I think the idea here is that it's safe to provide a head/first function
for reusable collections, but providing such a thing for an Iterator
doesn't make much sense since it'll consume the first element. I've seen a
related concept with iteratees/enumeratees for a less destructive way to
iterate over resources in functional programming, but that's a bit harder
to express in Java syntax AFAIK.

On 12 January 2018 at 12:05, Gary Gregory <ga...@gmail.com> wrote:

> first(Iterator/Iterable) is shorthand for get(Iterator/Iterable, 0), so the
> Javadocs is much like get().
>
> My current use case is to replace:
>
> aSet.iterator().next()
>
> with:
>
> first(aSet)
>
> Gary
>
> On Fri, Jan 12, 2018 at 12:43 AM, Claude Warren <cl...@xenei.com> wrote:
>
> > actually last() would probably be more useful.  I still don't see the
> need
> > for first() when next() will suffice, unless first() is changing the
> return
> > value when there is no first().
> >
> > As clarification what happens if I call:
> >
> > Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> > x = IteratorUtil.first( iterator );
> > y = IteratorUtil.first( iterator );
> >
> > Is the value of y intended to be "a" or "b"?  I assume "b"
> >
> > how does this differ from:
> >
> > Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> > x = iterator.next();
> > y = iterator.next();
> >
> > If I add another call so I have:
> >
> > Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> > x = IteratorUtil.first( iterator );
> > y = IteratorUtil.first( iterator );
> > z = IteratorUtil.first( iterator );
> >
> > what happens on the 3rd call?
> >
> > I know I get an exception in the standard case.
> >
> > Claude
> >
> > On Fri, Jan 12, 2018 at 1:21 AM, sebb <se...@gmail.com> wrote:
> >
> > > On 12 January 2018 at 00:51, Gary Gregory <ga...@gmail.com>
> > wrote:
> > > > On Thu, Jan 11, 2018 at 5:23 PM, sebb <se...@gmail.com> wrote:
> > > >
> > > >> On 11 January 2018 at 15:22, Gary Gregory <ga...@gmail.com>
> > > wrote:
> > > >> > Hi,
> > > >> >
> > > >> > Some APIs, either due to age or design, deal out an Iterator and
> > > nothing
> > > >> > else. And sometimes, all I care about (in tests, for example, or
> if
> > > the
> > > >> > list is a set of aliases) is the first object.
> > > >> >
> > > >> > The method IteratorUtils.first(Iterator) is a shorthand for
> > > >> > IteratorUtils.get(Iterator, 0).
> > > >>
> > > >> The code method is only shorter by one character and that is a
> space.
> > > >>
> > > >> This will just add unnecessary code and maintenance costs.
> > > >>
> > > >
> > > > I do not look at it that way. I see it at providing an abstraction
> that
> > > > says "give me the first element" instead of "give me the 0th
> element".
> > > Code
> > > > reads better that way. IMO.
> > >
> > > So add a comment.
> > > Or write your own wrapper.
> > >
> > > > What maintenance costs are referring to here?
> > >
> > > All code needs updating from time to time.
> > >
> > > Remember JUnit3? Javadoc pre-8?
> > >
> > > Are you sure that the initial commit will be perfect?
> > > Or will the Javadoc need adjusting?
> > >
> > > Will there be questions asking whether first includes null or not?
> > > Such queries may occur even if the Javadoc is perfect.
> > >
> > > And of course it takes a tiny bit longer to run the tests, and the
> > > test logs are bigger.
> > >
> > > And it's a bit harder to navigate the source.
> > >
> > > etc.
> > >
> > >
> > >
> > > > Gary
> > > >
> > > >
> > > >
> > > >>
> > > >> > I do not plan to add last(), the obvious sibling to such a method,
> > > YAGNI
> > > >> > for now.
> > > >> >
> > > >> > Gary
> > > >> >
> > > >> >
> > > >> > On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
> > > >> >
> > > >> >> Also, what is the use case for such methods?
> > > >> >> How many will there be - i.e. do you plan to add .last, .second,
> > > >> .random?
> > > >> >>
> > > >> >> I'm not keen on methods that save a few lines of code unless
> > there's
> > > a
> > > >> >> common use case and the behaviour is obvious/unambiguous from the
> > > >> >> name.
> > > >> >>
> > > >> >> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com>
> > wrote:
> > > >> >> > does first return the first object or the first non-null
> object?
> > > >> >> >
> > > >> >> > If the first object how do you distinguish between first()
> > > returning a
> > > >> >> null
> > > >> >> > object and there being an empty container?
> > > >> >> > If the first non-null object how do you determine that nulls
> were
> > > >> >> skipped?
> > > >> >> >
> > > >> >> > Keep in mind that the Optional implementation in Java8 will
> throw
> > > an
> > > >> >> > exception if it is constructed with a null object.
> > > >> >> >
> > > >> >> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <
> > > garydgregory@gmail.com
> > > >> >
> > > >> >> > wrote:
> > > >> >> >
> > > >> >> >> Hi All,
> > > >> >> >>
> > > >> >> >> I plan on adding methods like:
> > > >> >> >> - IteratorUtils.first(Iterator)
> > > >> >> >> - IterableUtils.first(Iterable)
> > > >> >> >>
> > > >> >> >> Gary
> > > >> >> >>
> > > >> >> >
> > > >> >> >
> > > >> >> >
> > > >> >> > --
> > > >> >> > I like: Like Like - The likeliest place on the web
> > > >> >> > <http://like-like.xenei.com>
> > > >> >> > LinkedIn: http://www.linkedin.com/in/claudewarren
> > > >> >>
> > > >> >> ------------------------------------------------------------
> > > ---------
> > > >> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > >> >> For additional commands, e-mail: dev-help@commons.apache.org
> > > >> >>
> > > >> >>
> > > >>
> > > >> ------------------------------------------------------------
> ---------
> > > >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > >> For additional commands, e-mail: dev-help@commons.apache.org
> > > >>
> > > >>
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > For additional commands, e-mail: dev-help@commons.apache.org
> > >
> > >
> >
> >
> > --
> > I like: Like Like - The likeliest place on the web
> > <http://like-like.xenei.com>
> > LinkedIn: http://www.linkedin.com/in/claudewarren
> >
>



-- 
Matt Sicker <bo...@gmail.com>

Re: [collections] New iterator/iterable methods

Posted by Gary Gregory <ga...@gmail.com>.
first(Iterator/Iterable) is shorthand for get(Iterator/Iterable, 0), so the
Javadocs is much like get().

My current use case is to replace:

aSet.iterator().next()

with:

first(aSet)

Gary

On Fri, Jan 12, 2018 at 12:43 AM, Claude Warren <cl...@xenei.com> wrote:

> actually last() would probably be more useful.  I still don't see the need
> for first() when next() will suffice, unless first() is changing the return
> value when there is no first().
>
> As clarification what happens if I call:
>
> Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> x = IteratorUtil.first( iterator );
> y = IteratorUtil.first( iterator );
>
> Is the value of y intended to be "a" or "b"?  I assume "b"
>
> how does this differ from:
>
> Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> x = iterator.next();
> y = iterator.next();
>
> If I add another call so I have:
>
> Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
> x = IteratorUtil.first( iterator );
> y = IteratorUtil.first( iterator );
> z = IteratorUtil.first( iterator );
>
> what happens on the 3rd call?
>
> I know I get an exception in the standard case.
>
> Claude
>
> On Fri, Jan 12, 2018 at 1:21 AM, sebb <se...@gmail.com> wrote:
>
> > On 12 January 2018 at 00:51, Gary Gregory <ga...@gmail.com>
> wrote:
> > > On Thu, Jan 11, 2018 at 5:23 PM, sebb <se...@gmail.com> wrote:
> > >
> > >> On 11 January 2018 at 15:22, Gary Gregory <ga...@gmail.com>
> > wrote:
> > >> > Hi,
> > >> >
> > >> > Some APIs, either due to age or design, deal out an Iterator and
> > nothing
> > >> > else. And sometimes, all I care about (in tests, for example, or if
> > the
> > >> > list is a set of aliases) is the first object.
> > >> >
> > >> > The method IteratorUtils.first(Iterator) is a shorthand for
> > >> > IteratorUtils.get(Iterator, 0).
> > >>
> > >> The code method is only shorter by one character and that is a space.
> > >>
> > >> This will just add unnecessary code and maintenance costs.
> > >>
> > >
> > > I do not look at it that way. I see it at providing an abstraction that
> > > says "give me the first element" instead of "give me the 0th element".
> > Code
> > > reads better that way. IMO.
> >
> > So add a comment.
> > Or write your own wrapper.
> >
> > > What maintenance costs are referring to here?
> >
> > All code needs updating from time to time.
> >
> > Remember JUnit3? Javadoc pre-8?
> >
> > Are you sure that the initial commit will be perfect?
> > Or will the Javadoc need adjusting?
> >
> > Will there be questions asking whether first includes null or not?
> > Such queries may occur even if the Javadoc is perfect.
> >
> > And of course it takes a tiny bit longer to run the tests, and the
> > test logs are bigger.
> >
> > And it's a bit harder to navigate the source.
> >
> > etc.
> >
> >
> >
> > > Gary
> > >
> > >
> > >
> > >>
> > >> > I do not plan to add last(), the obvious sibling to such a method,
> > YAGNI
> > >> > for now.
> > >> >
> > >> > Gary
> > >> >
> > >> >
> > >> > On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
> > >> >
> > >> >> Also, what is the use case for such methods?
> > >> >> How many will there be - i.e. do you plan to add .last, .second,
> > >> .random?
> > >> >>
> > >> >> I'm not keen on methods that save a few lines of code unless
> there's
> > a
> > >> >> common use case and the behaviour is obvious/unambiguous from the
> > >> >> name.
> > >> >>
> > >> >> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com>
> wrote:
> > >> >> > does first return the first object or the first non-null object?
> > >> >> >
> > >> >> > If the first object how do you distinguish between first()
> > returning a
> > >> >> null
> > >> >> > object and there being an empty container?
> > >> >> > If the first non-null object how do you determine that nulls were
> > >> >> skipped?
> > >> >> >
> > >> >> > Keep in mind that the Optional implementation in Java8 will throw
> > an
> > >> >> > exception if it is constructed with a null object.
> > >> >> >
> > >> >> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <
> > garydgregory@gmail.com
> > >> >
> > >> >> > wrote:
> > >> >> >
> > >> >> >> Hi All,
> > >> >> >>
> > >> >> >> I plan on adding methods like:
> > >> >> >> - IteratorUtils.first(Iterator)
> > >> >> >> - IterableUtils.first(Iterable)
> > >> >> >>
> > >> >> >> Gary
> > >> >> >>
> > >> >> >
> > >> >> >
> > >> >> >
> > >> >> > --
> > >> >> > I like: Like Like - The likeliest place on the web
> > >> >> > <http://like-like.xenei.com>
> > >> >> > LinkedIn: http://www.linkedin.com/in/claudewarren
> > >> >>
> > >> >> ------------------------------------------------------------
> > ---------
> > >> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > >> >> For additional commands, e-mail: dev-help@commons.apache.org
> > >> >>
> > >> >>
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > >> For additional commands, e-mail: dev-help@commons.apache.org
> > >>
> > >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
> >
>
>
> --
> I like: Like Like - The likeliest place on the web
> <http://like-like.xenei.com>
> LinkedIn: http://www.linkedin.com/in/claudewarren
>

Re: [collections] New iterator/iterable methods

Posted by Claude Warren <cl...@xenei.com>.
actually last() would probably be more useful.  I still don't see the need
for first() when next() will suffice, unless first() is changing the return
value when there is no first().

As clarification what happens if I call:

Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
x = IteratorUtil.first( iterator );
y = IteratorUtil.first( iterator );

Is the value of y intended to be "a" or "b"?  I assume "b"

how does this differ from:

Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
x = iterator.next();
y = iterator.next();

If I add another call so I have:

Iterator<String> iterator = Arrays.asList( "a","b" ).iterator();
x = IteratorUtil.first( iterator );
y = IteratorUtil.first( iterator );
z = IteratorUtil.first( iterator );

what happens on the 3rd call?

I know I get an exception in the standard case.

Claude

On Fri, Jan 12, 2018 at 1:21 AM, sebb <se...@gmail.com> wrote:

> On 12 January 2018 at 00:51, Gary Gregory <ga...@gmail.com> wrote:
> > On Thu, Jan 11, 2018 at 5:23 PM, sebb <se...@gmail.com> wrote:
> >
> >> On 11 January 2018 at 15:22, Gary Gregory <ga...@gmail.com>
> wrote:
> >> > Hi,
> >> >
> >> > Some APIs, either due to age or design, deal out an Iterator and
> nothing
> >> > else. And sometimes, all I care about (in tests, for example, or if
> the
> >> > list is a set of aliases) is the first object.
> >> >
> >> > The method IteratorUtils.first(Iterator) is a shorthand for
> >> > IteratorUtils.get(Iterator, 0).
> >>
> >> The code method is only shorter by one character and that is a space.
> >>
> >> This will just add unnecessary code and maintenance costs.
> >>
> >
> > I do not look at it that way. I see it at providing an abstraction that
> > says "give me the first element" instead of "give me the 0th element".
> Code
> > reads better that way. IMO.
>
> So add a comment.
> Or write your own wrapper.
>
> > What maintenance costs are referring to here?
>
> All code needs updating from time to time.
>
> Remember JUnit3? Javadoc pre-8?
>
> Are you sure that the initial commit will be perfect?
> Or will the Javadoc need adjusting?
>
> Will there be questions asking whether first includes null or not?
> Such queries may occur even if the Javadoc is perfect.
>
> And of course it takes a tiny bit longer to run the tests, and the
> test logs are bigger.
>
> And it's a bit harder to navigate the source.
>
> etc.
>
>
>
> > Gary
> >
> >
> >
> >>
> >> > I do not plan to add last(), the obvious sibling to such a method,
> YAGNI
> >> > for now.
> >> >
> >> > Gary
> >> >
> >> >
> >> > On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
> >> >
> >> >> Also, what is the use case for such methods?
> >> >> How many will there be - i.e. do you plan to add .last, .second,
> >> .random?
> >> >>
> >> >> I'm not keen on methods that save a few lines of code unless there's
> a
> >> >> common use case and the behaviour is obvious/unambiguous from the
> >> >> name.
> >> >>
> >> >> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com> wrote:
> >> >> > does first return the first object or the first non-null object?
> >> >> >
> >> >> > If the first object how do you distinguish between first()
> returning a
> >> >> null
> >> >> > object and there being an empty container?
> >> >> > If the first non-null object how do you determine that nulls were
> >> >> skipped?
> >> >> >
> >> >> > Keep in mind that the Optional implementation in Java8 will throw
> an
> >> >> > exception if it is constructed with a null object.
> >> >> >
> >> >> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <
> garydgregory@gmail.com
> >> >
> >> >> > wrote:
> >> >> >
> >> >> >> Hi All,
> >> >> >>
> >> >> >> I plan on adding methods like:
> >> >> >> - IteratorUtils.first(Iterator)
> >> >> >> - IterableUtils.first(Iterable)
> >> >> >>
> >> >> >> Gary
> >> >> >>
> >> >> >
> >> >> >
> >> >> >
> >> >> > --
> >> >> > I like: Like Like - The likeliest place on the web
> >> >> > <http://like-like.xenei.com>
> >> >> > LinkedIn: http://www.linkedin.com/in/claudewarren
> >> >>
> >> >> ------------------------------------------------------------
> ---------
> >> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> >> For additional commands, e-mail: dev-help@commons.apache.org
> >> >>
> >> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: dev-help@commons.apache.org
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>


-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: [collections] New iterator/iterable methods

Posted by sebb <se...@gmail.com>.
On 12 January 2018 at 00:51, Gary Gregory <ga...@gmail.com> wrote:
> On Thu, Jan 11, 2018 at 5:23 PM, sebb <se...@gmail.com> wrote:
>
>> On 11 January 2018 at 15:22, Gary Gregory <ga...@gmail.com> wrote:
>> > Hi,
>> >
>> > Some APIs, either due to age or design, deal out an Iterator and nothing
>> > else. And sometimes, all I care about (in tests, for example, or if the
>> > list is a set of aliases) is the first object.
>> >
>> > The method IteratorUtils.first(Iterator) is a shorthand for
>> > IteratorUtils.get(Iterator, 0).
>>
>> The code method is only shorter by one character and that is a space.
>>
>> This will just add unnecessary code and maintenance costs.
>>
>
> I do not look at it that way. I see it at providing an abstraction that
> says "give me the first element" instead of "give me the 0th element". Code
> reads better that way. IMO.

So add a comment.
Or write your own wrapper.

> What maintenance costs are referring to here?

All code needs updating from time to time.

Remember JUnit3? Javadoc pre-8?

Are you sure that the initial commit will be perfect?
Or will the Javadoc need adjusting?

Will there be questions asking whether first includes null or not?
Such queries may occur even if the Javadoc is perfect.

And of course it takes a tiny bit longer to run the tests, and the
test logs are bigger.

And it's a bit harder to navigate the source.

etc.



> Gary
>
>
>
>>
>> > I do not plan to add last(), the obvious sibling to such a method, YAGNI
>> > for now.
>> >
>> > Gary
>> >
>> >
>> > On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
>> >
>> >> Also, what is the use case for such methods?
>> >> How many will there be - i.e. do you plan to add .last, .second,
>> .random?
>> >>
>> >> I'm not keen on methods that save a few lines of code unless there's a
>> >> common use case and the behaviour is obvious/unambiguous from the
>> >> name.
>> >>
>> >> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com> wrote:
>> >> > does first return the first object or the first non-null object?
>> >> >
>> >> > If the first object how do you distinguish between first() returning a
>> >> null
>> >> > object and there being an empty container?
>> >> > If the first non-null object how do you determine that nulls were
>> >> skipped?
>> >> >
>> >> > Keep in mind that the Optional implementation in Java8 will throw an
>> >> > exception if it is constructed with a null object.
>> >> >
>> >> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <garydgregory@gmail.com
>> >
>> >> > wrote:
>> >> >
>> >> >> Hi All,
>> >> >>
>> >> >> I plan on adding methods like:
>> >> >> - IteratorUtils.first(Iterator)
>> >> >> - IterableUtils.first(Iterable)
>> >> >>
>> >> >> Gary
>> >> >>
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > I like: Like Like - The likeliest place on the web
>> >> > <http://like-like.xenei.com>
>> >> > LinkedIn: http://www.linkedin.com/in/claudewarren
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> >> For additional commands, e-mail: dev-help@commons.apache.org
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [collections] New iterator/iterable methods

Posted by Gary Gregory <ga...@gmail.com>.
On Thu, Jan 11, 2018 at 5:23 PM, sebb <se...@gmail.com> wrote:

> On 11 January 2018 at 15:22, Gary Gregory <ga...@gmail.com> wrote:
> > Hi,
> >
> > Some APIs, either due to age or design, deal out an Iterator and nothing
> > else. And sometimes, all I care about (in tests, for example, or if the
> > list is a set of aliases) is the first object.
> >
> > The method IteratorUtils.first(Iterator) is a shorthand for
> > IteratorUtils.get(Iterator, 0).
>
> The code method is only shorter by one character and that is a space.
>
> This will just add unnecessary code and maintenance costs.
>

I do not look at it that way. I see it at providing an abstraction that
says "give me the first element" instead of "give me the 0th element". Code
reads better that way. IMO.

What maintenance costs are referring to here?

Gary



>
> > I do not plan to add last(), the obvious sibling to such a method, YAGNI
> > for now.
> >
> > Gary
> >
> >
> > On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
> >
> >> Also, what is the use case for such methods?
> >> How many will there be - i.e. do you plan to add .last, .second,
> .random?
> >>
> >> I'm not keen on methods that save a few lines of code unless there's a
> >> common use case and the behaviour is obvious/unambiguous from the
> >> name.
> >>
> >> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com> wrote:
> >> > does first return the first object or the first non-null object?
> >> >
> >> > If the first object how do you distinguish between first() returning a
> >> null
> >> > object and there being an empty container?
> >> > If the first non-null object how do you determine that nulls were
> >> skipped?
> >> >
> >> > Keep in mind that the Optional implementation in Java8 will throw an
> >> > exception if it is constructed with a null object.
> >> >
> >> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <garydgregory@gmail.com
> >
> >> > wrote:
> >> >
> >> >> Hi All,
> >> >>
> >> >> I plan on adding methods like:
> >> >> - IteratorUtils.first(Iterator)
> >> >> - IterableUtils.first(Iterable)
> >> >>
> >> >> Gary
> >> >>
> >> >
> >> >
> >> >
> >> > --
> >> > I like: Like Like - The likeliest place on the web
> >> > <http://like-like.xenei.com>
> >> > LinkedIn: http://www.linkedin.com/in/claudewarren
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: dev-help@commons.apache.org
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [collections] New iterator/iterable methods

Posted by sebb <se...@gmail.com>.
On 11 January 2018 at 15:22, Gary Gregory <ga...@gmail.com> wrote:
> Hi,
>
> Some APIs, either due to age or design, deal out an Iterator and nothing
> else. And sometimes, all I care about (in tests, for example, or if the
> list is a set of aliases) is the first object.
>
> The method IteratorUtils.first(Iterator) is a shorthand for
> IteratorUtils.get(Iterator, 0).

The code method is only shorter by one character and that is a space.

This will just add unnecessary code and maintenance costs.

> I do not plan to add last(), the obvious sibling to such a method, YAGNI
> for now.
>
> Gary
>
>
> On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
>
>> Also, what is the use case for such methods?
>> How many will there be - i.e. do you plan to add .last, .second, .random?
>>
>> I'm not keen on methods that save a few lines of code unless there's a
>> common use case and the behaviour is obvious/unambiguous from the
>> name.
>>
>> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com> wrote:
>> > does first return the first object or the first non-null object?
>> >
>> > If the first object how do you distinguish between first() returning a
>> null
>> > object and there being an empty container?
>> > If the first non-null object how do you determine that nulls were
>> skipped?
>> >
>> > Keep in mind that the Optional implementation in Java8 will throw an
>> > exception if it is constructed with a null object.
>> >
>> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <ga...@gmail.com>
>> > wrote:
>> >
>> >> Hi All,
>> >>
>> >> I plan on adding methods like:
>> >> - IteratorUtils.first(Iterator)
>> >> - IterableUtils.first(Iterable)
>> >>
>> >> Gary
>> >>
>> >
>> >
>> >
>> > --
>> > I like: Like Like - The likeliest place on the web
>> > <http://like-like.xenei.com>
>> > LinkedIn: http://www.linkedin.com/in/claudewarren
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [collections] New iterator/iterable methods

Posted by Claude Warren <cl...@xenei.com>.
for test cases I tend to use iterator.next() to get the first item.  It
will fail spetacularly if the iterator has no next() and if it does you
have first().  No need for extra functions.

Claude

On Thu, Jan 11, 2018 at 3:22 PM, Gary Gregory <ga...@gmail.com>
wrote:

> Hi,
>
> Some APIs, either due to age or design, deal out an Iterator and nothing
> else. And sometimes, all I care about (in tests, for example, or if the
> list is a set of aliases) is the first object.
>
> The method IteratorUtils.first(Iterator) is a shorthand for
> IteratorUtils.get(Iterator, 0).
>
> I do not plan to add last(), the obvious sibling to such a method, YAGNI
> for now.
>
> Gary
>
>
> On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:
>
> > Also, what is the use case for such methods?
> > How many will there be - i.e. do you plan to add .last, .second, .random?
> >
> > I'm not keen on methods that save a few lines of code unless there's a
> > common use case and the behaviour is obvious/unambiguous from the
> > name.
> >
> > On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com> wrote:
> > > does first return the first object or the first non-null object?
> > >
> > > If the first object how do you distinguish between first() returning a
> > null
> > > object and there being an empty container?
> > > If the first non-null object how do you determine that nulls were
> > skipped?
> > >
> > > Keep in mind that the Optional implementation in Java8 will throw an
> > > exception if it is constructed with a null object.
> > >
> > > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <ga...@gmail.com>
> > > wrote:
> > >
> > >> Hi All,
> > >>
> > >> I plan on adding methods like:
> > >> - IteratorUtils.first(Iterator)
> > >> - IterableUtils.first(Iterable)
> > >>
> > >> Gary
> > >>
> > >
> > >
> > >
> > > --
> > > I like: Like Like - The likeliest place on the web
> > > <http://like-like.xenei.com>
> > > LinkedIn: http://www.linkedin.com/in/claudewarren
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
> >
>



-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: [collections] New iterator/iterable methods

Posted by Gary Gregory <ga...@gmail.com>.
Hi,

Some APIs, either due to age or design, deal out an Iterator and nothing
else. And sometimes, all I care about (in tests, for example, or if the
list is a set of aliases) is the first object.

The method IteratorUtils.first(Iterator) is a shorthand for
IteratorUtils.get(Iterator, 0).

I do not plan to add last(), the obvious sibling to such a method, YAGNI
for now.

Gary


On Thu, Jan 11, 2018 at 2:52 AM, sebb <se...@gmail.com> wrote:

> Also, what is the use case for such methods?
> How many will there be - i.e. do you plan to add .last, .second, .random?
>
> I'm not keen on methods that save a few lines of code unless there's a
> common use case and the behaviour is obvious/unambiguous from the
> name.
>
> On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com> wrote:
> > does first return the first object or the first non-null object?
> >
> > If the first object how do you distinguish between first() returning a
> null
> > object and there being an empty container?
> > If the first non-null object how do you determine that nulls were
> skipped?
> >
> > Keep in mind that the Optional implementation in Java8 will throw an
> > exception if it is constructed with a null object.
> >
> > On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <ga...@gmail.com>
> > wrote:
> >
> >> Hi All,
> >>
> >> I plan on adding methods like:
> >> - IteratorUtils.first(Iterator)
> >> - IterableUtils.first(Iterable)
> >>
> >> Gary
> >>
> >
> >
> >
> > --
> > I like: Like Like - The likeliest place on the web
> > <http://like-like.xenei.com>
> > LinkedIn: http://www.linkedin.com/in/claudewarren
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [collections] New iterator/iterable methods

Posted by sebb <se...@gmail.com>.
Also, what is the use case for such methods?
How many will there be - i.e. do you plan to add .last, .second, .random?

I'm not keen on methods that save a few lines of code unless there's a
common use case and the behaviour is obvious/unambiguous from the
name.

On 11 January 2018 at 07:45, Claude Warren <cl...@xenei.com> wrote:
> does first return the first object or the first non-null object?
>
> If the first object how do you distinguish between first() returning a null
> object and there being an empty container?
> If the first non-null object how do you determine that nulls were skipped?
>
> Keep in mind that the Optional implementation in Java8 will throw an
> exception if it is constructed with a null object.
>
> On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <ga...@gmail.com>
> wrote:
>
>> Hi All,
>>
>> I plan on adding methods like:
>> - IteratorUtils.first(Iterator)
>> - IterableUtils.first(Iterable)
>>
>> Gary
>>
>
>
>
> --
> I like: Like Like - The likeliest place on the web
> <http://like-like.xenei.com>
> LinkedIn: http://www.linkedin.com/in/claudewarren

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [collections] New iterator/iterable methods

Posted by Claude Warren <cl...@xenei.com>.
does first return the first object or the first non-null object?

If the first object how do you distinguish between first() returning a null
object and there being an empty container?
If the first non-null object how do you determine that nulls were skipped?

Keep in mind that the Optional implementation in Java8 will throw an
exception if it is constructed with a null object.

On Wed, Jan 10, 2018 at 4:45 PM, Gary Gregory <ga...@gmail.com>
wrote:

> Hi All,
>
> I plan on adding methods like:
> - IteratorUtils.first(Iterator)
> - IterableUtils.first(Iterable)
>
> Gary
>



-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren