Skip to content

Commit

Permalink
squash! Add -shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
cireu committed Mar 17, 2023
1 parent 27c5eac commit a62d3d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions dash.el
Expand Up @@ -3248,14 +3248,14 @@ if the first element should sort before the second."
(target (pop rest)))
(cons target (nconc head rest))))

(defun -shuffle (list)
"Return a new shuffled LIST.
(defun -shuffle (list &optional rng)
"Return a new shuffled LIST, shuffling using RNG.
The returned list is shuffled by using Fisher-Yates' Algorithm. See
https://en.wikipedia.org/wiki/Fisher-Yates_shuffle for more details."
(declare (pure t) (side-effect-free t))
(let* ((len (length list))
(random-nums (-map #'random (number-sequence len 1 -1)))
(random-nums (-map (or rng #'random) (number-sequence len 1 -1)))
result)
(--each random-nums
(setq list (-to-head it list))
Expand Down
22 changes: 14 additions & 8 deletions dev/examples.el
Expand Up @@ -53,6 +53,17 @@
(defun even? (num) (= 0 (% num 2)))
(defun square (num) (* num num))

(defun make-xorshift32-rng (seed)
(let ((state (list seed))
(uint32-max (- (expt 2 32) 1)))
(lambda (limit)
(let* ((seed (car state))
(step1 (logxor seed (logand uint32-max (ash seed 13))))
(step2 (logxor step1 (logand uint32-max (ash seed -17))))
(final (logxor step2 (logand uint32-max (ash step2 5)))))
(setcar state final)
(mod final limit)))))

(def-example-group "Maps"
"Functions in this category take a transforming function, which
is then applied sequentially to each or selected elements of the
Expand Down Expand Up @@ -1928,15 +1939,10 @@ related predicates."
(list (-to-head 2 l) l)) => '((3 1 2 4 5) (1 2 3 4 5)))

(defexamples -shuffle
(progn
(random "dash1")
(-shuffle '(1 2 3 4 5 6 7))) => '(2 7 6 4 5 1 3)
(progn
(random "dash2")
(-shuffle '(1 2 3 4 5 6 7))) => '(1 5 2 4 3 7 6)
(-shuffle '(1 2 3 4 5 6 7) (make-xorshift32-rng #xcafe)) => '(7 6 1 2 3 5 4)
(-shuffle '(1 2 3 4 5 6 7) (make-xorshift32-rng #xbeef)) => '(4 3 2 5 6 7 1)
(let ((l '(1 2 3 4 5 6 7)))
(random "dash3")
(list (-shuffle '(1 2 3 4 5 6 7)) l)) => '((3 4 1 5 7 6 2) (1 2 3 4 5 6 7))
(list (-shuffle '(1 2 3 4 5 6 7) (make-xorshift32-rng #xdead)) l)) => '((3 4 6 5 1 2 7) (1 2 3 4 5 6 7))
(-shuffle nil) => nil)

(defexamples -list
Expand Down

0 comments on commit a62d3d9

Please sign in to comment.