mirror of
https://github.com/carp-lang/Carp.git
synced 2024-11-04 01:25:04 +03:00
Fix all nth usage
This commit is contained in:
parent
949d4e29ee
commit
07def7db23
@ -14,7 +14,7 @@ It will sum the previous sum with each new value, starting at `0`.")
|
|||||||
(let [total x]
|
(let [total x]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (length xs)]
|
(for [i 0 (length xs)]
|
||||||
(set! total (~f total (nth xs i))))
|
(set! total (~f total (unsafe-nth xs i))))
|
||||||
total)))
|
total)))
|
||||||
|
|
||||||
(doc empty? "checks whether the array `a` is empty.")
|
(doc empty? "checks whether the array `a` is empty.")
|
||||||
@ -25,7 +25,7 @@ It will sum the previous sum with each new value, starting at `0`.")
|
|||||||
(defn any? [f a]
|
(defn any? [f a]
|
||||||
(let-do [res false]
|
(let-do [res false]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (~f (nth a i))
|
(when (~f (unsafe-nth a i))
|
||||||
(do
|
(do
|
||||||
(set! res true)
|
(set! res true)
|
||||||
(break))))
|
(break))))
|
||||||
@ -35,7 +35,7 @@ It will sum the previous sum with each new value, starting at `0`.")
|
|||||||
(defn all? [f a]
|
(defn all? [f a]
|
||||||
(let-do [res true]
|
(let-do [res true]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (not (~f (nth a i)))
|
(when (not (~f (unsafe-nth a i)))
|
||||||
(do
|
(do
|
||||||
(set! res false)
|
(set! res false)
|
||||||
(break))))
|
(break))))
|
||||||
@ -47,9 +47,9 @@ If it doesn’t find an element, `Nothing` will be returned.")
|
|||||||
(defn find [f a]
|
(defn find [f a]
|
||||||
(let-do [res (Maybe.Nothing)]
|
(let-do [res (Maybe.Nothing)]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (~f (nth a i))
|
(when (~f (unsafe-nth a i))
|
||||||
(do
|
(do
|
||||||
(set! res (Maybe.Just @(nth a i)))
|
(set! res (Maybe.Just @(unsafe-nth a i)))
|
||||||
(break))))
|
(break))))
|
||||||
res))
|
res))
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ If it doesn’t find an index, `Nothing` will be returned.")
|
|||||||
(defn find-index [f a]
|
(defn find-index [f a]
|
||||||
(let-do [ret (Maybe.Nothing)]
|
(let-do [ret (Maybe.Nothing)]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (~f (nth a i))
|
(when (~f (unsafe-nth a i))
|
||||||
(do
|
(do
|
||||||
(set! ret (Maybe.Just i))
|
(set! ret (Maybe.Just i))
|
||||||
(break))))
|
(break))))
|
||||||
@ -69,7 +69,7 @@ If it doesn’t find an index, `Nothing` will be returned.")
|
|||||||
|
|
||||||
Generates a runtime error if the array is empty.")
|
Generates a runtime error if the array is empty.")
|
||||||
(defn unsafe-first [a]
|
(defn unsafe-first [a]
|
||||||
@(Array.nth a 0))
|
@(Array.unsafe-nth a 0))
|
||||||
|
|
||||||
(doc first "takes the first element of an array and returns a `Just`.
|
(doc first "takes the first element of an array and returns a `Just`.
|
||||||
|
|
||||||
@ -77,13 +77,13 @@ Returns `Nothing` if the array is empty.")
|
|||||||
(defn first [a]
|
(defn first [a]
|
||||||
(if (empty? a)
|
(if (empty? a)
|
||||||
(Maybe.Nothing)
|
(Maybe.Nothing)
|
||||||
(Maybe.Just @(Array.nth a 0))))
|
(Maybe.Just @(Array.unsafe-nth a 0))))
|
||||||
|
|
||||||
(doc unsafe-last "takes the last element of an array.
|
(doc unsafe-last "takes the last element of an array.
|
||||||
|
|
||||||
Generates a runtime error if the array is empty.")
|
Generates a runtime error if the array is empty.")
|
||||||
(defn unsafe-last [a]
|
(defn unsafe-last [a]
|
||||||
@(Array.nth a (Int.dec (Array.length a))))
|
@(Array.unsafe-nth a (Int.dec (Array.length a))))
|
||||||
|
|
||||||
|
|
||||||
(doc last "takes the last element of an array and returns a `Just`.
|
(doc last "takes the last element of an array and returns a `Just`.
|
||||||
@ -92,7 +92,7 @@ Returns `Nothing` if the array is empty.")
|
|||||||
(defn last [a]
|
(defn last [a]
|
||||||
(if (empty? a)
|
(if (empty? a)
|
||||||
(Maybe.Nothing)
|
(Maybe.Nothing)
|
||||||
(Maybe.Just @(Array.nth a (Int.dec (Array.length a))))))
|
(Maybe.Just @(Array.unsafe-nth a (Int.dec (Array.length a))))))
|
||||||
|
|
||||||
(doc = "compares two arrays.")
|
(doc = "compares two arrays.")
|
||||||
(defn = [a b]
|
(defn = [a b]
|
||||||
@ -100,7 +100,7 @@ Returns `Nothing` if the array is empty.")
|
|||||||
false
|
false
|
||||||
(let-do [eq true]
|
(let-do [eq true]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (/= (nth a i) (nth b i))
|
(when (/= (unsafe-nth a i) (unsafe-nth b i))
|
||||||
(do
|
(do
|
||||||
(set! eq false)
|
(set! eq false)
|
||||||
(break))))
|
(break))))
|
||||||
@ -112,10 +112,10 @@ If the array is empty, it returns `Nothing`.")
|
|||||||
(defn maximum [xs]
|
(defn maximum [xs]
|
||||||
(if (empty? xs)
|
(if (empty? xs)
|
||||||
(Maybe.Nothing)
|
(Maybe.Nothing)
|
||||||
(let-do [result (nth xs 0)
|
(let-do [result (unsafe-nth xs 0)
|
||||||
n (length xs)]
|
n (length xs)]
|
||||||
(for [i 1 n]
|
(for [i 1 n]
|
||||||
(let [x (nth xs i)]
|
(let [x (unsafe-nth xs i)]
|
||||||
(when (< result x)
|
(when (< result x)
|
||||||
(set! result x))))
|
(set! result x))))
|
||||||
(Maybe.Just @result))))
|
(Maybe.Just @result))))
|
||||||
@ -126,10 +126,10 @@ If the array is empty, returns `Nothing`")
|
|||||||
(defn minimum [xs]
|
(defn minimum [xs]
|
||||||
(if (empty? xs)
|
(if (empty? xs)
|
||||||
(Maybe.Nothing)
|
(Maybe.Nothing)
|
||||||
(let-do [result (nth xs 0)
|
(let-do [result (unsafe-nth xs 0)
|
||||||
n (length xs)]
|
n (length xs)]
|
||||||
(for [i 1 n]
|
(for [i 1 n]
|
||||||
(let [x (nth xs i)]
|
(let [x (unsafe-nth xs i)]
|
||||||
(when (> result x)
|
(when (> result x)
|
||||||
(set! result x))))
|
(set! result x))))
|
||||||
(Maybe.Just @result))))
|
(Maybe.Just @result))))
|
||||||
@ -142,7 +142,7 @@ If the array is empty, returns `Nothing`")
|
|||||||
(defn subarray [xs start-index end-index]
|
(defn subarray [xs start-index end-index]
|
||||||
(let-do [result []]
|
(let-do [result []]
|
||||||
(for [i start-index end-index]
|
(for [i start-index end-index]
|
||||||
(set! result (push-back result @(nth xs i))))
|
(set! result (push-back result @(unsafe-nth xs i))))
|
||||||
result))
|
result))
|
||||||
|
|
||||||
(doc prefix-array "gets a prefix array to `end-index`.")
|
(doc prefix-array "gets a prefix array to `end-index`.")
|
||||||
@ -158,8 +158,8 @@ If the array is empty, returns `Nothing`")
|
|||||||
(let-do [i 0
|
(let-do [i 0
|
||||||
j (Int.dec (length &a))]
|
j (Int.dec (length &a))]
|
||||||
(while (Int.< i j)
|
(while (Int.< i j)
|
||||||
(let-do [tmp @(nth &a i)]
|
(let-do [tmp @(unsafe-nth &a i)]
|
||||||
(aset! &a i @(nth &a j))
|
(aset! &a i @(unsafe-nth &a j))
|
||||||
(set! i (Int.inc i))
|
(set! i (Int.inc i))
|
||||||
(aset! &a j tmp)
|
(aset! &a j tmp)
|
||||||
(set! j (Int.dec j))))
|
(set! j (Int.dec j))))
|
||||||
@ -171,7 +171,7 @@ If the element is not found, returns `Nothing`")
|
|||||||
(defn index-of [a e]
|
(defn index-of [a e]
|
||||||
(let-do [idx (Maybe.Nothing)]
|
(let-do [idx (Maybe.Nothing)]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (= (nth a i) e)
|
(when (= (unsafe-nth a i) e)
|
||||||
(do
|
(do
|
||||||
(set! idx (Maybe.Just i))
|
(set! idx (Maybe.Just i))
|
||||||
(break))))
|
(break))))
|
||||||
@ -181,36 +181,36 @@ If the element is not found, returns `Nothing`")
|
|||||||
(defn element-count [a e]
|
(defn element-count [a e]
|
||||||
(let-do [c 0]
|
(let-do [c 0]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (= e (nth a i)) (set! c (Int.inc c))))
|
(when (= e (unsafe-nth a i)) (set! c (Int.inc c))))
|
||||||
c))
|
c))
|
||||||
|
|
||||||
(doc predicate-count "counts the number of elements satisfying the predicate function `pred` in an array.")
|
(doc predicate-count "counts the number of elements satisfying the predicate function `pred` in an array.")
|
||||||
(defn predicate-count [a pred]
|
(defn predicate-count [a pred]
|
||||||
(let-do [c 0]
|
(let-do [c 0]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(when (~pred (nth a i))
|
(when (~pred (unsafe-nth a i))
|
||||||
(set! c (Int.inc c))))
|
(set! c (Int.inc c))))
|
||||||
c))
|
c))
|
||||||
|
|
||||||
(doc aupdate "transmutes (i.e. updates) the element at index `i` of an array `a` using the function `f`.")
|
(doc aupdate "transmutes (i.e. updates) the element at index `i` of an array `a` using the function `f`.")
|
||||||
(defn aupdate [a i f]
|
(defn aupdate [a i f]
|
||||||
(let [new-value (~f (nth &a i))]
|
(let [new-value (~f (unsafe-nth &a i))]
|
||||||
(aset a i new-value)))
|
(aset a i new-value)))
|
||||||
|
|
||||||
(doc aupdate! "transmutes (i.e. updates) the element at index `i` of an array `a` using the function `f` in place.")
|
(doc aupdate! "transmutes (i.e. updates) the element at index `i` of an array `a` using the function `f` in place.")
|
||||||
(defn aupdate! [a i f]
|
(defn aupdate! [a i f]
|
||||||
(aset! a i (~f (nth a i))))
|
(aset! a i (~f (unsafe-nth a i))))
|
||||||
|
|
||||||
(doc swap "swaps the indices `i` and `j` of an array `a`.")
|
(doc swap "swaps the indices `i` and `j` of an array `a`.")
|
||||||
(defn swap [a i j]
|
(defn swap [a i j]
|
||||||
(let [x @(nth &a i)
|
(let [x @(unsafe-nth &a i)
|
||||||
y @(nth &a j)]
|
y @(unsafe-nth &a j)]
|
||||||
(aset (aset a i y) j x)))
|
(aset (aset a i y) j x)))
|
||||||
|
|
||||||
(doc swap! "swaps the indices `i` and `j` of an array `a` in place.")
|
(doc swap! "swaps the indices `i` and `j` of an array `a` in place.")
|
||||||
(defn swap! [a i j]
|
(defn swap! [a i j]
|
||||||
(let-do [x @(nth a i)
|
(let-do [x @(unsafe-nth a i)
|
||||||
y @(nth a j)]
|
y @(unsafe-nth a j)]
|
||||||
(aset! a i y)
|
(aset! a i y)
|
||||||
(aset! a j x)))
|
(aset! a j x)))
|
||||||
|
|
||||||
@ -254,7 +254,7 @@ This function copies the array. If you don’t want that, use [`endo-map`](#endo
|
|||||||
(defn copy-map [f a]
|
(defn copy-map [f a]
|
||||||
(let-do [na (allocate (length a))]
|
(let-do [na (allocate (length a))]
|
||||||
(for [i 0 (length a)]
|
(for [i 0 (length a)]
|
||||||
(aset-uninitialized! &na i (~f (nth a i))))
|
(aset-uninitialized! &na i (~f (unsafe-nth a i))))
|
||||||
na))
|
na))
|
||||||
|
|
||||||
(doc zip "maps over two arrays using a function `f` that takes two arguments. It will produces a new array with the length of the shorter input.
|
(doc zip "maps over two arrays using a function `f` that takes two arguments. It will produces a new array with the length of the shorter input.
|
||||||
@ -264,7 +264,7 @@ The trailing elements of the longer array will be discarded.")
|
|||||||
(let-do [l (Int.min (length a) (length b))
|
(let-do [l (Int.min (length a) (length b))
|
||||||
na (allocate l)]
|
na (allocate l)]
|
||||||
(for [i 0 l]
|
(for [i 0 l]
|
||||||
(aset-uninitialized! &na i (~f (nth a i) (nth b i))))
|
(aset-uninitialized! &na i (~f (unsafe-nth a i) (unsafe-nth b i))))
|
||||||
na))
|
na))
|
||||||
|
|
||||||
(doc sum-length "returns the sum of lengths from a nested array `xs`.")
|
(doc sum-length "returns the sum of lengths from a nested array `xs`.")
|
||||||
@ -272,7 +272,7 @@ The trailing elements of the longer array will be discarded.")
|
|||||||
(let-do [sum 0
|
(let-do [sum 0
|
||||||
lxs (Array.length xs)]
|
lxs (Array.length xs)]
|
||||||
(for [i 0 lxs]
|
(for [i 0 lxs]
|
||||||
(set! sum (+ sum (Array.length (Array.nth xs i)))))
|
(set! sum (+ sum (Array.length (Array.unsafe-nth xs i)))))
|
||||||
sum))
|
sum))
|
||||||
|
|
||||||
(doc zero "returns the empty array.")
|
(doc zero "returns the empty array.")
|
||||||
@ -288,10 +288,10 @@ The trailing elements of the longer array will be discarded.")
|
|||||||
lxs (Array.length xs)
|
lxs (Array.length xs)
|
||||||
result (Array.allocate (sum-length xs))]
|
result (Array.allocate (sum-length xs))]
|
||||||
(for [i 0 lxs]
|
(for [i 0 lxs]
|
||||||
(let-do [arr (Array.nth xs i)
|
(let-do [arr (Array.unsafe-nth xs i)
|
||||||
len (Array.length arr)]
|
len (Array.length arr)]
|
||||||
(for [k 0 len]
|
(for [k 0 len]
|
||||||
(aset-uninitialized! &result (+ j k) @(Array.nth arr k)))
|
(aset-uninitialized! &result (+ j k) @(Array.unsafe-nth arr k)))
|
||||||
(set! j (+ j len))))
|
(set! j (+ j len))))
|
||||||
result))
|
result))
|
||||||
|
|
||||||
@ -312,7 +312,7 @@ The trailing elements of the longer array will be discarded.")
|
|||||||
;;(assert (<= 0 i))
|
;;(assert (<= 0 i))
|
||||||
;;(assert (< i (Array.length &arr)))
|
;;(assert (< i (Array.length &arr)))
|
||||||
(for [j i (Int.dec (Array.length &arr))]
|
(for [j i (Int.dec (Array.length &arr))]
|
||||||
(aset! &arr j @(nth &arr (inc j))))
|
(aset! &arr j @(unsafe-nth &arr (inc j))))
|
||||||
(pop-back arr)))
|
(pop-back arr)))
|
||||||
|
|
||||||
(doc copy-filter "filters the elements in an array.
|
(doc copy-filter "filters the elements in an array.
|
||||||
@ -324,7 +324,7 @@ It will create a copy. If you want to avoid that, consider using [`endo-filter`]
|
|||||||
(defn contains? [arr el]
|
(defn contains? [arr el]
|
||||||
(let-do [result false]
|
(let-do [result false]
|
||||||
(for [i 0 (Array.length arr)]
|
(for [i 0 (Array.length arr)]
|
||||||
(when (= el (Array.nth arr i))
|
(when (= el (Array.unsafe-nth arr i))
|
||||||
(do
|
(do
|
||||||
(set! result true)
|
(set! result true)
|
||||||
(break))))
|
(break))))
|
||||||
|
@ -22,9 +22,9 @@
|
|||||||
(defn max-of-three-until! [heap i len ord]
|
(defn max-of-three-until! [heap i len ord]
|
||||||
(let-do [lchild-i (lchild i)
|
(let-do [lchild-i (lchild i)
|
||||||
rchild-i (rchild i)]
|
rchild-i (rchild i)]
|
||||||
(when (and (< lchild-i len) (~ord (Array.nth heap lchild-i) (Array.nth heap i)))
|
(when (and (< lchild-i len) (~ord (Array.unsafe-nth heap lchild-i) (Array.unsafe-nth heap i)))
|
||||||
(set! i lchild-i))
|
(set! i lchild-i))
|
||||||
(when (and (< rchild-i len) (~ord (Array.nth heap rchild-i) (Array.nth heap i)))
|
(when (and (< rchild-i len) (~ord (Array.unsafe-nth heap rchild-i) (Array.unsafe-nth heap i)))
|
||||||
(set! i rchild-i))
|
(set! i rchild-i))
|
||||||
i))
|
i))
|
||||||
|
|
||||||
@ -45,9 +45,9 @@
|
|||||||
|
|
||||||
(defn push-up! [heap i ord]
|
(defn push-up! [heap i ord]
|
||||||
(while (/= i 0)
|
(while (/= i 0)
|
||||||
(let [elem (Array.nth heap i)
|
(let [elem (Array.unsafe-nth heap i)
|
||||||
parent-i (Heap.parent i)
|
parent-i (Heap.parent i)
|
||||||
parent-elem (Array.nth heap parent-i)]
|
parent-elem (Array.unsafe-nth heap parent-i)]
|
||||||
(if (not (~ord elem parent-elem))
|
(if (not (~ord elem parent-elem))
|
||||||
(break)
|
(break)
|
||||||
(do (Array.swap! heap i parent-i)
|
(do (Array.swap! heap i parent-i)
|
||||||
|
@ -154,7 +154,7 @@
|
|||||||
(list 'let ['xs xs
|
(list 'let ['xs xs
|
||||||
'len (list 'Array.length 'xs)]
|
'len (list 'Array.length 'xs)]
|
||||||
(list 'for ['i 0 'len]
|
(list 'for ['i 0 'len]
|
||||||
(list 'let [var (list 'Array.nth 'xs 'i)]
|
(list 'let [var (list 'Array.unsafe-nth 'xs 'i)]
|
||||||
expr))))
|
expr))))
|
||||||
|
|
||||||
(defmacro foreach [binding expr]
|
(defmacro foreach [binding expr]
|
||||||
|
@ -56,14 +56,14 @@
|
|||||||
l (Array.length (Bucket.entries b))
|
l (Array.length (Bucket.entries b))
|
||||||
es (entries b)]
|
es (entries b)]
|
||||||
(for [i 0 l]
|
(for [i 0 l]
|
||||||
(when (= (Pair.a (Array.nth es i)) k)
|
(when (= (Pair.a (Array.unsafe-nth es i)) k)
|
||||||
(do
|
(do
|
||||||
(set! ret i)
|
(set! ret i)
|
||||||
(break))))
|
(break))))
|
||||||
ret))
|
ret))
|
||||||
|
|
||||||
(defn get-idx [b i]
|
(defn get-idx [b i]
|
||||||
@(Pair.b (Array.nth (entries b) i)))
|
@(Pair.b (Array.unsafe-nth (entries b) i)))
|
||||||
|
|
||||||
(defn set-idx [b i val]
|
(defn set-idx [b i val]
|
||||||
(do (Array.aupdate! (entries &b) i &(fn [p] (Pair.set-b @p @val)))
|
(do (Array.aupdate! (entries &b) i &(fn [p] (Pair.set-b @p @val)))
|
||||||
@ -83,7 +83,7 @@
|
|||||||
(let [i (find b k)]
|
(let [i (find b k)]
|
||||||
(if (<= 0 i)
|
(if (<= 0 i)
|
||||||
;; The call to copy ('@') here is annoying - had to add it since sumtypes can't contain refs for now:
|
;; The call to copy ('@') here is annoying - had to add it since sumtypes can't contain refs for now:
|
||||||
(Maybe.Just @(Pair.b (Array.nth (entries b) i)))
|
(Maybe.Just @(Pair.b (Array.unsafe-nth (entries b) i)))
|
||||||
(Maybe.Nothing))))
|
(Maybe.Nothing))))
|
||||||
|
|
||||||
(defn put [b k v]
|
(defn put [b k v]
|
||||||
@ -98,7 +98,7 @@
|
|||||||
(defn remove [entries k]
|
(defn remove [entries k]
|
||||||
(let-do [nentries (the (Array (Pair a b)) [])]
|
(let-do [nentries (the (Array (Pair a b)) [])]
|
||||||
(for [i 0 (Array.length entries)]
|
(for [i 0 (Array.length entries)]
|
||||||
(let [e (Array.nth entries i)]
|
(let [e (Array.unsafe-nth entries i)]
|
||||||
(unless (= (Pair.a e) k)
|
(unless (= (Pair.a e) k)
|
||||||
(set! nentries (Array.push-back nentries @e)))))
|
(set! nentries (Array.push-back nentries @e)))))
|
||||||
nentries))
|
nentries))
|
||||||
@ -128,20 +128,20 @@
|
|||||||
(defn put [m k v]
|
(defn put [m k v]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
||||||
(update-buckets m &(fn [b]
|
(update-buckets m &(fn [b]
|
||||||
(let [n (Array.nth &b idx)]
|
(let [n (Array.unsafe-nth &b idx)]
|
||||||
(Array.aset b idx (Bucket.put @n k v)))))))
|
(Array.aset b idx (Bucket.put @n k v)))))))
|
||||||
|
|
||||||
(doc put! "Put a a value v into map m, using the key k, in place.")
|
(doc put! "Put a a value v into map m, using the key k, in place.")
|
||||||
(defn put! [m k v]
|
(defn put! [m k v]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets m))
|
(let [idx (Int.positive-mod (hash k) @(n-buckets m))
|
||||||
b (buckets m)
|
b (buckets m)
|
||||||
n (Array.nth b idx)]
|
n (Array.unsafe-nth b idx)]
|
||||||
(Array.aset! b idx (Bucket.put @n k v))))
|
(Array.aset! b idx (Bucket.put @n k v))))
|
||||||
|
|
||||||
(doc get-with-default "Get the value for the key k from map m. If it isn’t found, the default is returned.")
|
(doc get-with-default "Get the value for the key k from map m. If it isn’t found, the default is returned.")
|
||||||
(defn get-with-default [m k default-value]
|
(defn get-with-default [m k default-value]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
|
||||||
(Bucket.get (Array.nth (buckets m) idx) k default-value)))
|
(Bucket.get (Array.unsafe-nth (buckets m) idx) k default-value)))
|
||||||
|
|
||||||
(doc get "Get the value for the key k from map m. If it isn’t found, a zero element for the value type is returned.")
|
(doc get "Get the value for the key k from map m. If it isn’t found, a zero element for the value type is returned.")
|
||||||
(defn get [m k]
|
(defn get [m k]
|
||||||
@ -150,13 +150,13 @@
|
|||||||
(doc get-maybe "Get the value for the key k from map m. It returns a Maybe type, meaning that if nothing is found, Nothing is returned.")
|
(doc get-maybe "Get the value for the key k from map m. It returns a Maybe type, meaning that if nothing is found, Nothing is returned.")
|
||||||
(defn get-maybe [m k]
|
(defn get-maybe [m k]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
|
||||||
(Bucket.get-maybe (Array.nth (buckets m) idx) k)))
|
(Bucket.get-maybe (Array.unsafe-nth (buckets m) idx) k)))
|
||||||
|
|
||||||
(doc update "Update value at key k in map with function f, if it exists.")
|
(doc update "Update value at key k in map with function f, if it exists.")
|
||||||
(defn update [m k f]
|
(defn update [m k f]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
||||||
(update-buckets m &(fn [b]
|
(update-buckets m &(fn [b]
|
||||||
(let [n (Array.nth &b idx)
|
(let [n (Array.unsafe-nth &b idx)
|
||||||
i (Bucket.find n k)]
|
i (Bucket.find n k)]
|
||||||
(if (<= 0 i)
|
(if (<= 0 i)
|
||||||
;; currently can't write a Bucket.update that takes f due to bug #347
|
;; currently can't write a Bucket.update that takes f due to bug #347
|
||||||
@ -167,7 +167,7 @@
|
|||||||
(defn update-with-default [m k f v]
|
(defn update-with-default [m k f v]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
||||||
(update-buckets m &(fn [b]
|
(update-buckets m &(fn [b]
|
||||||
(let [n (Array.nth &b idx)
|
(let [n (Array.unsafe-nth &b idx)
|
||||||
i (Bucket.find n k)]
|
i (Bucket.find n k)]
|
||||||
(if (<= 0 i)
|
(if (<= 0 i)
|
||||||
(Array.aset b idx (Bucket.set-idx @n i &(~f (Bucket.get-idx n i))))
|
(Array.aset b idx (Bucket.set-idx @n i &(~f (Bucket.get-idx n i))))
|
||||||
@ -177,7 +177,7 @@
|
|||||||
(defn length [m]
|
(defn length [m]
|
||||||
(let-do [c 0]
|
(let-do [c 0]
|
||||||
(for [i 0 @(n-buckets m)]
|
(for [i 0 @(n-buckets m)]
|
||||||
(set! c (+ c (Array.length (Bucket.entries (Array.nth (buckets m) i))))))
|
(set! c (+ c (Array.length (Bucket.entries (Array.unsafe-nth (buckets m) i))))))
|
||||||
c))
|
c))
|
||||||
|
|
||||||
(doc empty? "Check whether the map m is empty.")
|
(doc empty? "Check whether the map m is empty.")
|
||||||
@ -187,24 +187,24 @@
|
|||||||
(doc contains? "Check whether the map m contains the key k.")
|
(doc contains? "Check whether the map m contains the key k.")
|
||||||
(defn contains? [m k]
|
(defn contains? [m k]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets m))]
|
||||||
(Bucket.contains? (Array.nth (buckets m) idx) k)))
|
(Bucket.contains? (Array.unsafe-nth (buckets m) idx) k)))
|
||||||
|
|
||||||
(doc remove "Remove the value under the key k from the map m.")
|
(doc remove "Remove the value under the key k from the map m.")
|
||||||
(defn remove [m k]
|
(defn remove [m k]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
|
||||||
(update-buckets m &(fn [b]
|
(update-buckets m &(fn [b]
|
||||||
(let [n (Array.nth &b idx)]
|
(let [n (Array.unsafe-nth &b idx)]
|
||||||
(Array.aset b idx (Bucket.shrink @n k)))))))
|
(Array.aset b idx (Bucket.shrink @n k)))))))
|
||||||
|
|
||||||
(doc all? "Do all key-value pairs pass the given predicate (of two arguments)?")
|
(doc all? "Do all key-value pairs pass the given predicate (of two arguments)?")
|
||||||
(defn all? [pred m]
|
(defn all? [pred m]
|
||||||
(let-do [ret true]
|
(let-do [ret true]
|
||||||
(for [i 0 @(n-buckets m)]
|
(for [i 0 @(n-buckets m)]
|
||||||
(let [bucket (Array.nth (buckets m) i)
|
(let [bucket (Array.unsafe-nth (buckets m) i)
|
||||||
len (Array.length (Bucket.entries bucket))
|
len (Array.length (Bucket.entries bucket))
|
||||||
entries (Bucket.entries bucket)]
|
entries (Bucket.entries bucket)]
|
||||||
(for [j 0 len]
|
(for [j 0 len]
|
||||||
(let [e (Array.nth entries j)]
|
(let [e (Array.unsafe-nth entries j)]
|
||||||
(unless (~pred (Pair.a e) (Pair.b e))
|
(unless (~pred (Pair.a e) (Pair.b e))
|
||||||
(set! ret false))))))
|
(set! ret false))))))
|
||||||
ret))
|
ret))
|
||||||
@ -217,22 +217,22 @@
|
|||||||
(doc for-each "Execute the binary function f for all keys and values in the map m.")
|
(doc for-each "Execute the binary function f for all keys and values in the map m.")
|
||||||
(defn for-each [m f]
|
(defn for-each [m f]
|
||||||
(for [i 0 @(n-buckets m)]
|
(for [i 0 @(n-buckets m)]
|
||||||
(let [bucket (Array.nth (buckets m) i)
|
(let [bucket (Array.unsafe-nth (buckets m) i)
|
||||||
len (Array.length (Bucket.entries bucket))
|
len (Array.length (Bucket.entries bucket))
|
||||||
entries (Bucket.entries bucket)]
|
entries (Bucket.entries bucket)]
|
||||||
(for [j 0 len]
|
(for [j 0 len]
|
||||||
(let [e (Array.nth entries j)]
|
(let [e (Array.unsafe-nth entries j)]
|
||||||
(~f (Pair.a e) (Pair.b e)))))))
|
(~f (Pair.a e) (Pair.b e)))))))
|
||||||
|
|
||||||
(doc endo-map "Transform values of the given map in place. f gets two arguments, key and value, and should return new value")
|
(doc endo-map "Transform values of the given map in place. f gets two arguments, key and value, and should return new value")
|
||||||
(defn endo-map [f m]
|
(defn endo-map [f m]
|
||||||
(do
|
(do
|
||||||
(for [i 0 @(n-buckets &m)]
|
(for [i 0 @(n-buckets &m)]
|
||||||
(let [bucket (Array.nth (buckets &m) i)
|
(let [bucket (Array.unsafe-nth (buckets &m) i)
|
||||||
len (Array.length (Bucket.entries bucket))
|
len (Array.length (Bucket.entries bucket))
|
||||||
entries (Bucket.entries bucket)]
|
entries (Bucket.entries bucket)]
|
||||||
(for [j 0 len]
|
(for [j 0 len]
|
||||||
(let [e (Array.nth entries j)]
|
(let [e (Array.unsafe-nth entries j)]
|
||||||
(Array.aset! entries j (Pair.init @(Pair.a e)
|
(Array.aset! entries j (Pair.init @(Pair.a e)
|
||||||
(~f (Pair.a e) (Pair.b e))))))))
|
(~f (Pair.a e) (Pair.b e))))))))
|
||||||
m))
|
m))
|
||||||
@ -241,11 +241,11 @@
|
|||||||
(defn kv-reduce [f init m]
|
(defn kv-reduce [f init m]
|
||||||
(do
|
(do
|
||||||
(for [i 0 @(n-buckets m)]
|
(for [i 0 @(n-buckets m)]
|
||||||
(let [bucket (Array.nth (buckets m) i)
|
(let [bucket (Array.unsafe-nth (buckets m) i)
|
||||||
len (Array.length (Bucket.entries bucket))
|
len (Array.length (Bucket.entries bucket))
|
||||||
entries (Bucket.entries bucket)]
|
entries (Bucket.entries bucket)]
|
||||||
(for [j 0 len]
|
(for [j 0 len]
|
||||||
(let [e (Array.nth entries j)]
|
(let [e (Array.unsafe-nth entries j)]
|
||||||
(set! init (~f init (Pair.a e) (Pair.b e)))))))
|
(set! init (~f init (Pair.a e) (Pair.b e)))))))
|
||||||
init))
|
init))
|
||||||
|
|
||||||
@ -265,7 +265,7 @@
|
|||||||
(defn from-array [a]
|
(defn from-array [a]
|
||||||
(let-do [m (create)]
|
(let-do [m (create)]
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(let [e (Array.nth a i)
|
(let [e (Array.unsafe-nth a i)
|
||||||
k (Pair.a e)
|
k (Pair.a e)
|
||||||
v (Pair.b e)]
|
v (Pair.b e)]
|
||||||
(set! m (put m k v))))
|
(set! m (put m k v))))
|
||||||
@ -303,7 +303,7 @@
|
|||||||
es (entries b)
|
es (entries b)
|
||||||
l (Array.length es)]
|
l (Array.length es)]
|
||||||
(for [i 0 l]
|
(for [i 0 l]
|
||||||
(when (= (Array.nth es i) k)
|
(when (= (Array.unsafe-nth es i) k)
|
||||||
(do
|
(do
|
||||||
(set! e true)
|
(set! e true)
|
||||||
(break))))
|
(break))))
|
||||||
@ -312,7 +312,7 @@
|
|||||||
(defn remove [entries k]
|
(defn remove [entries k]
|
||||||
(let-do [nentries []]
|
(let-do [nentries []]
|
||||||
(for [i 0 (Array.length entries)]
|
(for [i 0 (Array.length entries)]
|
||||||
(let [e (Array.nth entries i)]
|
(let [e (Array.unsafe-nth entries i)]
|
||||||
(unless (= e k)
|
(unless (= e k)
|
||||||
(set! nentries (Array.push-back nentries @e)))))
|
(set! nentries (Array.push-back nentries @e)))))
|
||||||
nentries))
|
nentries))
|
||||||
@ -341,7 +341,7 @@
|
|||||||
(defn put [s k]
|
(defn put [s k]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
|
||||||
(update-buckets s &(fn [b]
|
(update-buckets s &(fn [b]
|
||||||
(let [n (Array.nth &b idx)]
|
(let [n (Array.unsafe-nth &b idx)]
|
||||||
(if (SetBucket.contains? n k)
|
(if (SetBucket.contains? n k)
|
||||||
b
|
b
|
||||||
(Array.aset b idx (SetBucket.grow n @k))))))))
|
(Array.aset b idx (SetBucket.grow n @k))))))))
|
||||||
@ -350,7 +350,7 @@
|
|||||||
(defn put! [s k]
|
(defn put! [s k]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets s))
|
(let [idx (Int.positive-mod (hash k) @(n-buckets s))
|
||||||
b (buckets s)
|
b (buckets s)
|
||||||
n (Array.nth b idx)]
|
n (Array.unsafe-nth b idx)]
|
||||||
(when (not (SetBucket.contains? n k))
|
(when (not (SetBucket.contains? n k))
|
||||||
(Array.aset! b idx (SetBucket.grow n @k)))))
|
(Array.aset! b idx (SetBucket.grow n @k)))))
|
||||||
|
|
||||||
@ -358,7 +358,7 @@
|
|||||||
(defn length [s]
|
(defn length [s]
|
||||||
(let-do [c 0]
|
(let-do [c 0]
|
||||||
(for [i 0 @(n-buckets s)]
|
(for [i 0 @(n-buckets s)]
|
||||||
(set! c (+ c (Array.length (SetBucket.entries (Array.nth (buckets s) i))))))
|
(set! c (+ c (Array.length (SetBucket.entries (Array.unsafe-nth (buckets s) i))))))
|
||||||
c))
|
c))
|
||||||
|
|
||||||
(doc empty? "Check whether the set s is empty.")
|
(doc empty? "Check whether the set s is empty.")
|
||||||
@ -368,13 +368,13 @@
|
|||||||
(doc contains? "Check whether the set s contains the key k.")
|
(doc contains? "Check whether the set s contains the key k.")
|
||||||
(defn contains? [s k]
|
(defn contains? [s k]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets s))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets s))]
|
||||||
(SetBucket.contains? (Array.nth (buckets s) idx) k)))
|
(SetBucket.contains? (Array.unsafe-nth (buckets s) idx) k)))
|
||||||
|
|
||||||
(doc remove "Remove the key k from the set s.")
|
(doc remove "Remove the key k from the set s.")
|
||||||
(defn remove [s k]
|
(defn remove [s k]
|
||||||
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
|
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
|
||||||
(update-buckets s &(fn [b]
|
(update-buckets s &(fn [b]
|
||||||
(let [n (Array.nth &b idx)]
|
(let [n (Array.unsafe-nth &b idx)]
|
||||||
(Array.aset b idx (SetBucket.shrink n k)))))))
|
(Array.aset b idx (SetBucket.shrink n k)))))))
|
||||||
|
|
||||||
(doc all? "Does the predicate hold for all values in this set?")
|
(doc all? "Does the predicate hold for all values in this set?")
|
||||||
@ -399,18 +399,18 @@
|
|||||||
(doc for-each "Execute the unary function f for each element in the set s.")
|
(doc for-each "Execute the unary function f for each element in the set s.")
|
||||||
(defn for-each [s f]
|
(defn for-each [s f]
|
||||||
(for [i 0 @(n-buckets s)]
|
(for [i 0 @(n-buckets s)]
|
||||||
(let [bucket (Array.nth (buckets s) i)
|
(let [bucket (Array.unsafe-nth (buckets s) i)
|
||||||
len (Array.length (SetBucket.entries bucket))
|
len (Array.length (SetBucket.entries bucket))
|
||||||
entries (SetBucket.entries bucket)]
|
entries (SetBucket.entries bucket)]
|
||||||
(for [j 0 len]
|
(for [j 0 len]
|
||||||
(let [e (Array.nth entries j)]
|
(let [e (Array.unsafe-nth entries j)]
|
||||||
(~f e))))))
|
(~f e))))))
|
||||||
|
|
||||||
(doc from-array "Create a set from the values in array a.")
|
(doc from-array "Create a set from the values in array a.")
|
||||||
(defn from-array [a]
|
(defn from-array [a]
|
||||||
(let-do [s (create)]
|
(let-do [s (create)]
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(let [e (Array.nth a i)]
|
(let [e (Array.unsafe-nth a i)]
|
||||||
(set! s (put s e))))
|
(set! s (put s e))))
|
||||||
s))
|
s))
|
||||||
|
|
||||||
@ -418,11 +418,11 @@
|
|||||||
(defn reduce [f init s]
|
(defn reduce [f init s]
|
||||||
(do
|
(do
|
||||||
(for [i 0 @(n-buckets s)]
|
(for [i 0 @(n-buckets s)]
|
||||||
(let [bucket (Array.nth (buckets s) i)
|
(let [bucket (Array.unsafe-nth (buckets s) i)
|
||||||
len (Array.length (SetBucket.entries bucket))
|
len (Array.length (SetBucket.entries bucket))
|
||||||
entries (SetBucket.entries bucket)]
|
entries (SetBucket.entries bucket)]
|
||||||
(for [j 0 len]
|
(for [j 0 len]
|
||||||
(let [e (Array.nth entries j)]
|
(let [e (Array.unsafe-nth entries j)]
|
||||||
(set! init (~f init e))))))
|
(set! init (~f init e))))))
|
||||||
init))
|
init))
|
||||||
|
|
||||||
|
@ -97,13 +97,13 @@ If you want to replace all occurrences of the pattern, use `-1`.")
|
|||||||
lidx (Array.length &idx)
|
lidx (Array.length &idx)
|
||||||
result (Array.allocate (Int.inc lidx))]
|
result (Array.allocate (Int.inc lidx))]
|
||||||
(Array.aset-uninitialized! &result 0
|
(Array.aset-uninitialized! &result 0
|
||||||
(substring s 0 (if (> lidx 0) @(Array.nth &idx 0) (length s))))
|
(substring s 0 (if (> lidx 0) @(Array.unsafe-nth &idx 0) (length s))))
|
||||||
(for [i 0 (Int.dec (Array.length &idx))]
|
(for [i 0 (Int.dec (Array.length &idx))]
|
||||||
(Array.aset-uninitialized! &result (Int.inc i)
|
(Array.aset-uninitialized! &result (Int.inc i)
|
||||||
(substring s (Int.inc @(Array.nth &idx i)) @(Array.nth &idx (Int.inc i)))))
|
(substring s (Int.inc @(Array.unsafe-nth &idx i)) @(Array.unsafe-nth &idx (Int.inc i)))))
|
||||||
(when (> lidx 0)
|
(when (> lidx 0)
|
||||||
(Array.aset-uninitialized! &result lidx
|
(Array.aset-uninitialized! &result lidx
|
||||||
(suffix-string s (Int.inc @(Array.nth &idx (Int.dec lidx))))))
|
(suffix-string s (Int.inc @(Array.unsafe-nth &idx (Int.dec lidx))))))
|
||||||
result))
|
result))
|
||||||
|
|
||||||
(doc words "splits a string into words.")
|
(doc words "splits a string into words.")
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
(let [sum 0.0]
|
(let [sum 0.0]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(let [tmp (Double.- (Double.copy (Array.nth a i)) mean)]
|
(let [tmp (Double.- (Double.copy (Array.unsafe-nth a i)) mean)]
|
||||||
(set! sum (Double.* tmp tmp))))
|
(set! sum (Double.* tmp tmp))))
|
||||||
sum)))
|
sum)))
|
||||||
|
|
||||||
@ -36,7 +36,7 @@
|
|||||||
(let [sum 0.0]
|
(let [sum 0.0]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(set! sum (Double.- (Double.copy (Array.nth a i)) mean)))
|
(set! sum (Double.- (Double.copy (Array.unsafe-nth a i)) mean)))
|
||||||
sum)))
|
sum)))
|
||||||
|
|
||||||
(hidden _ss)
|
(hidden _ss)
|
||||||
@ -52,10 +52,10 @@
|
|||||||
(let [n (Array.length data)
|
(let [n (Array.length data)
|
||||||
sorted (Array.sorted data)]
|
sorted (Array.sorted data)]
|
||||||
(cond (= n 0) 0.0
|
(cond (= n 0) 0.0
|
||||||
(= (mod n 2) 1) @(Array.nth data (/ n 2))
|
(= (mod n 2) 1) @(Array.unsafe-nth data (/ n 2))
|
||||||
(let [mid (/ n 2)] ; else
|
(let [mid (/ n 2)] ; else
|
||||||
(/ (+ (the Double @(Array.nth data (dec mid)))
|
(/ (+ (the Double @(Array.unsafe-nth data (dec mid)))
|
||||||
@(Array.nth data mid))
|
@(Array.unsafe-nth data mid))
|
||||||
2.0)))))
|
2.0)))))
|
||||||
|
|
||||||
(doc low-median "Compute the low median of the samples data.")
|
(doc low-median "Compute the low median of the samples data.")
|
||||||
@ -63,8 +63,8 @@
|
|||||||
(let [n (Array.length data)
|
(let [n (Array.length data)
|
||||||
sorted (Array.sorted data)]
|
sorted (Array.sorted data)]
|
||||||
(cond (= n 0) 0.0
|
(cond (= n 0) 0.0
|
||||||
(= (mod n 2) 1) @(Array.nth data (/ n 2))
|
(= (mod n 2) 1) @(Array.unsafe-nth data (/ n 2))
|
||||||
@(Array.nth data (dec (/ n 2)))))) ; else
|
@(Array.unsafe-nth data (dec (/ n 2)))))) ; else
|
||||||
|
|
||||||
(doc high-median "Compute the high median of the samples data.")
|
(doc high-median "Compute the high median of the samples data.")
|
||||||
(defn high-median [data]
|
(defn high-median [data]
|
||||||
@ -72,15 +72,15 @@
|
|||||||
sorted (Array.sorted data)]
|
sorted (Array.sorted data)]
|
||||||
(if (= n 0)
|
(if (= n 0)
|
||||||
0.0
|
0.0
|
||||||
@(Array.nth data (/ n 2)))))
|
@(Array.unsafe-nth data (/ n 2)))))
|
||||||
|
|
||||||
(doc grouped-median "Compute the grouped median of the samples data.")
|
(doc grouped-median "Compute the grouped median of the samples data.")
|
||||||
(defn grouped-median [data interval]
|
(defn grouped-median [data interval]
|
||||||
(let [n (Array.length data)
|
(let [n (Array.length data)
|
||||||
sorted (Array.sorted data)]
|
sorted (Array.sorted data)]
|
||||||
(cond (= n 0) 0.0
|
(cond (= n 0) 0.0
|
||||||
(= n 1) @(Array.nth data 0)
|
(= n 1) @(Array.unsafe-nth data 0)
|
||||||
(let [x @(Array.nth data (/ n 2)) ; else
|
(let [x @(Array.unsafe-nth data (/ n 2)) ; else
|
||||||
l (- x (/ (from-int interval) 2.0))
|
l (- x (/ (from-int interval) 2.0))
|
||||||
cf (Maybe.from (Array.index-of data &x) -1)
|
cf (Maybe.from (Array.index-of data &x) -1)
|
||||||
f (Array.element-count data &x)]
|
f (Array.element-count data &x)]
|
||||||
@ -119,7 +119,7 @@
|
|||||||
n 1.4826] ; taken from Rust and R, because that’s how it’s done apparently
|
n 1.4826] ; taken from Rust and R, because that’s how it’s done apparently
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length data)]
|
(for [i 0 (Array.length data)]
|
||||||
(Array.aset! &abs-devs i (abs (- med @(Array.nth data i)))))
|
(Array.aset! &abs-devs i (abs (- med @(Array.unsafe-nth data i)))))
|
||||||
(* (median &abs-devs) n))))
|
(* (median &abs-devs) n))))
|
||||||
|
|
||||||
(hidden median-abs-dev-pct)
|
(hidden median-abs-dev-pct)
|
||||||
@ -132,15 +132,15 @@
|
|||||||
(Int.= 0 (Array.length sorted)) -1.0 ; should abort here
|
(Int.= 0 (Array.length sorted)) -1.0 ; should abort here
|
||||||
(Double.< pct 0.0) -1.0 ; should abort here
|
(Double.< pct 0.0) -1.0 ; should abort here
|
||||||
(Double.> pct 100.0) -1.0 ; should abort here
|
(Double.> pct 100.0) -1.0 ; should abort here
|
||||||
(Int.= 1 (Array.length sorted)) @(Array.nth sorted 0)
|
(Int.= 1 (Array.length sorted)) @(Array.unsafe-nth sorted 0)
|
||||||
(Double.= 100.0 pct) @(Array.nth sorted (Int.dec (Array.length sorted)))
|
(Double.= 100.0 pct) @(Array.unsafe-nth sorted (Int.dec (Array.length sorted)))
|
||||||
(let [len (Int.dec (Array.length sorted))
|
(let [len (Int.dec (Array.length sorted))
|
||||||
rank (Double.* (Double./ pct 100.0) (Double.from-int len))
|
rank (Double.* (Double./ pct 100.0) (Double.from-int len))
|
||||||
lrank (Double.floor rank)
|
lrank (Double.floor rank)
|
||||||
d (Double.- rank lrank)
|
d (Double.- rank lrank)
|
||||||
n (Double.to-int lrank)
|
n (Double.to-int lrank)
|
||||||
lo @(Array.nth sorted n)
|
lo @(Array.unsafe-nth sorted n)
|
||||||
hi @(Array.nth sorted (Int.inc n))]
|
hi @(Array.unsafe-nth sorted (Int.inc n))]
|
||||||
(Double.+ lo (Double.* d (Double.- hi lo))))))
|
(Double.+ lo (Double.* d (Double.- hi lo))))))
|
||||||
|
|
||||||
(doc quartiles "Compute the quartiles of the samples data.")
|
(doc quartiles "Compute the quartiles of the samples data.")
|
||||||
@ -157,7 +157,7 @@
|
|||||||
(doc iqr "Compute the interquartile range.")
|
(doc iqr "Compute the interquartile range.")
|
||||||
(defn iqr [data]
|
(defn iqr [data]
|
||||||
(let [s &(quartiles data)]
|
(let [s &(quartiles data)]
|
||||||
(the Double (- @(Array.nth s 2) @(Array.nth s 0)))))
|
(the Double (- @(Array.unsafe-nth s 2) @(Array.unsafe-nth s 0)))))
|
||||||
|
|
||||||
(hidden winsorize)
|
(hidden winsorize)
|
||||||
(defn winsorize [samples pct]
|
(defn winsorize [samples pct]
|
||||||
@ -166,7 +166,7 @@
|
|||||||
hi (Statistics.percentile-of-sorted tmp (Double.- 100.0 pct))]
|
hi (Statistics.percentile-of-sorted tmp (Double.- 100.0 pct))]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length tmp)]
|
(for [i 0 (Array.length tmp)]
|
||||||
(let [samp @(Array.nth tmp i)]
|
(let [samp @(Array.unsafe-nth tmp i)]
|
||||||
(cond
|
(cond
|
||||||
(> samp hi) (Array.aset! tmp i hi)
|
(> samp hi) (Array.aset! tmp i hi)
|
||||||
(< samp lo) (Array.aset! tmp i lo)
|
(< samp lo) (Array.aset! tmp i lo)
|
||||||
|
@ -89,7 +89,7 @@
|
|||||||
(let-do [sum 0
|
(let-do [sum 0
|
||||||
lstrings (Array.length strings)]
|
lstrings (Array.length strings)]
|
||||||
(for [i 0 lstrings]
|
(for [i 0 lstrings]
|
||||||
(set! sum (+ sum (String.length (Array.nth strings i)))))
|
(set! sum (+ sum (String.length (Array.unsafe-nth strings i)))))
|
||||||
sum))
|
sum))
|
||||||
|
|
||||||
(doc concat "Returns a new string which is the concatenation of the provided `strings`.")
|
(doc concat "Returns a new string which is the concatenation of the provided `strings`.")
|
||||||
@ -102,7 +102,7 @@
|
|||||||
lstrings (Array.length strings)
|
lstrings (Array.length strings)
|
||||||
result (String.allocate (sum-length strings) \ )]
|
result (String.allocate (sum-length strings) \ )]
|
||||||
(for [i 0 lstrings]
|
(for [i 0 lstrings]
|
||||||
(let-do [str (Array.nth strings i)
|
(let-do [str (Array.unsafe-nth strings i)
|
||||||
len (String.length str)]
|
len (String.length str)]
|
||||||
(string-set-at! &result j str)
|
(string-set-at! &result j str)
|
||||||
(set! j (+ j len))))
|
(set! j (+ j len))))
|
||||||
@ -117,7 +117,7 @@
|
|||||||
seps-size (* num-seps sep-length)
|
seps-size (* num-seps sep-length)
|
||||||
result (String.allocate (+ seps-size (sum-length strings)) \ )]
|
result (String.allocate (+ seps-size (sum-length strings)) \ )]
|
||||||
(for [i 0 lstrings]
|
(for [i 0 lstrings]
|
||||||
(let-do [str (Array.nth strings i)
|
(let-do [str (Array.unsafe-nth strings i)
|
||||||
len (String.length str)]
|
len (String.length str)]
|
||||||
(when (> i 0)
|
(when (> i 0)
|
||||||
(do
|
(do
|
||||||
@ -135,7 +135,7 @@
|
|||||||
sep-length (max 0 (- lstrings 1))
|
sep-length (max 0 (- lstrings 1))
|
||||||
result (String.allocate (+ sep-length (sum-length strings)) \ )]
|
result (String.allocate (+ sep-length (sum-length strings)) \ )]
|
||||||
(for [i 0 lstrings]
|
(for [i 0 lstrings]
|
||||||
(let-do [str (Array.nth strings i)
|
(let-do [str (Array.unsafe-nth strings i)
|
||||||
len (String.length str)]
|
len (String.length str)]
|
||||||
(when (> i 0)
|
(when (> i 0)
|
||||||
(do
|
(do
|
||||||
|
@ -219,8 +219,8 @@
|
|||||||
(let [total (Array.allocate (Array.length a))]
|
(let [total (Array.allocate (Array.length a))]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(Array.aset-uninitialized! &total i (f @(Array.nth a i)
|
(Array.aset-uninitialized! &total i (f @(Array.unsafe-nth a i)
|
||||||
@(Array.nth b i))))
|
@(Array.unsafe-nth b i))))
|
||||||
(init (Array.length a) total))))
|
(init (Array.length a) total))))
|
||||||
|
|
||||||
(defn zip [f a b]
|
(defn zip [f a b]
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
(+ (* y @(State.width state)) x))
|
(+ (* y @(State.width state)) x))
|
||||||
|
|
||||||
(defn get-square [state x y]
|
(defn get-square [state x y]
|
||||||
@(nth (State.grid state) (coord-to-index state x y)))
|
@(unsafe-nth (State.grid state) (coord-to-index state x y)))
|
||||||
|
|
||||||
(defn draw [rend state]
|
(defn draw [rend state]
|
||||||
(do
|
(do
|
||||||
@ -53,7 +53,7 @@
|
|||||||
(let [x @(State.x &state)
|
(let [x @(State.x &state)
|
||||||
y @(State.y &state)
|
y @(State.y &state)
|
||||||
n (coord-to-index &state x y)
|
n (coord-to-index &state x y)
|
||||||
b (nth (State.grid &state) n)]
|
b (unsafe-nth (State.grid &state) n)]
|
||||||
(do (aset! (State.grid &state) n (not @b))
|
(do (aset! (State.grid &state) n (not @b))
|
||||||
state)))
|
state)))
|
||||||
|
|
||||||
@ -67,7 +67,7 @@
|
|||||||
(let [x @(State.x &state)
|
(let [x @(State.x &state)
|
||||||
y @(State.y &state)
|
y @(State.y &state)
|
||||||
n (coord-to-index &state x y)
|
n (coord-to-index &state x y)
|
||||||
b (nth (State.grid &state) n)
|
b (unsafe-nth (State.grid &state) n)
|
||||||
dir @(State.dir &state)
|
dir @(State.dir &state)
|
||||||
new-dir (if @b (turn-left dir) (turn-right dir))
|
new-dir (if @b (turn-left dir) (turn-right dir))
|
||||||
new-x (case new-dir
|
new-x (case new-dir
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
c (Array.aset b 1 (fn [] @"World"))
|
c (Array.aset b 1 (fn [] @"World"))
|
||||||
]
|
]
|
||||||
(do
|
(do
|
||||||
(println* (@(Array.nth &c 0)))
|
(println* (@(Array.unsafe-nth &c 0)))
|
||||||
(println* (@(Array.nth &c 1))))))
|
(println* (@(Array.unsafe-nth &c 1))))))
|
||||||
|
|
||||||
(defn ex2 []
|
(defn ex2 []
|
||||||
(let [a (Array.allocate 2)
|
(let [a (Array.allocate 2)
|
||||||
@ -16,8 +16,8 @@
|
|||||||
c (Array.aset b 1 (fn [s] (String.append s "World")))
|
c (Array.aset b 1 (fn [s] (String.append s "World")))
|
||||||
]
|
]
|
||||||
(do
|
(do
|
||||||
(println* (@(Array.nth &c 0) " World"))
|
(println* (@(Array.unsafe-nth &c 0) " World"))
|
||||||
(println* (@(Array.nth &c 1) "Hello ")))))
|
(println* (@(Array.unsafe-nth &c 1) "Hello ")))))
|
||||||
|
|
||||||
(defn main []
|
(defn main []
|
||||||
(do
|
(do
|
||||||
|
@ -127,7 +127,7 @@
|
|||||||
|
|
||||||
(defn get-last-string [xs] ;; Should be generic preferably...
|
(defn get-last-string [xs] ;; Should be generic preferably...
|
||||||
(let [i (dec (Array.length &xs))]
|
(let [i (dec (Array.length &xs))]
|
||||||
(String.copy (Array.nth &xs i))))
|
(String.copy (Array.unsafe-nth &xs i))))
|
||||||
|
|
||||||
(defn print-last-string []
|
(defn print-last-string []
|
||||||
(println &(get-last-string [(String.copy "NO") (String.copy "NO") (String.copy "YES")])))
|
(println &(get-last-string [(String.copy "NO") (String.copy "NO") (String.copy "YES")])))
|
||||||
@ -196,7 +196,7 @@
|
|||||||
|
|
||||||
(defn resolve-correctly []
|
(defn resolve-correctly []
|
||||||
(let [words (words "a bb ccc dddd eeeee")]
|
(let [words (words "a bb ccc dddd eeeee")]
|
||||||
(println* (nth &words 2))))
|
(println* (unsafe-nth &words 2))))
|
||||||
|
|
||||||
(defn main []
|
(defn main []
|
||||||
; here we always use the same seed for determinism, for simple random
|
; here we always use the same seed for determinism, for simple random
|
||||||
|
@ -52,7 +52,7 @@
|
|||||||
(defn reduce-pz [t b] (+ @t (* (Planet.vz b) (Planet.mass b))))
|
(defn reduce-pz [t b] (+ @t (* (Planet.vz b) (Planet.mass b))))
|
||||||
|
|
||||||
(defn offset_momentum [bodies]
|
(defn offset_momentum [bodies]
|
||||||
(let [b (nth bodies 0)
|
(let [b (unsafe-nth bodies 0)
|
||||||
px (reduce reduce-px 0.0 bodies)
|
px (reduce reduce-px 0.0 bodies)
|
||||||
py (reduce reduce-py 0.0 bodies)
|
py (reduce reduce-py 0.0 bodies)
|
||||||
pz (reduce reduce-pz 0.0 bodies)]
|
pz (reduce reduce-pz 0.0 bodies)]
|
||||||
@ -66,14 +66,14 @@
|
|||||||
(defn energy [bodies]
|
(defn energy [bodies]
|
||||||
(let-do [e 0.0]
|
(let-do [e 0.0]
|
||||||
(for [i 0 (length bodies)]
|
(for [i 0 (length bodies)]
|
||||||
(let-do [b (nth bodies i)]
|
(let-do [b (unsafe-nth bodies i)]
|
||||||
(set! e (+ e (* 0.5 (* (Planet.mass b)
|
(set! e (+ e (* 0.5 (* (Planet.mass b)
|
||||||
(+ (ipow (Planet.vx b) 2)
|
(+ (ipow (Planet.vx b) 2)
|
||||||
(+ (ipow (Planet.vy b) 2)
|
(+ (ipow (Planet.vy b) 2)
|
||||||
(ipow (Planet.vz b) 2)))))))
|
(ipow (Planet.vz b) 2)))))))
|
||||||
|
|
||||||
(for [j (+ i 1) (length bodies)]
|
(for [j (+ i 1) (length bodies)]
|
||||||
(let [b2 (nth bodies j)
|
(let [b2 (unsafe-nth bodies j)
|
||||||
dx (- (Planet.x b) (Planet.x b2))
|
dx (- (Planet.x b) (Planet.x b2))
|
||||||
dy (- (Planet.y b) (Planet.y b2))
|
dy (- (Planet.y b) (Planet.y b2))
|
||||||
dz (- (Planet.z b) (Planet.z b2))
|
dz (- (Planet.z b) (Planet.z b2))
|
||||||
@ -83,7 +83,7 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn update-planet [i bodies dt]
|
(defn update-planet [i bodies dt]
|
||||||
(let [b (nth bodies i)]
|
(let [b (unsafe-nth bodies i)]
|
||||||
(do
|
(do
|
||||||
(Planet.set-x! b (+ (Planet.x b) (* dt (Planet.vx b))))
|
(Planet.set-x! b (+ (Planet.x b) (* dt (Planet.vx b))))
|
||||||
(Planet.set-y! b (+ (Planet.y b) (* dt (Planet.vy b))))
|
(Planet.set-y! b (+ (Planet.y b) (* dt (Planet.vy b))))
|
||||||
@ -92,9 +92,9 @@
|
|||||||
(defn advance [bodies dt]
|
(defn advance [bodies dt]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (length bodies)]
|
(for [i 0 (length bodies)]
|
||||||
(let [b (nth bodies i)]
|
(let [b (unsafe-nth bodies i)]
|
||||||
(for [j (+ i 1) (length bodies)]
|
(for [j (+ i 1) (length bodies)]
|
||||||
(let [b2 (nth bodies j)
|
(let [b2 (unsafe-nth bodies j)
|
||||||
dx (- (Planet.x b) (Planet.x b2))
|
dx (- (Planet.x b) (Planet.x b2))
|
||||||
dy (- (Planet.y b) (Planet.y b2))
|
dy (- (Planet.y b) (Planet.y b2))
|
||||||
dz (- (Planet.z b) (Planet.z b2))
|
dz (- (Planet.z b) (Planet.z b2))
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
(let-do [pairs (Bucket.entries bucket)
|
(let-do [pairs (Bucket.entries bucket)
|
||||||
result (zero)]
|
result (zero)]
|
||||||
(for [i 0 (length pairs)]
|
(for [i 0 (length pairs)]
|
||||||
(let [pair (nth pairs i)]
|
(let [pair (unsafe-nth pairs i)]
|
||||||
(when (= (Entry.key pair) &lookup-key)
|
(when (= (Entry.key pair) &lookup-key)
|
||||||
(set! result @(Entry.value pair)))))
|
(set! result @(Entry.value pair)))))
|
||||||
result))
|
result))
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
(defn handle-mouse [world]
|
(defn handle-mouse [world]
|
||||||
(let [mouse &(MouseState.get)
|
(let [mouse &(MouseState.get)
|
||||||
index (+ (/ @(x mouse) 10) (* (/ @(y mouse) 10) width))]
|
index (+ (/ @(x mouse) 10) (* (/ @(y mouse) 10) width))]
|
||||||
(aset! world index (not @(nth world index)))))
|
(aset! world index (not @(unsafe-nth world index)))))
|
||||||
|
|
||||||
(defn handle-events [app rend world play]
|
(defn handle-events [app rend world play]
|
||||||
(let [event (init)
|
(let [event (init)
|
||||||
@ -56,7 +56,7 @@
|
|||||||
(for [x 0 width]
|
(for [x 0 width]
|
||||||
(let [square (rect (* x 10) (* y 10) 9 9)]
|
(let [square (rect (* x 10) (* y 10) 9 9)]
|
||||||
(do
|
(do
|
||||||
(if @(nth world (cell-index x y))
|
(if @(unsafe-nth world (cell-index x y))
|
||||||
(set-render-draw-color rend 255 255 255 255)
|
(set-render-draw-color rend 255 255 255 255)
|
||||||
(set-render-draw-color rend 50 50 50 255))
|
(set-render-draw-color rend 50 50 50 255))
|
||||||
(render-fill-rect rend (address square))
|
(render-fill-rect rend (address square))
|
||||||
@ -69,7 +69,7 @@
|
|||||||
(< (dec width) x) 0
|
(< (dec width) x) 0
|
||||||
(< y 0) 0
|
(< y 0) 0
|
||||||
(< (dec height) y) 0
|
(< (dec height) y) 0
|
||||||
(if @(nth world (cell-index x y))
|
(if @(unsafe-nth world (cell-index x y))
|
||||||
1
|
1
|
||||||
0)))
|
0)))
|
||||||
|
|
||||||
@ -96,7 +96,7 @@
|
|||||||
(< total 2) false
|
(< total 2) false
|
||||||
(= total 3) true
|
(= total 3) true
|
||||||
(> total 3) false
|
(> total 3) false
|
||||||
@(nth world i))]
|
@(unsafe-nth world i))]
|
||||||
(aset! newWorld i newState))))
|
(aset! newWorld i newState))))
|
||||||
|
|
||||||
(defn flip []
|
(defn flip []
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
(defn draw-snake [rend snake]
|
(defn draw-snake [rend snake]
|
||||||
(let [body-length (length (body snake))]
|
(let [body-length (length (body snake))]
|
||||||
(for [i 0 body-length]
|
(for [i 0 body-length]
|
||||||
(let [part (nth (body snake) i)
|
(let [part (unsafe-nth (body snake) i)
|
||||||
x (* grid-size (to-int @(Vector2.x part)))
|
x (* grid-size (to-int @(Vector2.x part)))
|
||||||
y (* grid-size (to-int @(Vector2.y part)))]
|
y (* grid-size (to-int @(Vector2.y part)))]
|
||||||
(if (= i 0)
|
(if (= i 0)
|
||||||
@ -92,7 +92,7 @@
|
|||||||
(let [i (- (length body) 2)]
|
(let [i (- (length body) 2)]
|
||||||
(while (> i -1)
|
(while (> i -1)
|
||||||
(do
|
(do
|
||||||
(aset! body (inc i) @(nth body i))
|
(aset! body (inc i) @(unsafe-nth body i))
|
||||||
(set! i (dec i)))))
|
(set! i (dec i)))))
|
||||||
@body))
|
@body))
|
||||||
|
|
||||||
@ -139,7 +139,7 @@
|
|||||||
(defn tick [world]
|
(defn tick [world]
|
||||||
(let [s (Snake.set-dir @(World.snake world) input-dir)
|
(let [s (Snake.set-dir @(World.snake world) input-dir)
|
||||||
b (Snake.body &s)
|
b (Snake.body &s)
|
||||||
new-head (move @(nth b 0) @(dir &s))
|
new-head (move @(unsafe-nth b 0) @(dir &s))
|
||||||
new-body (aset (shift-body b) 0 new-head)
|
new-body (aset (shift-body b) 0 new-head)
|
||||||
new-snake (Snake.set-body s new-body)
|
new-snake (Snake.set-body s new-body)
|
||||||
world-after-snake-move (World.set-snake @world new-snake)
|
world-after-snake-move (World.set-snake @world new-snake)
|
||||||
|
@ -117,12 +117,12 @@
|
|||||||
"suffix-array works as expected")
|
"suffix-array works as expected")
|
||||||
(assert-equal test
|
(assert-equal test
|
||||||
5
|
5
|
||||||
@(nth &a 5)
|
@(unsafe-nth &a 5)
|
||||||
"nth works as expected")
|
"unsafe-nth works as expected")
|
||||||
(assert-equal test
|
(assert-equal test
|
||||||
&[1 2 3]
|
&[1 2 3]
|
||||||
(nth &(nested) 0)
|
(unsafe-nth &(nested) 0)
|
||||||
"nth works as expected")
|
"unsafe-nth works as expected")
|
||||||
(assert-equal test
|
(assert-equal test
|
||||||
&[10 11 12 13 14 15]
|
&[10 11 12 13 14 15]
|
||||||
&(range 10 15 1)
|
&(range 10 15 1)
|
||||||
|
@ -46,7 +46,7 @@
|
|||||||
(let [eq true]
|
(let [eq true]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(if (/= @(Array.nth a i) @(Array.nth b i))
|
(if (/= @(Array.unsafe-nth a i) @(Array.unsafe-nth b i))
|
||||||
(set! eq false)
|
(set! eq false)
|
||||||
()))
|
()))
|
||||||
eq))))
|
eq))))
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
;; It initializes a global with a something that needs to allocate memory and call functions,
|
;; It initializes a global with a something that needs to allocate memory and call functions,
|
||||||
;; then checks that the memory balance is correct after the init step.
|
;; then checks that the memory balance is correct after the init step.
|
||||||
|
|
||||||
(def g (copy (Array.nth &[@"A" @"B" @"C"] 1)))
|
(def g (copy (Array.unsafe-nth &[@"A" @"B" @"C"] 1)))
|
||||||
|
|
||||||
;; The one allocation left after 'carp_init_globals' should be 'g' itself:
|
;; The one allocation left after 'carp_init_globals' should be 'g' itself:
|
||||||
(defn main []
|
(defn main []
|
||||||
|
@ -348,7 +348,7 @@
|
|||||||
|
|
||||||
(defn lambda-3 []
|
(defn lambda-3 []
|
||||||
(let-do [stuff [100 200 300]
|
(let-do [stuff [100 200 300]
|
||||||
f (fn [n] (copy (nth &stuff n)))]
|
f (fn [n] (copy (unsafe-nth &stuff n)))]
|
||||||
(assert (= 100 (f 0)))
|
(assert (= 100 (f 0)))
|
||||||
(assert (= 200 (f 1)))
|
(assert (= 200 (f 1)))
|
||||||
(assert (= 300 (f 2)))))
|
(assert (= 300 (f 2)))))
|
||||||
|
@ -70,7 +70,7 @@
|
|||||||
"matches? works as exptected on tabs special case")
|
"matches? works as exptected on tabs special case")
|
||||||
(assert-equal test
|
(assert-equal test
|
||||||
&[@"3" @"4"]
|
&[@"3" @"4"]
|
||||||
(Array.nth &(global-match #"(\d)-(\d)" "1-2 2-3 3-4 4-5") 2)
|
(Array.unsafe-nth &(global-match #"(\d)-(\d)" "1-2 2-3 3-4 4-5") 2)
|
||||||
"global-match works as expected")
|
"global-match works as expected")
|
||||||
(assert-equal test
|
(assert-equal test
|
||||||
"1-2"
|
"1-2"
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
(let [res true]
|
(let [res true]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(if (not (Double.= @(Array.nth a i) @(Array.nth b i)))
|
(if (not (Double.= @(Array.unsafe-nth a i) @(Array.unsafe-nth b i)))
|
||||||
(set! res false)
|
(set! res false)
|
||||||
()))
|
()))
|
||||||
res))))
|
res))))
|
||||||
@ -20,7 +20,7 @@
|
|||||||
(let [res true]
|
(let [res true]
|
||||||
(do
|
(do
|
||||||
(for [i 0 (Array.length a)]
|
(for [i 0 (Array.length a)]
|
||||||
(if (not (Double.approx @(Array.nth a i) @(Array.nth b i)))
|
(if (not (Double.approx @(Array.unsafe-nth a i) @(Array.unsafe-nth b i)))
|
||||||
(set! res false)
|
(set! res false)
|
||||||
()))
|
()))
|
||||||
res))))
|
res))))
|
||||||
|
Loading…
Reference in New Issue
Block a user