Clojure Goodness: Shuffle A Collection
In Clojure we can use the shuffle
function with a collection argument to get a new collection where the items of the input collection are re-ordered randomly. The function delegates to the Java java.util.Collections#shuffle
method.
In the following example code we use the shuffle
method:
(ns mrhaki.core.shuffle
(:require [clojure.test :refer [is]]))
;; shuffle will return a new collection
;; where the items are in a different order.
(shuffle (range 5)) ;; Possible collection [4 0 1 2 3]
(shuffle (range 5)) ;; Possible collection [1 3 4 2 0]
;; Define a deck of cards.
(def cards (for [suite [\♥ \♠ \♣ \♦]
symbol (concat (range 2 11) [\J \Q \K \A])]
(str suite symbol)))
;; Some checks on our deck of cards.
(is (= 52 (count cards)))
(is (= (list "♥2" "♥3" "♥4" "♥5" "♥6" "♥7" "♥8" "♥9" "♥10" "♥J" "♥Q" "♥K" "♥A")
(take 13 cards)))
;; Let's shuffle the deck. We get a new collection of cards ordered randomly.
(def shuffled-deck (shuffle cards))
;; Shuffled deck contains all items from the cards collection.
(is (true? (every? (set cards) shuffled-deck)))
;; We can take a number of cards.
(take 5 shuffled-deck) ;; Possible result: ("♦6" "♦10" "♥K" "♥4" "♥10")
;; We do a re-shuffle and get different cards now.
(take 5 (shuffle shuffled-deck)) ;; Possible result: ("♥10" "♥Q" "♦4" "♣8" "♠5")
Written with Clojure 1.10.1.