Often, I find myself folding over a range
object if I need to repeatedly transform something. This, to me though, feels like an abuse of fold
since I never even use the numbers that I'm folding with.
Almost perfect example:
(ns shuffle-example
(:import [java.util Collection Collections]))
(defn shuffle [^Collection coll rand-gen]
(let [j-coll (java.util.ArrayList. coll)]
(Collections/shuffle j-coll rand-gen)
(into (empty coll) j-coll)))
(defn shuffle-many [^Collection coll n-shuffles rand-gen]
(reduce (fn [c _] (shuffle c rand-gen))
coll
(range n-shuffles)))
- Is this considered bad form? Is there a more idiomatic way of achieving this? I know in this case it's inefficient since it requires constant conversions between list types, but it shows my question well.
- Is there a better way to deterministically shuffle a list in Clojure? Clojure comes with a
shuffle
function, but it doesn't accept aRandom
, so it's not very useful. Short of writing my ownshuffle
from scratch, this is the best I've been able to come up with.