Guava: Add FluentIterable.shuffle

Created on 31 Oct 2014  路  2Comments  路  Source: google/guava

_Original issue created by electrum on 2013-04-03 at 02:20 AM_


It's often useful to have a functional version of shuffle:

private static <T> List<T> shuffle(Iterable<T> iterable)
{
    List<T> list = Lists.newArrayList(iterable);
    Collections.shuffle(list);
    return list;
}
P4 package=collect status=research type=addition

Most helpful comment

_Original comment posted by [email protected] on 2013-04-03 at 06:29 PM_


I looked at 20 Google callers to Collections.shuffle. The divide about evenly into people who are manually cloning the list, people who really ought to be manually cloning the list but aren't, and people who don't need to clone it.

I'd say that a functional version of shuffle is a good thing for us to have. The complication is going to be in whether it's worth adding plain Iterables.shuffle or whether we should opt for a more powerful API that can choose n items with or without replacement in a streaming fashion (but special cases for selecting one item from RandomAccess inputs) and whatever other features people might want. We do have such a chooser utility internally, and we should consider promoting it.


Labels: Package-Collect, Type-Addition

All 2 comments

_Original comment posted by [email protected] on 2013-04-03 at 06:29 PM_


I looked at 20 Google callers to Collections.shuffle. The divide about evenly into people who are manually cloning the list, people who really ought to be manually cloning the list but aren't, and people who don't need to clone it.

I'd say that a functional version of shuffle is a good thing for us to have. The complication is going to be in whether it's worth adding plain Iterables.shuffle or whether we should opt for a more powerful API that can choose n items with or without replacement in a streaming fashion (but special cases for selecting one item from RandomAccess inputs) and whatever other features people might want. We do have such a chooser utility internally, and we should consider promoting it.


Labels: Package-Collect, Type-Addition

Iterables aren't required to complete - how would that work with this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gissuebot picture gissuebot  路  3Comments

cowwoc picture cowwoc  路  3Comments

terence1984 picture terence1984  路  3Comments

oliviercailloux picture oliviercailloux  路  4Comments

JWT007 picture JWT007  路  4Comments