mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
Adds StaticArray sum, index-of, element-count, predicate-count, aupdate! & swap!
This commit is contained in:
parent
5965b098bb
commit
acc5dbb914
@ -153,4 +153,46 @@ If the array is empty, returns `Nothing`")
|
||||
(let [x (unsafe-nth xs i)]
|
||||
(when (> result x)
|
||||
(set! result x))))
|
||||
(Maybe.Just @result)))))
|
||||
(Maybe.Just @result))))
|
||||
|
||||
(doc sum "sums an array (elements must support `+` and `zero`).")
|
||||
(defn sum [xs]
|
||||
(reduce &(fn [x y] (+ x @y)) (zero) xs))
|
||||
|
||||
(doc index-of "gets the index of element `e` in an array and wraps it on a `Just`.
|
||||
|
||||
If the element is not found, returns `Nothing`")
|
||||
(defn index-of [a e]
|
||||
(let-do [idx (Maybe.Nothing)]
|
||||
(for [i 0 (length a)]
|
||||
(when (= (unsafe-nth a i) e)
|
||||
(do
|
||||
(set! idx (Maybe.Just i))
|
||||
(break))))
|
||||
idx))
|
||||
|
||||
(doc element-count "counts the occurrences of element `e` in an array.")
|
||||
(defn element-count [a e]
|
||||
(let-do [c 0]
|
||||
(for [i 0 (length a)]
|
||||
(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 (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` in place.")
|
||||
(defn aupdate! [a i f]
|
||||
(aset! a i (~f (unsafe-nth a i))))
|
||||
|
||||
(doc swap! "swaps the indices `i` and `j` of an array `a` in place.")
|
||||
(defn swap! [a i j]
|
||||
(let-do [x @(unsafe-nth a i)
|
||||
y @(unsafe-nth a j)]
|
||||
(aset! a i y)
|
||||
(aset! a j x))))
|
||||
|
@ -163,10 +163,22 @@
|
||||
&[1 2]
|
||||
&(swap [2 1] 0 1)
|
||||
"swap works as expected")
|
||||
(assert-equal test
|
||||
&[2 1]
|
||||
&(let-do [arr [1 2]]
|
||||
(Array.swap! &arr 0 1)
|
||||
arr)
|
||||
"swap! works as expected")
|
||||
(assert-equal test
|
||||
&[1 3]
|
||||
&(aupdate [1 2] 1 &inc-ref)
|
||||
"aupdate works as expected")
|
||||
(assert-equal test
|
||||
&[1 3]
|
||||
&(let-do [arr [1 2]]
|
||||
(aupdate! &arr 1 &inc-ref)
|
||||
arr)
|
||||
"aupdate! works as expected")
|
||||
(assert-equal test
|
||||
&[1 2 3 4 5 6 7 8]
|
||||
&(concat &[[1] [2 3] [4 5 6] [7 8]])
|
||||
@ -278,6 +290,10 @@
|
||||
&(Maybe.Just 1)
|
||||
&(find-index &(fn [i] (Int.even? @i)) &[1 8 5])
|
||||
"find-index works II")
|
||||
(assert-equal test
|
||||
2
|
||||
(element-count &[1 4 3 4] &4)
|
||||
"element-count works as expected")
|
||||
(assert-equal test
|
||||
2
|
||||
(predicate-count &[1 8 5 10 3] &(fn [i] (Int.even? @i)))
|
||||
|
@ -2,6 +2,8 @@
|
||||
(use Test)
|
||||
(use StaticArray)
|
||||
|
||||
(defn inc-ref [x] (+ @x 1))
|
||||
|
||||
(deftest test
|
||||
|
||||
(assert-equal test
|
||||
@ -154,5 +156,43 @@
|
||||
(assert-equal test
|
||||
&(Maybe.Just (Pair.init 1 3))
|
||||
&(minimum $[(Pair.init 1 3) (Pair.init 2 1) (Pair.init 2 0)])
|
||||
"minimum works on pairs"))
|
||||
"minimum works on pairs")
|
||||
|
||||
(assert-equal test
|
||||
55
|
||||
(sum $[1 2 3 4 5 6 7 8 9 10])
|
||||
"sum works as expected")
|
||||
|
||||
(assert-equal test
|
||||
&(Maybe.Just 3)
|
||||
&(index-of $[1 2 3 4] &4)
|
||||
"index-of works as expected when element is in the array")
|
||||
|
||||
(assert-equal test
|
||||
&(Maybe.Nothing)
|
||||
&(index-of $[1 2 3 4] &7)
|
||||
"index-of works as expected when element is not in the array")
|
||||
|
||||
(assert-equal test
|
||||
2
|
||||
(element-count $[1 4 3 4] &4)
|
||||
"element-count works as expected")
|
||||
|
||||
(assert-equal test
|
||||
2
|
||||
(predicate-count $[1 8 5 10 3] &(fn [i] (Int.even? @i)))
|
||||
"predicate-count works")
|
||||
|
||||
(assert-equal test
|
||||
$[1 3]
|
||||
(let-do [arr $[1 2]]
|
||||
(aupdate! arr 1 &inc-ref)
|
||||
arr)
|
||||
"aupdate! works as expected")
|
||||
|
||||
(assert-equal test
|
||||
$[2 1]
|
||||
(let-do [arr $[1 2]]
|
||||
(StaticArray.swap! arr 0 1)
|
||||
arr)
|
||||
"swap! works as expected"))
|
||||
|
Loading…
Reference in New Issue
Block a user