Monday, February 14, 2011

I whine too much.

Mea culpa.
   ...
   for (Foo foo : permute(fooList)) {
        operateOnFoo (foo);
    }
   ...

  private static final Random rng = new Random();

  private static <E> List<E> permute (List<E> list) {
    List<E> answer = Lists.newArrayList (list);
    Collections.shuffle (answer, rng);
    return answer;
  }

2 comments:

  1. But is that the implementation people would expect? If I read permute I'd assume/hope for an implementation that will over subsequent n! (assuming distinct elements) calls enumerate all permutations of the collection.

    ReplyDelete
  2. That's why it is private and static.

    If you really thought that, wouldn't you expect a two-argument version that allowed you to select which of the n! permutations?

    ReplyDelete