Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {Iterables, Iterators}.{dropWhile, takeWhile} #619

Closed
gissuebot opened this issue Oct 31, 2014 · 6 comments
Closed

Add {Iterables, Iterators}.{dropWhile, takeWhile} #619

gissuebot opened this issue Oct 31, 2014 · 6 comments

Comments

@gissuebot
Copy link

gissuebot commented Oct 31, 2014

Original issue created by j...@nwsnet.de on 2011-05-11 at 01:42 PM


I miss methods to skip/retrieve the first elements of an Iterable according to a predicate (i.e. the predicate is used to determine the index at which an Iterable is partitioned, then the first/second partition is returned).

On a side note: I'd welcome a wiki page which maps Guava's methods and their names to common names in order to help people easily find methods (also to find them missing, as in this case) under names they know (e.g. from various programming languages or libraries).

E.g. (what I can recall from Guava, Haskell, Scala, Python, and Ruby):
  * any, exists
  * all, forall
  * filter, select
  * transform, map, collect
  * fold*, reduce*, inject
  * find
  * limit, take
  * takeWhile
  * skip, drop
  * dropWhile
  * concat, chain
  * zip
  * partition, grouped
  * slice
  * uniqueIndex, groupBy
etc.

Some references:
  * [http://www.cse.unsw.edu.au/~en1000/haskell/hof.html]
  * [http://www.scala-lang.org/api/current/scala/collection/Iterable.html]
  * [http://docs.python.org/library/itertools.html#module-itertools]
  * [http://code.google.com/p/fluentjava/wiki/EnumerationProtocol]
  * [http://wiki.squeak.org/squeak/uploads/SqueakClassesRef.html#Enumerating%20Collections]
  * [http://www.ruby-doc.org/core/]

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by j...@nwsnet.de on 2011-05-11 at 01:46 PM


Hm, the lists worked well when tested with the [http://fossil.wanderinghorse.net/demos/wikiwym/GoCoWi-previewer.html Wikiym] live preview. Maybe someone privileged can fix them to regain readability?

Also, the side note got quite long. Might be a good idea to copy it to a separate ticket if there is interest in it.

@gissuebot
Copy link
Author

Original comment posted by gscerbak on 2011-05-11 at 04:26 PM


I thought before about such a list (how come you missed out the Lisp?:)...), but I think most of the time it would be quite misleading. If you take a source e.g. in Haskell, translate it using such a list, the result might not behave as the Haskell version. I think the details of guava implementation and Java semantics make it specific enough for a library userto take the effort of learning what is what and how it works.

@gissuebot
Copy link
Author

Original comment posted by ogregoire on 2011-05-11 at 04:33 PM


I think it's identical to issue 477.

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by j...@nwsnet.de on 2011-05-11 at 07:13 PM


gscerbak:
I thought about Lisp, but couldn't remember its naming and hoped my list would already cover them ;)

The list is not meant to offer drop-in replacements, but as a kick-starter for new Guava users with a sufficient background in dealing with collections/iterables. Looking closer is always a must; fortunately, the Guava docs are quite insightful, detailed, and prepare against pitfalls.

ogregoire:
Indeed (I even posted there), this is a duplicate and can be closed.

So you're reponsible for that (to my knowledge) pretty uncommon naming of skip and limit? ;)

@gissuebot
Copy link
Author

Original comment posted by cgdecker on 2011-05-11 at 08:18 PM


@yo.gi: I think names like "skip" and "limit" were chosen because they're good names for those methods. Precedent from other languages isn't a good enough reason to use a name, particularly if that name doesn't actually fit well with the behavior of a method. The names "take" and "drop", for example, both imply an active removal of elements... the actual behavior of those methods is to "limit" the size of an iterable and to "skip" the first elements of an iterable.


Status: Duplicate
Merged Into: #477

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by j...@nwsnet.de on 2011-05-11 at 09:16 PM


Your basic argument is valid, of course. Excuse my trace of conservatism :)

Which names are suitable is debatable, though. "limit" can be interpreted as modifying the underlying data, too (in the sense of truncation). I think it depends on the point of view one chooses. When considering the data to operate with as on-the-fly, speaking of "modifying" seems very appropriate to me.

Also, "transform", "partition" and "reverse" don't operate in-place in Guava, too (although one could think so, ignoring the return type). Actually, when it comes to Iterables, only the three methods that deal with Collections modify data in-place (ok, that's because of the unmodifyable nature of Iterable, but still).

However, as "skip" and "limit" are already in place, sticking to those IMHO is the wisest option anyway. Not my first choice (principle of least surprise is violated here, I'd say from my experience with common naming in this domain), and it might underline the usefulness a mapping like the one I outlined above, but hey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant