Skip to content

Instantly share code, notes, and snippets.

@theronic
Created November 6, 2025 15:57
Show Gist options
  • Select an option

  • Save theronic/62a03642ffc28f50b8acad22760cb33f to your computer and use it in GitHub Desktop.

Select an option

Save theronic/62a03642ffc28f50b8acad22760cb33f to your computer and use it in GitHub Desktop.
Bubble Sort in Clojure
(defn bubble-sort-step [coll]
(if-not (seq coll)
coll
(let [[a b & rst] coll]
(if-not b
coll
(lazy-seq
(cons (min a b)
(bubble-sort-step (cons (max a b) rst))))))))
(defn fixed-point
"Applies f repeatedly to x until the result no longer changes (i.e., f(result) = result)."
[f x]
(loop [current x]
(let [next (f current)]
(if (= next current)
current
(recur next)))))
(comment
(fixed-point bubble-sort-step '(4 3 2 1 5)) => (1 2 3 4 5)
)
@theronic
Copy link
Author

theronic commented Nov 6, 2025

there are cleaner ways to do this with reduce, and peek

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment