On Wed, Aug 27, 2008 at 6:59 AM, Rich Hickey <[EMAIL PROTECTED]> wrote:
> While I appreciate the intellectual exercise (and would point you to > http://okmij.org/ftp/Haskell/perfect-shuffle.txt if you haven't seen > it), one of the nice things about Clojure is that you aren't trapped > without mutability when you need it, and you need it here since there > is a known O(n) in-place implementation and you can't do better than > O(n log n) otherwise. Your implementation leveraging Java's > Collections/shuffle _is_ functional, it neither alters its arg nor has > any other side effects, and is the right way to implement this, since > Collections/shuffle has seen a lot of use/testing. See Clojure's sort, > for instance. It's ok to leverage Java's speed inside a functional > interface. For example, Clojure's persistent maps and vectors couldn't > be as nearly fast as they are if they were implemented purely > functionally. > > Rich > It was a good exercise, but it's getting old given the superior easy solution :) Thanks, I hadn't seen the perfect-shuffle you linked. I still think the sort by random keys is pretty good for how simple it is, but the little nugget about random number bias when N!>M helps show how it can fall short of perfection for large sequences. Now that you say it, it's obvious that shuffle-java is functional. I only discovered late in the game how sort uses toArray. To do it over without Collections/shuffle, I would throw out sort-by-mem-keys, get a Java array, and shuffle that in place. Frankly, any opportunity to call Java is satisfying just because the integration is so seamless! (As long as it doesn't harm concurrency in the program, of course.) Shawn --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---