Fix all nth usage

This commit is contained in:
GrayJack 2019-10-31 06:23:23 -03:00
parent 949d4e29ee
commit 07def7db23
21 changed files with 132 additions and 132 deletions

View File

@ -14,7 +14,7 @@ It will sum the previous sum with each new value, starting at `0`.")
(let [total x]
(do
(for [i 0 (length xs)]
(set! total (~f total (nth xs i))))
(set! total (~f total (unsafe-nth xs i))))
total)))
(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]
(let-do [res false]
(for [i 0 (length a)]
(when (~f (nth a i))
(when (~f (unsafe-nth a i))
(do
(set! res true)
(break))))
@ -35,7 +35,7 @@ It will sum the previous sum with each new value, starting at `0`.")
(defn all? [f a]
(let-do [res true]
(for [i 0 (length a)]
(when (not (~f (nth a i)))
(when (not (~f (unsafe-nth a i)))
(do
(set! res false)
(break))))
@ -47,9 +47,9 @@ If it doesnt find an element, `Nothing` will be returned.")
(defn find [f a]
(let-do [res (Maybe.Nothing)]
(for [i 0 (length a)]
(when (~f (nth a i))
(when (~f (unsafe-nth a i))
(do
(set! res (Maybe.Just @(nth a i)))
(set! res (Maybe.Just @(unsafe-nth a i)))
(break))))
res))
@ -59,7 +59,7 @@ If it doesnt find an index, `Nothing` will be returned.")
(defn find-index [f a]
(let-do [ret (Maybe.Nothing)]
(for [i 0 (length a)]
(when (~f (nth a i))
(when (~f (unsafe-nth a i))
(do
(set! ret (Maybe.Just i))
(break))))
@ -69,7 +69,7 @@ If it doesnt find an index, `Nothing` will be returned.")
Generates a runtime error if the array is empty.")
(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`.
@ -77,13 +77,13 @@ Returns `Nothing` if the array is empty.")
(defn first [a]
(if (empty? a)
(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.
Generates a runtime error if the array is empty.")
(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`.
@ -92,7 +92,7 @@ Returns `Nothing` if the array is empty.")
(defn last [a]
(if (empty? a)
(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.")
(defn = [a b]
@ -100,7 +100,7 @@ Returns `Nothing` if the array is empty.")
false
(let-do [eq true]
(for [i 0 (length a)]
(when (/= (nth a i) (nth b i))
(when (/= (unsafe-nth a i) (unsafe-nth b i))
(do
(set! eq false)
(break))))
@ -112,10 +112,10 @@ If the array is empty, it returns `Nothing`.")
(defn maximum [xs]
(if (empty? xs)
(Maybe.Nothing)
(let-do [result (nth xs 0)
(let-do [result (unsafe-nth xs 0)
n (length xs)]
(for [i 1 n]
(let [x (nth xs i)]
(let [x (unsafe-nth xs i)]
(when (< result x)
(set! result x))))
(Maybe.Just @result))))
@ -126,10 +126,10 @@ If the array is empty, returns `Nothing`")
(defn minimum [xs]
(if (empty? xs)
(Maybe.Nothing)
(let-do [result (nth xs 0)
(let-do [result (unsafe-nth xs 0)
n (length xs)]
(for [i 1 n]
(let [x (nth xs i)]
(let [x (unsafe-nth xs i)]
(when (> result x)
(set! result x))))
(Maybe.Just @result))))
@ -142,7 +142,7 @@ If the array is empty, returns `Nothing`")
(defn subarray [xs start-index end-index]
(let-do [result []]
(for [i start-index end-index]
(set! result (push-back result @(nth xs i))))
(set! result (push-back result @(unsafe-nth xs i))))
result))
(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
j (Int.dec (length &a))]
(while (Int.< i j)
(let-do [tmp @(nth &a i)]
(aset! &a i @(nth &a j))
(let-do [tmp @(unsafe-nth &a i)]
(aset! &a i @(unsafe-nth &a j))
(set! i (Int.inc i))
(aset! &a j tmp)
(set! j (Int.dec j))))
@ -171,7 +171,7 @@ If the element is not found, returns `Nothing`")
(defn index-of [a e]
(let-do [idx (Maybe.Nothing)]
(for [i 0 (length a)]
(when (= (nth a i) e)
(when (= (unsafe-nth a i) e)
(do
(set! idx (Maybe.Just i))
(break))))
@ -181,36 +181,36 @@ If the element is not found, returns `Nothing`")
(defn element-count [a e]
(let-do [c 0]
(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))
(doc predicate-count "counts the number of elements satisfying the predicate function `pred` in an array.")
(defn predicate-count [a pred]
(let-do [c 0]
(for [i 0 (length a)]
(when (~pred (nth a i))
(when (~pred (unsafe-nth a i))
(set! c (Int.inc c))))
c))
(doc aupdate "transmutes (i.e. updates) the element at index `i` of an array `a` using the function `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)))
(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]
(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`.")
(defn swap [a i j]
(let [x @(nth &a i)
y @(nth &a j)]
(let [x @(unsafe-nth &a i)
y @(unsafe-nth &a j)]
(aset (aset a i y) j x)))
(doc swap! "swaps the indices `i` and `j` of an array `a` in place.")
(defn swap! [a i j]
(let-do [x @(nth a i)
y @(nth a j)]
(let-do [x @(unsafe-nth a i)
y @(unsafe-nth a j)]
(aset! a i y)
(aset! a j x)))
@ -254,7 +254,7 @@ This function copies the array. If you dont want that, use [`endo-map`](#endo
(defn copy-map [f a]
(let-do [na (allocate (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))
(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))
na (allocate 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))
(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
lxs (Array.length xs)]
(for [i 0 lxs]
(set! sum (+ sum (Array.length (Array.nth xs i)))))
(set! sum (+ sum (Array.length (Array.unsafe-nth xs i)))))
sum))
(doc zero "returns the empty array.")
@ -288,10 +288,10 @@ The trailing elements of the longer array will be discarded.")
lxs (Array.length xs)
result (Array.allocate (sum-length xs))]
(for [i 0 lxs]
(let-do [arr (Array.nth xs i)
(let-do [arr (Array.unsafe-nth xs i)
len (Array.length arr)]
(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))))
result))
@ -312,7 +312,7 @@ The trailing elements of the longer array will be discarded.")
;;(assert (<= 0 i))
;;(assert (< i (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)))
(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]
(let-do [result false]
(for [i 0 (Array.length arr)]
(when (= el (Array.nth arr i))
(when (= el (Array.unsafe-nth arr i))
(do
(set! result true)
(break))))

View File

@ -22,9 +22,9 @@
(defn max-of-three-until! [heap i len ord]
(let-do [lchild-i (lchild 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))
(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))
i))
@ -45,9 +45,9 @@
(defn push-up! [heap i ord]
(while (/= i 0)
(let [elem (Array.nth heap i)
(let [elem (Array.unsafe-nth heap 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))
(break)
(do (Array.swap! heap i parent-i)

View File

@ -154,7 +154,7 @@
(list 'let ['xs xs
'len (list 'Array.length 'xs)]
(list 'for ['i 0 'len]
(list 'let [var (list 'Array.nth 'xs 'i)]
(list 'let [var (list 'Array.unsafe-nth 'xs 'i)]
expr))))
(defmacro foreach [binding expr]

View File

@ -56,14 +56,14 @@
l (Array.length (Bucket.entries b))
es (entries b)]
(for [i 0 l]
(when (= (Pair.a (Array.nth es i)) k)
(when (= (Pair.a (Array.unsafe-nth es i)) k)
(do
(set! ret i)
(break))))
ret))
(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]
(do (Array.aupdate! (entries &b) i &(fn [p] (Pair.set-b @p @val)))
@ -83,7 +83,7 @@
(let [i (find b k)]
(if (<= 0 i)
;; 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))))
(defn put [b k v]
@ -98,7 +98,7 @@
(defn remove [entries k]
(let-do [nentries (the (Array (Pair a b)) [])]
(for [i 0 (Array.length entries)]
(let [e (Array.nth entries i)]
(let [e (Array.unsafe-nth entries i)]
(unless (= (Pair.a e) k)
(set! nentries (Array.push-back nentries @e)))))
nentries))
@ -128,20 +128,20 @@
(defn put [m k v]
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
(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)))))))
(doc put! "Put a a value v into map m, using the key k, in place.")
(defn put! [m k v]
(let [idx (Int.positive-mod (hash k) @(n-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))))
(doc get-with-default "Get the value for the key k from map m. If it isnt found, the default is returned.")
(defn get-with-default [m k default-value]
(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 isnt found, a zero element for the value type is returned.")
(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.")
(defn get-maybe [m k]
(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.")
(defn update [m k f]
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
(update-buckets m &(fn [b]
(let [n (Array.nth &b idx)
(let [n (Array.unsafe-nth &b idx)
i (Bucket.find n k)]
(if (<= 0 i)
;; 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]
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
(update-buckets m &(fn [b]
(let [n (Array.nth &b idx)
(let [n (Array.unsafe-nth &b idx)
i (Bucket.find n k)]
(if (<= 0 i)
(Array.aset b idx (Bucket.set-idx @n i &(~f (Bucket.get-idx n i))))
@ -177,7 +177,7 @@
(defn length [m]
(let-do [c 0]
(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))
(doc empty? "Check whether the map m is empty.")
@ -187,24 +187,24 @@
(doc contains? "Check whether the map m contains the key k.")
(defn contains? [m k]
(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.")
(defn remove [m k]
(let [idx (Int.positive-mod (hash k) @(n-buckets &m))]
(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)))))))
(doc all? "Do all key-value pairs pass the given predicate (of two arguments)?")
(defn all? [pred m]
(let-do [ret true]
(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))
entries (Bucket.entries bucket)]
(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))
(set! ret false))))))
ret))
@ -217,22 +217,22 @@
(doc for-each "Execute the binary function f for all keys and values in the map m.")
(defn for-each [m f]
(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))
entries (Bucket.entries bucket)]
(for [j 0 len]
(let [e (Array.nth entries j)]
(let [e (Array.unsafe-nth entries j)]
(~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")
(defn endo-map [f m]
(do
(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))
entries (Bucket.entries bucket)]
(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)
(~f (Pair.a e) (Pair.b e))))))))
m))
@ -241,11 +241,11 @@
(defn kv-reduce [f init m]
(do
(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))
entries (Bucket.entries bucket)]
(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)))))))
init))
@ -265,7 +265,7 @@
(defn from-array [a]
(let-do [m (create)]
(for [i 0 (Array.length a)]
(let [e (Array.nth a i)
(let [e (Array.unsafe-nth a i)
k (Pair.a e)
v (Pair.b e)]
(set! m (put m k v))))
@ -303,7 +303,7 @@
es (entries b)
l (Array.length es)]
(for [i 0 l]
(when (= (Array.nth es i) k)
(when (= (Array.unsafe-nth es i) k)
(do
(set! e true)
(break))))
@ -312,7 +312,7 @@
(defn remove [entries k]
(let-do [nentries []]
(for [i 0 (Array.length entries)]
(let [e (Array.nth entries i)]
(let [e (Array.unsafe-nth entries i)]
(unless (= e k)
(set! nentries (Array.push-back nentries @e)))))
nentries))
@ -341,7 +341,7 @@
(defn put [s k]
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
(update-buckets s &(fn [b]
(let [n (Array.nth &b idx)]
(let [n (Array.unsafe-nth &b idx)]
(if (SetBucket.contains? n k)
b
(Array.aset b idx (SetBucket.grow n @k))))))))
@ -350,7 +350,7 @@
(defn put! [s k]
(let [idx (Int.positive-mod (hash k) @(n-buckets s))
b (buckets s)
n (Array.nth b idx)]
n (Array.unsafe-nth b idx)]
(when (not (SetBucket.contains? n k))
(Array.aset! b idx (SetBucket.grow n @k)))))
@ -358,7 +358,7 @@
(defn length [s]
(let-do [c 0]
(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))
(doc empty? "Check whether the set s is empty.")
@ -368,13 +368,13 @@
(doc contains? "Check whether the set s contains the key k.")
(defn contains? [s k]
(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.")
(defn remove [s k]
(let [idx (Int.positive-mod (hash k) @(n-buckets &s))]
(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)))))))
(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.")
(defn for-each [s f]
(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))
entries (SetBucket.entries bucket)]
(for [j 0 len]
(let [e (Array.nth entries j)]
(let [e (Array.unsafe-nth entries j)]
(~f e))))))
(doc from-array "Create a set from the values in array a.")
(defn from-array [a]
(let-do [s (create)]
(for [i 0 (Array.length a)]
(let [e (Array.nth a i)]
(let [e (Array.unsafe-nth a i)]
(set! s (put s e))))
s))
@ -418,11 +418,11 @@
(defn reduce [f init s]
(do
(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))
entries (SetBucket.entries bucket)]
(for [j 0 len]
(let [e (Array.nth entries j)]
(let [e (Array.unsafe-nth entries j)]
(set! init (~f init e))))))
init))

View File

@ -97,13 +97,13 @@ If you want to replace all occurrences of the pattern, use `-1`.")
lidx (Array.length &idx)
result (Array.allocate (Int.inc lidx))]
(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))]
(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)
(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))
(doc words "splits a string into words.")

View File

@ -27,7 +27,7 @@
(let [sum 0.0]
(do
(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))))
sum)))
@ -36,7 +36,7 @@
(let [sum 0.0]
(do
(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)))
(hidden _ss)
@ -52,10 +52,10 @@
(let [n (Array.length data)
sorted (Array.sorted data)]
(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
(/ (+ (the Double @(Array.nth data (dec mid)))
@(Array.nth data mid))
(/ (+ (the Double @(Array.unsafe-nth data (dec mid)))
@(Array.unsafe-nth data mid))
2.0)))))
(doc low-median "Compute the low median of the samples data.")
@ -63,8 +63,8 @@
(let [n (Array.length data)
sorted (Array.sorted data)]
(cond (= n 0) 0.0
(= (mod n 2) 1) @(Array.nth data (/ n 2))
@(Array.nth data (dec (/ n 2)))))) ; else
(= (mod n 2) 1) @(Array.unsafe-nth data (/ n 2))
@(Array.unsafe-nth data (dec (/ n 2)))))) ; else
(doc high-median "Compute the high median of the samples data.")
(defn high-median [data]
@ -72,15 +72,15 @@
sorted (Array.sorted data)]
(if (= n 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.")
(defn grouped-median [data interval]
(let [n (Array.length data)
sorted (Array.sorted data)]
(cond (= n 0) 0.0
(= n 1) @(Array.nth data 0)
(let [x @(Array.nth data (/ n 2)) ; else
(= n 1) @(Array.unsafe-nth data 0)
(let [x @(Array.unsafe-nth data (/ n 2)) ; else
l (- x (/ (from-int interval) 2.0))
cf (Maybe.from (Array.index-of data &x) -1)
f (Array.element-count data &x)]
@ -119,7 +119,7 @@
n 1.4826] ; taken from Rust and R, because thats how its done apparently
(do
(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))))
(hidden median-abs-dev-pct)
@ -132,15 +132,15 @@
(Int.= 0 (Array.length sorted)) -1.0 ; should abort here
(Double.< pct 0.0) -1.0 ; should abort here
(Double.> pct 100.0) -1.0 ; should abort here
(Int.= 1 (Array.length sorted)) @(Array.nth sorted 0)
(Double.= 100.0 pct) @(Array.nth sorted (Int.dec (Array.length sorted)))
(Int.= 1 (Array.length sorted)) @(Array.unsafe-nth sorted 0)
(Double.= 100.0 pct) @(Array.unsafe-nth sorted (Int.dec (Array.length sorted)))
(let [len (Int.dec (Array.length sorted))
rank (Double.* (Double./ pct 100.0) (Double.from-int len))
lrank (Double.floor rank)
d (Double.- rank lrank)
n (Double.to-int lrank)
lo @(Array.nth sorted n)
hi @(Array.nth sorted (Int.inc n))]
lo @(Array.unsafe-nth sorted n)
hi @(Array.unsafe-nth sorted (Int.inc n))]
(Double.+ lo (Double.* d (Double.- hi lo))))))
(doc quartiles "Compute the quartiles of the samples data.")
@ -157,7 +157,7 @@
(doc iqr "Compute the interquartile range.")
(defn iqr [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)
(defn winsorize [samples pct]
@ -166,7 +166,7 @@
hi (Statistics.percentile-of-sorted tmp (Double.- 100.0 pct))]
(do
(for [i 0 (Array.length tmp)]
(let [samp @(Array.nth tmp i)]
(let [samp @(Array.unsafe-nth tmp i)]
(cond
(> samp hi) (Array.aset! tmp i hi)
(< samp lo) (Array.aset! tmp i lo)

View File

@ -89,7 +89,7 @@
(let-do [sum 0
lstrings (Array.length strings)]
(for [i 0 lstrings]
(set! sum (+ sum (String.length (Array.nth strings i)))))
(set! sum (+ sum (String.length (Array.unsafe-nth strings i)))))
sum))
(doc concat "Returns a new string which is the concatenation of the provided `strings`.")
@ -102,7 +102,7 @@
lstrings (Array.length strings)
result (String.allocate (sum-length strings) \ )]
(for [i 0 lstrings]
(let-do [str (Array.nth strings i)
(let-do [str (Array.unsafe-nth strings i)
len (String.length str)]
(string-set-at! &result j str)
(set! j (+ j len))))
@ -117,7 +117,7 @@
seps-size (* num-seps sep-length)
result (String.allocate (+ seps-size (sum-length strings)) \ )]
(for [i 0 lstrings]
(let-do [str (Array.nth strings i)
(let-do [str (Array.unsafe-nth strings i)
len (String.length str)]
(when (> i 0)
(do
@ -135,7 +135,7 @@
sep-length (max 0 (- lstrings 1))
result (String.allocate (+ sep-length (sum-length strings)) \ )]
(for [i 0 lstrings]
(let-do [str (Array.nth strings i)
(let-do [str (Array.unsafe-nth strings i)
len (String.length str)]
(when (> i 0)
(do

View File

@ -219,8 +219,8 @@
(let [total (Array.allocate (Array.length a))]
(do
(for [i 0 (Array.length a)]
(Array.aset-uninitialized! &total i (f @(Array.nth a i)
@(Array.nth b i))))
(Array.aset-uninitialized! &total i (f @(Array.unsafe-nth a i)
@(Array.unsafe-nth b i))))
(init (Array.length a) total))))
(defn zip [f a b]

View File

@ -22,7 +22,7 @@
(+ (* y @(State.width state)) x))
(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]
(do
@ -53,7 +53,7 @@
(let [x @(State.x &state)
y @(State.y &state)
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))
state)))
@ -67,7 +67,7 @@
(let [x @(State.x &state)
y @(State.y &state)
n (coord-to-index &state x y)
b (nth (State.grid &state) n)
b (unsafe-nth (State.grid &state) n)
dir @(State.dir &state)
new-dir (if @b (turn-left dir) (turn-right dir))
new-x (case new-dir

View File

@ -7,8 +7,8 @@
c (Array.aset b 1 (fn [] @"World"))
]
(do
(println* (@(Array.nth &c 0)))
(println* (@(Array.nth &c 1))))))
(println* (@(Array.unsafe-nth &c 0)))
(println* (@(Array.unsafe-nth &c 1))))))
(defn ex2 []
(let [a (Array.allocate 2)
@ -16,8 +16,8 @@
c (Array.aset b 1 (fn [s] (String.append s "World")))
]
(do
(println* (@(Array.nth &c 0) " World"))
(println* (@(Array.nth &c 1) "Hello ")))))
(println* (@(Array.unsafe-nth &c 0) " World"))
(println* (@(Array.unsafe-nth &c 1) "Hello ")))))
(defn main []
(do

View File

@ -127,7 +127,7 @@
(defn get-last-string [xs] ;; Should be generic preferably...
(let [i (dec (Array.length &xs))]
(String.copy (Array.nth &xs i))))
(String.copy (Array.unsafe-nth &xs i))))
(defn print-last-string []
(println &(get-last-string [(String.copy "NO") (String.copy "NO") (String.copy "YES")])))
@ -196,7 +196,7 @@
(defn resolve-correctly []
(let [words (words "a bb ccc dddd eeeee")]
(println* (nth &words 2))))
(println* (unsafe-nth &words 2))))
(defn main []
; here we always use the same seed for determinism, for simple random

View File

@ -52,7 +52,7 @@
(defn reduce-pz [t b] (+ @t (* (Planet.vz b) (Planet.mass b))))
(defn offset_momentum [bodies]
(let [b (nth bodies 0)
(let [b (unsafe-nth bodies 0)
px (reduce reduce-px 0.0 bodies)
py (reduce reduce-py 0.0 bodies)
pz (reduce reduce-pz 0.0 bodies)]
@ -66,14 +66,14 @@
(defn energy [bodies]
(let-do [e 0.0]
(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)
(+ (ipow (Planet.vx b) 2)
(+ (ipow (Planet.vy b) 2)
(ipow (Planet.vz b) 2)))))))
(for [j (+ i 1) (length bodies)]
(let [b2 (nth bodies j)
(let [b2 (unsafe-nth bodies j)
dx (- (Planet.x b) (Planet.x b2))
dy (- (Planet.y b) (Planet.y b2))
dz (- (Planet.z b) (Planet.z b2))
@ -83,7 +83,7 @@
(defn update-planet [i bodies dt]
(let [b (nth bodies i)]
(let [b (unsafe-nth bodies i)]
(do
(Planet.set-x! b (+ (Planet.x b) (* dt (Planet.vx b))))
(Planet.set-y! b (+ (Planet.y b) (* dt (Planet.vy b))))
@ -92,9 +92,9 @@
(defn advance [bodies dt]
(do
(for [i 0 (length bodies)]
(let [b (nth bodies i)]
(let [b (unsafe-nth bodies i)]
(for [j (+ i 1) (length bodies)]
(let [b2 (nth bodies j)
(let [b2 (unsafe-nth bodies j)
dx (- (Planet.x b) (Planet.x b2))
dy (- (Planet.y b) (Planet.y b2))
dz (- (Planet.z b) (Planet.z b2))

View File

@ -15,7 +15,7 @@
(let-do [pairs (Bucket.entries bucket)
result (zero)]
(for [i 0 (length pairs)]
(let [pair (nth pairs i)]
(let [pair (unsafe-nth pairs i)]
(when (= (Entry.key pair) &lookup-key)
(set! result @(Entry.value pair)))))
result))

View File

@ -29,7 +29,7 @@
(defn handle-mouse [world]
(let [mouse &(MouseState.get)
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]
(let [event (init)
@ -56,7 +56,7 @@
(for [x 0 width]
(let [square (rect (* x 10) (* y 10) 9 9)]
(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 50 50 50 255))
(render-fill-rect rend (address square))
@ -69,7 +69,7 @@
(< (dec width) x) 0
(< y 0) 0
(< (dec height) y) 0
(if @(nth world (cell-index x y))
(if @(unsafe-nth world (cell-index x y))
1
0)))
@ -96,7 +96,7 @@
(< total 2) false
(= total 3) true
(> total 3) false
@(nth world i))]
@(unsafe-nth world i))]
(aset! newWorld i newState))))
(defn flip []

View File

@ -31,7 +31,7 @@
(defn draw-snake [rend snake]
(let [body-length (length (body snake))]
(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)))
y (* grid-size (to-int @(Vector2.y part)))]
(if (= i 0)
@ -92,7 +92,7 @@
(let [i (- (length body) 2)]
(while (> i -1)
(do
(aset! body (inc i) @(nth body i))
(aset! body (inc i) @(unsafe-nth body i))
(set! i (dec i)))))
@body))
@ -139,7 +139,7 @@
(defn tick [world]
(let [s (Snake.set-dir @(World.snake world) input-dir)
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-snake (Snake.set-body s new-body)
world-after-snake-move (World.set-snake @world new-snake)

View File

@ -117,12 +117,12 @@
"suffix-array works as expected")
(assert-equal test
5
@(nth &a 5)
"nth works as expected")
@(unsafe-nth &a 5)
"unsafe-nth works as expected")
(assert-equal test
&[1 2 3]
(nth &(nested) 0)
"nth works as expected")
(unsafe-nth &(nested) 0)
"unsafe-nth works as expected")
(assert-equal test
&[10 11 12 13 14 15]
&(range 10 15 1)

View File

@ -46,7 +46,7 @@
(let [eq true]
(do
(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)
()))
eq))))

View File

@ -2,7 +2,7 @@
;; 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.
(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:
(defn main []

View File

@ -348,7 +348,7 @@
(defn lambda-3 []
(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 (= 200 (f 1)))
(assert (= 300 (f 2)))))

View File

@ -70,7 +70,7 @@
"matches? works as exptected on tabs special case")
(assert-equal test
&[@"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")
(assert-equal test
"1-2"

View File

@ -9,7 +9,7 @@
(let [res true]
(do
(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)
()))
res))))
@ -20,7 +20,7 @@
(let [res true]
(do
(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)
()))
res))))