FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add sort support from @thomasmulvaney by halgari · Pull Request #361 · pixie-lang/pixie · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .pxi  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
34 changes: 34 additions & 0 deletions pixie/stdlib.pxi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2584,3 +2584,37 @@ Calling this function on something that is not ISeqable returns a seq with that
(if (satisfies? IComparable x)
(-compare x y)
(throw [::ComparisonError (str x " does not satisfy IComparable")])))

;;; Sorting

(defn -merge-sort-step
[comp-fn merged-left merged-right res]
(cond
(empty? merged-left) (into res merged-right)
(empty? merged-right) (into res merged-left)
:else
(if (neg? (comp-fn (first merged-left) (first merged-right)))
(recur comp-fn (rest merged-left) merged-right (conj res (first merged-left)))
(recur comp-fn merged-left (rest merged-right) (conj res (first merged-right))))))

(defn -merge-sort-split
[comp-fn coll]
(if (> (count coll) 1)
(let [[left right] (split-at (/ (count coll) 2) coll)]
(-merge-sort-step comp-fn
(-merge-sort-split comp-fn left)
(-merge-sort-split comp-fn right)
[]))
coll))

(defn merge-sort [comp-fn coll]
(-merge-sort-split comp-fn coll))

(defn sort
([coll]
(sort compare coll))
([comp-fn coll]
(merge-sort comp-fn coll)))


;;; End Sorting
11 changes: 11 additions & 0 deletions pixie/test-sort.pxi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns pixie.tests.test-sort
(require pixie.test :as t))

(t/deftest test-sort
(t/assert= (sort [5 2 3 1 4]) [1 2 3 4 5])
(t/assert= (sort ["d" "c" "b" "a"]) ["a" "b" "c" "d"])
(t/assert= (sort [1/1 1/2 1/3 1/4]) [1/4 1/3 1/2 1/1]))

(t/deftest test-sort-big
(t/assert= (sort (range 10000)) (range 10000))
(t/assert= (sort (range 9999 -1 -1)) (range 10000)))

Back | FazBrowse Home | New Git URL