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 <garydgreg...@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 <cla...@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 <seb...@gmail.com> wrote: > > > > > On 12 January 2018 at 00:51, Gary Gregory <garydgreg...@gmail.com> > > wrote: > > > > On Thu, Jan 11, 2018 at 5:23 PM, sebb <seb...@gmail.com> wrote: > > > > > > > >> On 11 January 2018 at 15:22, Gary Gregory <garydgreg...@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 <seb...@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 <cla...@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 < > > > garydgreg...@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-unsubscr...@commons.apache.org > > > >> >> For additional commands, e-mail: dev-h...@commons.apache.org > > > >> >> > > > >> >> > > > >> > > > >> ------------------------------------------------------------ > --------- > > > >> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > > > >> For additional commands, e-mail: dev-h...@commons.apache.org > > > >> > > > >> > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > > > For additional commands, e-mail: dev-h...@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 <boa...@gmail.com>