Skip to content

Instantly share code, notes, and snippets.

@moea
Last active May 10, 2026 09:41
Show Gist options
  • Select an option

  • Save moea/1bb39d7103e879005a3d099b75f56130 to your computer and use it in GitHub Desktop.

Select an option

Save moea/1bb39d7103e879005a3d099b75f56130 to your computer and use it in GitHub Desktop.
(effect Rand
(rand-int :: (-> int int))
(rand :: (-> float)))
(define with-rng :: (forall [A e] (-> int (-> A ! <Rand ...e>) A ! <...e>))
(fn [seed thunk]
(let [_ (_rng-seed seed)]
((handler
(return v -> v)
((rand-int n) k -> (resume k (_rand-int n)))
((rand) k -> (resume k (_rand))))
thunk))))
;; toy impl, have no arrays or transients
(define shuffle :: (forall [F A] :where [(Seq F)] (-> (F A) (Vector A) ! <Rand>))
(fn [xs]
(loop [remaining (seq->vec xs) acc []]
(if (empty? remaining)
acc
(let [i (rand-int (count remaining))]
(recur (seq-append (take i remaining)
(drop (+ i 1) remaining))
(conj acc (nth remaining i))))))))
(with-rng 27 (fn [] (shuffle '(1 2 3 4))))) ; gradually typed, so no effect annotation necessary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment