1
0
mirror of https://github.com/janet-lang/janet synced 2025-06-21 07:54:12 +00:00

Add sorting to boot.dst

This commit is contained in:
Calvin Rose 2018-03-28 16:38:05 -04:00
parent cee0f6acd1
commit f4ef0ae0bb

View File

@ -266,6 +266,11 @@ evaluates to true."
fal)))))) fal))))))
(aux 0)) (aux 0))
(defmacro default
"Suplies a default argument when a value is nil."
[sym default-value]
(tuple 'def sym (tuple 'or sym default-value)))
(defmacro when-let (defmacro when-let
"Takes the first one or two forms in vector and if true binds "Takes the first one or two forms in vector and if true binds
all the forms with let and evaluates the body" all the forms with let and evaluates the body"
@ -298,10 +303,48 @@ evaluates to true."
### ###
### ###
### Indexed Conbinators ### Indexed Combinators
### ###
### ###
(def sort
"Sort an array in-place. Uses quicksort and is not a stable sort."
(do
(defn partition
[a lo hi by]
(def pivot (get a hi))
(var i lo)
(for [j lo hi]
(def aj (get a j))
(when (by aj pivot)
(def ai (get a i))
(put a i aj)
(put a j ai)
(++ i)))
(put a hi (get a i))
(put a i pivot)
i)
(defn sort-help
[a lo hi by]
(when (> hi lo)
(def piv (partition a lo hi by))
(sort-help a lo (- piv 1) by)
(sort-help a (+ piv 1) hi by))
a)
(fn [a by]
(sort-help a 0 (- (length a) 1) (or by order<)))))
(defn sorted
"Returns the sorted version of an indexed data structure."
[ind by]
(def sa (sort (apply1 array ind) by))
(if (= :tuple (type ind))
(apply1 tuple sa)
sa))
(defn reduce (defn reduce
"Reduce, also know as foldleft in many languages, transform "Reduce, also know as foldleft in many languages, transform
an indexed type (array, tuple) with a function to produce a value." an indexed type (array, tuple) with a function to produce a value."