mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
Renaming: String.count -> String.length, Array.count -> Array.length
Issue #236
This commit is contained in:
parent
5c280fea4b
commit
7c1dd210d7
@ -8,10 +8,10 @@
|
||||
(ignore (let-do [a [0]]
|
||||
(for [i 1 n]
|
||||
(set! a (Array.push-back a i)))
|
||||
(assert (= n (Array.count &a)))
|
||||
(assert (= n (Array.length &a)))
|
||||
(for [i 1 n]
|
||||
(set! a (Array.pop-back a)))
|
||||
(assert (= 1 (Array.count &a))))))
|
||||
(assert (= 1 (Array.length &a))))))
|
||||
|
||||
(defn main []
|
||||
(do
|
||||
|
@ -9,12 +9,12 @@
|
||||
(defn some-subarray []
|
||||
(let-do [a (replicate n &1)
|
||||
b (subarray &a 0 (/ n 2))]
|
||||
(assert (= (/ n 2) (count &b)))))
|
||||
(assert (= (/ n 2) (length &b)))))
|
||||
|
||||
(defn perform-bench [new-n]
|
||||
(do
|
||||
(set! n new-n)
|
||||
(println* "\nTaking sub-array of array with count " n)
|
||||
(println* "\nTaking sub-array of array with length " n)
|
||||
(bench some-subarray)))
|
||||
|
||||
(defn main []
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
(defn perform-bench [n]
|
||||
(do
|
||||
(println* "\nSwap with array count " n)
|
||||
(println* "\nSwap with array length " n)
|
||||
(set! a (Array.replicate n &1))
|
||||
(bench some-swapping)))
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
|
||||
(defn perform-mutable-bench [n]
|
||||
(do
|
||||
(println* "\nMutable swap with array count " n)
|
||||
(println* "\nMutable swap with array length " n)
|
||||
(set! a (Array.replicate n &1))
|
||||
(bench some-mutable-swapping)))
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
(defn perform-bench [new-n]
|
||||
(do
|
||||
(set! n new-n)
|
||||
(println* "\nUpdating array with count " n)
|
||||
(println* "\nUpdating array with length " n)
|
||||
(bench some-updating)))
|
||||
|
||||
(defn main []
|
||||
|
@ -4,7 +4,7 @@
|
||||
(defn reduce [f x xs]
|
||||
(let [total x]
|
||||
(do
|
||||
(for [i 0 (count xs)]
|
||||
(for [i 0 (length xs)]
|
||||
(set! total (f &total (nth xs i))))
|
||||
total)))
|
||||
|
||||
@ -14,14 +14,14 @@
|
||||
|
||||
(doc last "Take the last element of an array.")
|
||||
(defn last [a]
|
||||
@(Array.nth a (Int.dec (Array.count a))))
|
||||
@(Array.nth a (Int.dec (Array.length a))))
|
||||
|
||||
(doc = "Compare two arrays.")
|
||||
(defn = [a b]
|
||||
(if (/= (count a) (count b))
|
||||
(if (/= (length a) (length b))
|
||||
false
|
||||
(let-do [eq true]
|
||||
(for [i 0 (count a)]
|
||||
(for [i 0 (length a)]
|
||||
(when (/= @(nth a i) @(nth b i))
|
||||
(do
|
||||
(set! eq false)
|
||||
@ -31,7 +31,7 @@
|
||||
(doc maximum "Get the maximum in an array (elements must support <).")
|
||||
(defn maximum [xs]
|
||||
(let [result (first xs)
|
||||
n (count xs)]
|
||||
n (length xs)]
|
||||
(do
|
||||
(for [i 1 n]
|
||||
(let [x @(nth xs i)]
|
||||
@ -43,7 +43,7 @@
|
||||
(doc minimum "Get the maximum in an array (elements must support >).")
|
||||
(defn minimum [xs]
|
||||
(let [result (first xs)
|
||||
n (count xs)]
|
||||
n (length xs)]
|
||||
(do
|
||||
(for [i 1 n]
|
||||
(let [x @(nth xs i)]
|
||||
@ -70,12 +70,12 @@
|
||||
|
||||
(doc suffix-array "Get subarray from start-index.")
|
||||
(defn suffix-array [xs start-index]
|
||||
(subarray xs start-index (count xs)))
|
||||
(subarray xs start-index (length xs)))
|
||||
|
||||
(doc reverse "Reverse an array.")
|
||||
(defn reverse [a]
|
||||
(let-do [i 0
|
||||
j (Int.dec (count &a))]
|
||||
j (Int.dec (length &a))]
|
||||
(while (Int.< i j)
|
||||
(let-do [tmp @(nth &a i)]
|
||||
(aset! &a i @(nth &a j))
|
||||
@ -87,7 +87,7 @@
|
||||
(doc index-of "Get the index of element e in an array.")
|
||||
(defn index-of [a e]
|
||||
(let-do [idx -1]
|
||||
(for [i 0 (count a)]
|
||||
(for [i 0 (length a)]
|
||||
(when (= (nth a i) &e)
|
||||
(do
|
||||
(set! idx i)
|
||||
@ -97,7 +97,7 @@
|
||||
(doc element-count "Count occurrences of element e in an array.")
|
||||
(defn element-count [a e]
|
||||
(let-do [c 0]
|
||||
(for [i 0 (count a)]
|
||||
(for [i 0 (length a)]
|
||||
(when (= e (nth a i)) (set! c (Int.inc c))))
|
||||
c))
|
||||
|
||||
@ -161,31 +161,31 @@
|
||||
|
||||
(doc copy-map "Map over array a using function f (copies the array).")
|
||||
(defn copy-map [f a]
|
||||
(let-do [na (allocate (count a))]
|
||||
(for [i 0 (count a)]
|
||||
(let-do [na (allocate (length a))]
|
||||
(for [i 0 (length a)]
|
||||
(aset-uninitialized! &na i (f (nth a i))))
|
||||
na))
|
||||
|
||||
(doc sum-count "Returns the sum of counts from an Array of Arrays.")
|
||||
(defn sum-count [xs]
|
||||
(doc sum-length "Returns the sum of lengths from an Array of Arrays.")
|
||||
(defn sum-length [xs]
|
||||
(let-do [sum 0
|
||||
lxs (Array.count xs)]
|
||||
lxs (Array.length xs)]
|
||||
(for [i 0 lxs]
|
||||
(set! sum (+ sum (Array.count (Array.nth xs i)))))
|
||||
(set! sum (+ sum (Array.length (Array.nth xs i)))))
|
||||
sum))
|
||||
|
||||
(doc concat "Returns a new Array which is the concatenation of the provided `xs`.")
|
||||
(defn concat [xs]
|
||||
;; This is using a StringBuilder pattern to only perform one allocation and
|
||||
;; to only copy each of the incoming Array(s) once.
|
||||
;; This currently performs wasted Array.count calls, as we call it for each
|
||||
;; Array once here and once in sum-count.
|
||||
;; This currently performs wasted Array.length calls, as we call it for each
|
||||
;; Array once here and once in sum-length.
|
||||
(let-do [j 0
|
||||
lxs (Array.count xs)
|
||||
result (Array.allocate (sum-count xs))]
|
||||
lxs (Array.length xs)
|
||||
result (Array.allocate (sum-length xs))]
|
||||
(for [i 0 lxs]
|
||||
(let-do [arr (Array.nth xs i)
|
||||
len (Array.count arr)]
|
||||
len (Array.length arr)]
|
||||
(for [k 0 len]
|
||||
(aset-uninitialized! &result (+ j k) @(Array.nth arr k)))
|
||||
(set! j (+ j len))))
|
||||
|
@ -10,7 +10,7 @@
|
||||
(doc dir-from-path "Removes the file-name part of a path to a file.")
|
||||
(defn dir-from-path [path]
|
||||
(let [segments (split-by path &[\/])
|
||||
n (dec (count &segments))
|
||||
n (dec (length &segments))
|
||||
without-last (prefix-array &segments n)]
|
||||
(concat &(copy-map append-slash &without-last))))
|
||||
|
||||
|
@ -2,20 +2,20 @@
|
||||
(hidden fmt-internal)
|
||||
(defdynamic fmt-internal [s args]
|
||||
(let [idx (String.index-of s \%)
|
||||
len (String.count s)]
|
||||
len (String.length s)]
|
||||
(if (= idx -1)
|
||||
(list 'copy s) ; no more splits found, just return string
|
||||
(if (= \% (String.char-at s (inc idx))) ; this is an escaped %
|
||||
(list 'StringCopy.append
|
||||
(list 'copy "%")
|
||||
(fmt-internal (String.substring s (+ idx 2) len) args))
|
||||
(if (= 0 (count args)) ; we need to insert something, but have nothing
|
||||
(if (= 0 (length args)) ; we need to insert something, but have nothing
|
||||
(macro-error "error in format string: not enough arguments to format string")
|
||||
; okay, this is the meat:
|
||||
; get the next % after our escaper
|
||||
(let [next (String.index-of (String.substring s (inc idx) len) \%)]
|
||||
(if (= -1 next)
|
||||
(if (< 1 (count args))
|
||||
(if (< 1 (length args))
|
||||
(macro-error "error in format string: too many arguments to format string")
|
||||
(list 'format s (car args)))
|
||||
(let [slice (String.substring s 0 (+ (inc idx) next))]
|
||||
|
@ -78,11 +78,11 @@
|
||||
)
|
||||
|
||||
(defdynamic cond-internal [xs]
|
||||
(if (= (count xs) 0)
|
||||
(if (= (length xs) 0)
|
||||
(list)
|
||||
(if (= (count xs) 2)
|
||||
(if (= (length xs) 2)
|
||||
(macro-error "cond has even number of branches; add an else branch")
|
||||
(if (= (count xs) 1)
|
||||
(if (= (length xs) 1)
|
||||
(car xs)
|
||||
(list
|
||||
'if
|
||||
@ -94,7 +94,7 @@
|
||||
(cond-internal xs))
|
||||
|
||||
(defmacro for [settings :rest body] ;; settings = variable, from, to, <step>
|
||||
(if (> (count body) 1)
|
||||
(if (> (length body) 1)
|
||||
(macro-error "Warning: the body of the 'for' loop can only contain one expression")
|
||||
(list
|
||||
'let
|
||||
@ -103,7 +103,7 @@
|
||||
'while
|
||||
(list 'Int.< (car settings) (caddr settings))
|
||||
(list 'do
|
||||
(if (= (count body) 0)
|
||||
(if (= (length body) 0)
|
||||
()
|
||||
(if (list? body)
|
||||
(car body)
|
||||
@ -112,7 +112,7 @@
|
||||
'set! (car settings)
|
||||
(list 'Int.+
|
||||
(car settings)
|
||||
(if (= 4 (count settings)) ;; optional arg for step
|
||||
(if (= 4 (length settings)) ;; optional arg for step
|
||||
(cadddr settings)
|
||||
1))))))))
|
||||
|
||||
@ -122,12 +122,12 @@
|
||||
|
||||
;; Old foreach, what's a better name for this? (it's just 'map' with side effects)
|
||||
;; (defmacro foreach [f xs]
|
||||
;; (list 'for ['i 0 (list 'Array.count (list 'ref xs))]
|
||||
;; (list 'for ['i 0 (list 'Array.length (list 'ref xs))]
|
||||
;; (list f (list 'Array.nth (list 'ref xs) 'i))))
|
||||
|
||||
(defdynamic foreach-internal [var xs expr]
|
||||
(list 'let ['xs xs
|
||||
'len (list 'Array.count 'xs)]
|
||||
'len (list 'Array.length 'xs)]
|
||||
(list 'for ['i 0 'len]
|
||||
(list 'let [var (list 'Array.nth 'xs 'i)]
|
||||
expr))))
|
||||
@ -138,7 +138,7 @@
|
||||
(macro-error "Binding has to be an array.")))
|
||||
|
||||
(defdynamic thread-first-internal [xs]
|
||||
(if (= (count xs) 2)
|
||||
(if (= (length xs) 2)
|
||||
(if (list? (last xs))
|
||||
(cons (caadr xs)
|
||||
(cons (car xs)
|
||||
@ -153,7 +153,7 @@
|
||||
(list (last xs) (thread-first-internal (all-but-last xs))))))
|
||||
|
||||
(defdynamic thread-last-internal [xs]
|
||||
(if (= (count xs) 2)
|
||||
(if (= (length xs) 2)
|
||||
(if (list? (last xs))
|
||||
(cons-last (car xs) (last xs))
|
||||
(list (cadr xs) (car xs)))
|
||||
@ -194,9 +194,9 @@
|
||||
()))
|
||||
|
||||
(defdynamic use-all-fn [names]
|
||||
(if (= (count names) 0)
|
||||
(if (= (length names) 0)
|
||||
(macro-error "Trying to call use-all without arguments")
|
||||
(if (= (count names) 1)
|
||||
(if (= (length names) 1)
|
||||
(list (list 'use (car names)))
|
||||
(cons (list 'use (car names)) (use-all-fn (cdr names))))));(use-all (cdr names))))))
|
||||
|
||||
@ -228,11 +228,11 @@
|
||||
(list 'while true (cons 'do forms)))
|
||||
|
||||
(defdynamic case-internal [name xs]
|
||||
(if (= (count xs) 0)
|
||||
(if (= (length xs) 0)
|
||||
(list)
|
||||
(if (= (count xs) 2)
|
||||
(if (= (length xs) 2)
|
||||
(macro-error "case has even number of branches; add an else branch")
|
||||
(if (= (count xs) 1)
|
||||
(if (= (length xs) 1)
|
||||
(car xs)
|
||||
(list 'if
|
||||
(list '= name (car xs))
|
||||
@ -243,9 +243,9 @@
|
||||
(case-internal name forms))
|
||||
|
||||
(defdynamic build-vararg [fn forms]
|
||||
(if (= (count forms) 0)
|
||||
(if (= (length forms) 0)
|
||||
(macro-error "vararg macro needs at least one argument")
|
||||
(if (= (count forms) 1)
|
||||
(if (= (length forms) 1)
|
||||
(car forms)
|
||||
(list fn (car forms) (build-vararg fn (cdr forms))))))
|
||||
|
||||
@ -256,9 +256,9 @@
|
||||
(build-vararg 'or forms))
|
||||
|
||||
(defdynamic build-str* [forms]
|
||||
(if (= (count forms) 0)
|
||||
(if (= (length forms) 0)
|
||||
(list "")
|
||||
(if (= (count forms) 1)
|
||||
(if (= (length forms) 1)
|
||||
(list 'str (car forms))
|
||||
(list 'StringCopy.append (list 'str (car forms)) (build-str* (cdr forms))))))
|
||||
|
||||
|
@ -18,21 +18,21 @@
|
||||
])
|
||||
|
||||
(defn sum [data]
|
||||
(if (= 0 (Array.count data))
|
||||
(if (= 0 (Array.length data))
|
||||
0.0
|
||||
(let [total @(Array.nth data 0)]
|
||||
(do
|
||||
(for [i 1 (Array.count data)]
|
||||
(for [i 1 (Array.length data)]
|
||||
(set! total (+ total @(Array.nth data i))))
|
||||
total))))
|
||||
|
||||
(defn mean [data]
|
||||
(/ (Statistics.sum data) (from-int (Array.count data))))
|
||||
(/ (Statistics.sum data) (from-int (Array.length data))))
|
||||
|
||||
(defn _pp [a mean]
|
||||
(let [sum 0.0]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(for [i 0 (Array.length a)]
|
||||
(let [tmp (Double.- (Double.copy (Array.nth a i)) mean)]
|
||||
(set! sum (Double.* tmp tmp))))
|
||||
sum)))
|
||||
@ -40,7 +40,7 @@
|
||||
(defn _xx [a mean]
|
||||
(let [sum 0.0]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(for [i 0 (Array.length a)]
|
||||
(set! sum (Double.- (Double.copy (Array.nth a i)) mean)))
|
||||
sum)))
|
||||
|
||||
@ -49,10 +49,10 @@
|
||||
tmp (_xx data m)]
|
||||
(Double.- (_pp data m)
|
||||
(Double./ (Double.* tmp tmp)
|
||||
(Double.from-int (Array.count data))))))
|
||||
(Double.from-int (Array.length data))))))
|
||||
|
||||
(defn median [data]
|
||||
(let [n (Array.count data)
|
||||
(let [n (Array.length data)
|
||||
sorted (Array.sort @data)]
|
||||
(cond (= n 0) 0.0
|
||||
(= (mod n 2) 1) @(Array.nth data (/ n 2))
|
||||
@ -62,21 +62,21 @@
|
||||
2.0)))))
|
||||
|
||||
(defn low-median [data]
|
||||
(let [n (Array.count data)
|
||||
(let [n (Array.length data)
|
||||
sorted (Array.sort @data)]
|
||||
(cond (= n 0) 0.0
|
||||
(= (mod n 2) 1) @(Array.nth data (/ n 2))
|
||||
@(Array.nth data (dec (/ n 2)))))) ; else
|
||||
|
||||
(defn high-median [data]
|
||||
(let [n (Array.count data)
|
||||
(let [n (Array.length data)
|
||||
sorted (Array.sort @data)]
|
||||
(if (= n 0)
|
||||
0.0
|
||||
@(Array.nth data (/ n 2)))))
|
||||
|
||||
(defn grouped-median [data interval]
|
||||
(let [n (Array.count data)
|
||||
(let [n (Array.length data)
|
||||
sorted (Array.sort @data)]
|
||||
(cond (= n 0) 0.0
|
||||
(= n 1) @(Array.nth data 0)
|
||||
@ -88,12 +88,12 @@
|
||||
(from-int f)))))))
|
||||
|
||||
(defn variance [data]
|
||||
(let [n (Array.count data)
|
||||
(let [n (Array.length data)
|
||||
ss (_ss data)]
|
||||
(/ ss (from-int (dec n)))))
|
||||
|
||||
(defn pvariance [data]
|
||||
(let [n (Array.count data)
|
||||
(let [n (Array.length data)
|
||||
ss (_ss data)]
|
||||
(/ ss (from-int n))))
|
||||
|
||||
@ -109,10 +109,10 @@
|
||||
(defn median-abs-dev [data]
|
||||
(let [med (median data)
|
||||
zero 0.0
|
||||
abs-devs (Array.replicate (Array.count data) &zero)
|
||||
abs-devs (Array.replicate (Array.length data) &zero)
|
||||
n 1.4826] ; taken from Rust and R, because that’s how it’s done apparently
|
||||
(do
|
||||
(for [i 0 (Array.count data)]
|
||||
(for [i 0 (Array.length data)]
|
||||
(Array.aset! &abs-devs i (abs (- med @(Array.nth data i)))))
|
||||
(* (median &abs-devs) n))))
|
||||
|
||||
@ -121,12 +121,12 @@
|
||||
|
||||
(defn percentile-of-sorted [sorted pct]
|
||||
(cond
|
||||
(Int.= 0 (Array.count 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 100.0) -1.0 ; should abort here
|
||||
(Int.= 1 (Array.count sorted)) @(Array.nth sorted 0)
|
||||
(Double.= 100.0 pct) @(Array.nth sorted (Int.dec (Array.count sorted)))
|
||||
(let [len (Int.dec (Array.count sorted))
|
||||
(Int.= 1 (Array.length sorted)) @(Array.nth sorted 0)
|
||||
(Double.= 100.0 pct) @(Array.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)
|
||||
@ -154,7 +154,7 @@
|
||||
lo (Statistics.percentile-of-sorted tmp pct)
|
||||
hi (Statistics.percentile-of-sorted tmp (Double.- 100.0 pct))]
|
||||
(do
|
||||
(for [i 0 (Array.count tmp)]
|
||||
(for [i 0 (Array.length tmp)]
|
||||
(let [samp @(Array.nth tmp i)]
|
||||
(cond
|
||||
(> samp hi) (Array.aset! tmp i hi)
|
||||
|
@ -6,7 +6,7 @@
|
||||
(register append (Fn [&String &String] String))
|
||||
(register delete (Fn [String] ()))
|
||||
(register copy (Fn [&String] String))
|
||||
(register count (Fn [&String] Int))
|
||||
(register length (Fn [&String] Int))
|
||||
(register cstr (Fn [&String] (Ptr Char)))
|
||||
(register str (Fn [&String] String))
|
||||
(register prn (Fn [&String] String))
|
||||
@ -37,17 +37,17 @@
|
||||
str)))
|
||||
|
||||
(defn pad-left [len pad s]
|
||||
(let [x (Int.max 0 (- len (count s)))]
|
||||
(let [x (Int.max 0 (- len (length s)))]
|
||||
(append &(from-chars &(Array.replicate x &pad)) s)))
|
||||
|
||||
(defn pad-right [len pad s]
|
||||
(let [x (Int.max 0 (- len (count s)))]
|
||||
(let [x (Int.max 0 (- len (length s)))]
|
||||
(append s &(from-chars &(Array.replicate x &pad)))))
|
||||
|
||||
(doc count-char "Returns the number of occurrences of `c` in the string `s`.")
|
||||
(defn count-char [s c]
|
||||
(let-do [n 0]
|
||||
(for [i 0 (count s)]
|
||||
(for [i 0 (length s)]
|
||||
(when (= c (char-at s i))
|
||||
(set! n (Int.inc n))))
|
||||
n))
|
||||
@ -58,7 +58,7 @@
|
||||
|
||||
(doc empty? "Check if the string is the empty string.")
|
||||
(defn empty? [s]
|
||||
(Int.= (count s) 0))
|
||||
(Int.= (length s) 0))
|
||||
|
||||
(defn substring [s a b]
|
||||
(from-chars &(Array.subarray &(chars s) a b)))
|
||||
@ -73,35 +73,35 @@
|
||||
|
||||
(doc starts-with? "Check if the string `s` begins with the string `sub`.")
|
||||
(defn starts-with? [s sub]
|
||||
(= sub &(prefix-string s (count sub))))
|
||||
(= sub &(prefix-string s (length sub))))
|
||||
|
||||
(doc starts-with? "Check if the string `s` ends with the string `sub`.")
|
||||
(defn ends-with? [s sub]
|
||||
(= sub &(suffix-string s (- (count s) (count sub)))))
|
||||
(= sub &(suffix-string s (- (length s) (length sub)))))
|
||||
|
||||
(doc zero "The empty string.")
|
||||
(defn zero [] @"")
|
||||
|
||||
(doc sum-count "Returns the sum of counts from an array of Strings.")
|
||||
(defn sum-count [strings]
|
||||
(doc sum-length "Returns the sum of lengths from an array of Strings.")
|
||||
(defn sum-length [strings]
|
||||
(let-do [sum 0
|
||||
lstrings (Array.count strings)]
|
||||
lstrings (Array.length strings)]
|
||||
(for [i 0 lstrings]
|
||||
(set! sum (+ sum (String.count (Array.nth strings i)))))
|
||||
(set! sum (+ sum (String.length (Array.nth strings i)))))
|
||||
sum))
|
||||
|
||||
(doc concat "Returns a new string which is the concatenation of the provided `strings`.")
|
||||
(defn concat [strings]
|
||||
;; This is using a StringBuilder pattern to only perform one allocation and
|
||||
;; to only copy each of the incoming strings once.
|
||||
;; This currently performs wasted String.count calls, as we call it for each
|
||||
;; string once here and once in sum-count.
|
||||
;; This currently performs wasted String.length calls, as we call it for each
|
||||
;; string once here and once in sum-length.
|
||||
(let-do [j 0
|
||||
lstrings (Array.count strings)
|
||||
result (String.allocate (sum-count strings) \ )]
|
||||
lstrings (Array.length strings)
|
||||
result (String.allocate (sum-length strings) \ )]
|
||||
(for [i 0 lstrings]
|
||||
(let-do [str (Array.nth strings i)
|
||||
len (String.count str)]
|
||||
len (String.length str)]
|
||||
(string-set-at! &result j str)
|
||||
(set! j (+ j len))))
|
||||
result))
|
||||
@ -109,14 +109,14 @@
|
||||
(doc join "Returns a new string which is the concatenation of the provided `strings` separated by string `sep`.")
|
||||
(defn join [sep strings]
|
||||
(let-do [j 0
|
||||
lstrings (Array.count strings)
|
||||
lstrings (Array.length strings)
|
||||
num-seps (- lstrings 1)
|
||||
sep-length (String.count &sep)
|
||||
sep-length (String.length &sep)
|
||||
seps-size (* num-seps sep-length)
|
||||
result (String.allocate (+ seps-size (sum-count strings)) \ )]
|
||||
result (String.allocate (+ seps-size (sum-length strings)) \ )]
|
||||
(for [i 0 lstrings]
|
||||
(let-do [str (Array.nth strings i)
|
||||
len (String.count str)]
|
||||
len (String.length str)]
|
||||
(when (> i 0)
|
||||
(do
|
||||
(string-set-at! &result j &sep)
|
||||
@ -129,12 +129,12 @@
|
||||
(defn join-with-char [sep strings]
|
||||
;; (= (join-with-char \ ["Hello" "world"]) (join " " ["Hello" "world"]))
|
||||
(let-do [j 0
|
||||
lstrings (Array.count strings)
|
||||
sep-count (- lstrings 1)
|
||||
result (String.allocate (+ sep-count (sum-count strings)) \ )]
|
||||
lstrings (Array.length strings)
|
||||
sep-length (- lstrings 1)
|
||||
result (String.allocate (+ sep-length (sum-length strings)) \ )]
|
||||
(for [i 0 lstrings]
|
||||
(let-do [str (Array.nth strings i)
|
||||
len (String.count str)]
|
||||
len (String.length str)]
|
||||
(when (> i 0)
|
||||
(do
|
||||
(string-set! &result j sep)
|
||||
@ -155,7 +155,7 @@
|
||||
)
|
||||
|
||||
(defdynamic string-join- [strs]
|
||||
(if (= (count strs) 0)
|
||||
(if (= (length strs) 0)
|
||||
'(String.copy "")
|
||||
(list 'StringCopy.append (car strs) (string-join- (cdr strs)))))
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
[@"bg-cyan" @"46"]
|
||||
[@"bg-white" @"47"]])
|
||||
|
||||
(def len-color-table (Array.count &color-table))
|
||||
(def len-color-table (Array.length &color-table))
|
||||
|
||||
(defn color-name-to-ansi [cname]
|
||||
(let [res @""]
|
||||
@ -103,7 +103,7 @@
|
||||
)
|
||||
|
||||
(defdynamic with-test-internal [name forms]
|
||||
(if (= (count forms) 1)
|
||||
(if (= (length forms) 1)
|
||||
(list (list 'set! name (list 'ref (car forms))))
|
||||
(cons (list 'set! name (list 'ref (car forms)))
|
||||
(with-test-internal name (cdr forms)))))
|
||||
|
@ -216,9 +216,9 @@
|
||||
(defn zip- [f a b]
|
||||
(let [total []]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(for [i 0 (Array.length a)]
|
||||
(set! total (Array.push-back @&total (f @(Array.nth a i) @(Array.nth b i)))))
|
||||
(VN.init (Array.count a) total))))
|
||||
(VN.init (Array.length a) total))))
|
||||
|
||||
(defn zip [f a b]
|
||||
(if (= @(VN.n a) @(VN.n b))
|
||||
|
@ -104,7 +104,7 @@ String StringCopy_append(String a, String b) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int String_count(String *s) {
|
||||
int String_length(String *s) {
|
||||
return strlen(*s);
|
||||
}
|
||||
|
||||
|
@ -127,7 +127,7 @@ you need to qualify it with the module name, like this: ```Float.cos```.
|
||||
|
||||
If there are several used modules that contain symbols with the same name, the type inferer will try to figure
|
||||
out which one of the symbols you really mean (based on the types in your code). If it can't, it will display an error.
|
||||
For example, both the module ```String``` and ```Array``` contain a function named 'count'. In the following code it's
|
||||
For example, both the module ```String``` and ```Array``` contain a function named 'length'. In the following code it's
|
||||
possible to see that it's the array version that is needed, and that one will be called:
|
||||
|
||||
```
|
||||
@ -135,7 +135,7 @@ possible to see that it's the array version that is needed, and that one will be
|
||||
(use Array)
|
||||
|
||||
(defn f []
|
||||
(count [1 2 3 4 5]))
|
||||
(length [1 2 3 4 5]))
|
||||
```
|
||||
|
||||
In the following example it's not possible to figure out which type is intended:
|
||||
@ -144,7 +144,7 @@ In the following example it's not possible to figure out which type is intended:
|
||||
(use Array)
|
||||
|
||||
(defn f [x]
|
||||
(count x))
|
||||
(length x))
|
||||
```
|
||||
|
||||
Specifying the type solves this error:
|
||||
@ -153,7 +153,7 @@ Specifying the type solves this error:
|
||||
(use Array)
|
||||
|
||||
(defn f [x]
|
||||
(String.count x))
|
||||
(String.length x))
|
||||
```
|
||||
|
||||
### Structs
|
||||
|
@ -58,7 +58,7 @@ A simple piece of code:
|
||||
|
||||
(defn say-hi [text]
|
||||
(while true
|
||||
(if (< (count &text) 10)
|
||||
(if (< (length &text) 10)
|
||||
(println "Too short!")
|
||||
(println &text))))
|
||||
```
|
||||
@ -69,7 +69,7 @@ void say_MINUS_hi(string text) {
|
||||
bool _5 = true;
|
||||
while (_5) {
|
||||
string* _14 = &text; // ref
|
||||
int _12 = String_count(_14);
|
||||
int _12 = String_length(_14);
|
||||
bool _10 = Int__LT_(_12, 10);
|
||||
if (_10) {
|
||||
string _19 = "Too short!";
|
||||
@ -89,7 +89,7 @@ If-statements are kind of tricky in regards to memory management:
|
||||
```clojure
|
||||
(defn say-what [text]
|
||||
(let [manage (copy &text)]
|
||||
(if (< (count &text) 10)
|
||||
(if (< (length &text) 10)
|
||||
(copy "Too short")
|
||||
manage)))
|
||||
```
|
||||
@ -105,7 +105,7 @@ string say_MINUS_what(string text) {
|
||||
string manage = _9;
|
||||
string _13;
|
||||
string* _19 = &text; // ref
|
||||
int _17 = String_count(_19);
|
||||
int _17 = String_length(_19);
|
||||
bool _15 = Int__LT_(_17, 10);
|
||||
if (_15) {
|
||||
string _24 = "Too short";
|
||||
|
@ -125,7 +125,7 @@
|
||||
(println (refstr after)))))
|
||||
|
||||
(defn get-last-string [xs] ;; Should be generic preferably...
|
||||
(let [i (dec (Array.count &xs))]
|
||||
(let [i (dec (Array.length &xs))]
|
||||
(String.copy (Array.nth &xs i))))
|
||||
|
||||
(defn print-last-string []
|
||||
@ -179,11 +179,11 @@
|
||||
))
|
||||
|
||||
;; This used to be a bug becuase of multisym:s getting the same type variables:
|
||||
(defn two-counts-in-same-func []
|
||||
(defn two-lengths-in-same-func []
|
||||
(let [a [[1]]
|
||||
b [1 2 3]
|
||||
c1 (count &a)
|
||||
c2 (count &b)]
|
||||
c1 (length &a)
|
||||
c2 (length &b)]
|
||||
(println* &(str (+ c1 c2)))))
|
||||
|
||||
(defn changing-target-of-ref []
|
||||
@ -226,6 +226,6 @@
|
||||
(convert-struct-with-array-member-to-string)
|
||||
(threading)
|
||||
(multiple-stringification-of-arrays)
|
||||
(two-counts-in-same-func)
|
||||
(two-lengths-in-same-func)
|
||||
(changing-target-of-ref)
|
||||
))
|
||||
|
@ -65,14 +65,14 @@
|
||||
|
||||
(defn energy [bodies]
|
||||
(let-do [e 0.0]
|
||||
(for [i 0 (count bodies)]
|
||||
(for [i 0 (length bodies)]
|
||||
(let-do [b (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) (count bodies)]
|
||||
(for [j (+ i 1) (length bodies)]
|
||||
(let [b2 (nth bodies j)
|
||||
dx (- (Planet.x b) (Planet.x b2))
|
||||
dy (- (Planet.y b) (Planet.y b2))
|
||||
@ -91,9 +91,9 @@
|
||||
|
||||
(defn advance [bodies dt]
|
||||
(do
|
||||
(for [i 0 (count bodies)]
|
||||
(for [i 0 (length bodies)]
|
||||
(let [b (nth bodies i)]
|
||||
(for [j (+ i 1) (count bodies)]
|
||||
(for [j (+ i 1) (length bodies)]
|
||||
(let [b2 (nth bodies j)
|
||||
dx (- (Planet.x b) (Planet.x b2))
|
||||
dy (- (Planet.y b) (Planet.y b2))
|
||||
@ -109,7 +109,7 @@
|
||||
(Planet.set-vy! b2 (+ (Planet.vy b2) (* dy (* (Planet.mass b) mag))))
|
||||
(Planet.set-vz! b2 (+ (Planet.vz b2) (* dz (* (Planet.mass b) mag)))))))))
|
||||
|
||||
(for [i 0 (count bodies)]
|
||||
(for [i 0 (length bodies)]
|
||||
(update-planet i bodies dt))))
|
||||
|
||||
; defining bodies outside main (`def`) results in c compile error:
|
||||
|
@ -41,7 +41,7 @@
|
||||
(SDL.rect (* x 2) 80 16 16)
|
||||
(SDL.rect (* x 4) 112 16 16)
|
||||
(SDL.rect (* x 8) 144 16 16)]
|
||||
n (count &rects)]
|
||||
n (length &rects)]
|
||||
(SDL.render-fill-rects rend (raw rects) n))
|
||||
(SDL.set-render-draw-color rend 255 50 100 255)
|
||||
(for [x 0 512 16]
|
||||
@ -50,7 +50,7 @@
|
||||
(SDL.render-draw-line rend 512 (+ 256 (/ x 2)) 0 512)))
|
||||
(SDL.set-render-draw-color rend 0 0 0 255)
|
||||
(let [lines (random-lines)
|
||||
n (count &lines)]
|
||||
n (length &lines)]
|
||||
(SDL.render-draw-lines rend (raw lines) n))
|
||||
(let [img @(Images.img1 &images)]
|
||||
(SDL.render-copy-ex rend
|
||||
|
@ -15,7 +15,7 @@
|
||||
(defn lookup [bucket lookup-key]
|
||||
(let-do [pairs (Bucket.entries bucket)
|
||||
result (zero)]
|
||||
(for [i 0 (count pairs)]
|
||||
(for [i 0 (length pairs)]
|
||||
(let [pair (nth pairs i)]
|
||||
(when (= (Entry.key pair) &lookup-key)
|
||||
(set! result @(Entry.value pair)))))
|
||||
|
@ -84,7 +84,7 @@
|
||||
g h i]))))
|
||||
|
||||
(defn tick [world newWorld]
|
||||
(for [i 0 (count world)]
|
||||
(for [i 0 (length world)]
|
||||
(let [x (mod i height)
|
||||
y (/ i width)
|
||||
total (neighbours world x y)
|
||||
|
@ -29,17 +29,17 @@
|
||||
(def grid-size 32)
|
||||
|
||||
(defn draw-snake [rend snake]
|
||||
(let [body-count (count (body snake))]
|
||||
(for [i 0 body-count]
|
||||
(let [body-length (length (body snake))]
|
||||
(for [i 0 body-length]
|
||||
(let [part (nth (body snake) i)
|
||||
x (* grid-size (to-int @(Vector2.V2.x part)))
|
||||
y (* grid-size (to-int @(Vector2.V2.y part)))]
|
||||
(if (= i 0)
|
||||
(do
|
||||
(SDL.set-render-draw-color rend 200 255 (+ 100 (* body-count 10)) 255)
|
||||
(SDL.set-render-draw-color rend 200 255 (+ 100 (* body-length 10)) 255)
|
||||
(SDL.render-fill-rect rend (address (SDL.rect x y grid-size grid-size))))
|
||||
(do
|
||||
(SDL.set-render-draw-color rend 200 (+ 50 (* i (/ 200 body-count))) 100 255)
|
||||
(SDL.set-render-draw-color rend 200 (+ 50 (* i (/ 200 body-length))) 100 255)
|
||||
(SDL.render-fill-rect rend (address (SDL.rect x y grid-size grid-size)))))))))
|
||||
|
||||
(defn draw-human [rend human]
|
||||
@ -89,7 +89,7 @@
|
||||
|
||||
(defn shift-body [body]
|
||||
(do
|
||||
(let [i (- (Array.count body) 2)]
|
||||
(let [i (- (Array.length body) 2)]
|
||||
(while (> i -1)
|
||||
(do
|
||||
(aset! body (inc i) @(nth body i))
|
||||
|
@ -6,6 +6,6 @@
|
||||
(defn main []
|
||||
(with-test test
|
||||
(assert-equal test 1 2 "1 == 2")
|
||||
(assert-true test (Int.> (Array.count &[1 2 3]) 1) "len([1 2 3]) == 1")
|
||||
(assert-true test (Int.> (Array.length &[1 2 3]) 1) "len([1 2 3]) == 1")
|
||||
(assert-not-equal test "hi" "bye" "hi != bye")
|
||||
(print-test-results test)))
|
||||
|
@ -239,9 +239,9 @@ templateAsetUninitializedBang = defineTypeParameterizedTemplate templateCreator
|
||||
,"}"]))
|
||||
(const [])
|
||||
|
||||
templateCount :: (String, Binder)
|
||||
templateCount = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = (SymPath ["Array"] "count")
|
||||
templateLength :: (String, Binder)
|
||||
templateLength = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = (SymPath ["Array"] "length")
|
||||
t = (FuncTy [RefTy (StructTy "Array" [VarTy "t"])] IntTy)
|
||||
templateCreator = TemplateCreator $
|
||||
\typeEnv env ->
|
||||
|
@ -438,12 +438,12 @@ commandIsSymbol [x] =
|
||||
XObj (Sym _ _) _ _ -> return (Right trueXObj)
|
||||
_ -> return (Right falseXObj)
|
||||
|
||||
commandCount :: CommandCallback
|
||||
commandCount [x] =
|
||||
commandLength :: CommandCallback
|
||||
commandLength [x] =
|
||||
case x of
|
||||
XObj (Lst lst) _ _ -> return (Right (XObj (Num IntTy (fromIntegral (length lst))) Nothing Nothing))
|
||||
XObj (Arr arr) _ _ -> return (Right (XObj (Num IntTy (fromIntegral (length arr))) Nothing Nothing))
|
||||
_ -> return (Left (EvalError ("Applying 'count' to non-list: " ++ pretty x ++ " at " ++ prettyInfoFromXObj x)))
|
||||
_ -> return (Left (EvalError ("Applying 'length' to non-list: " ++ pretty x ++ " at " ++ prettyInfoFromXObj x)))
|
||||
|
||||
commandCar :: CommandCallback
|
||||
commandCar [x] =
|
||||
@ -597,13 +597,13 @@ commandSubstring [a, b, c] =
|
||||
_ ->
|
||||
Left (EvalError ("Can't call substring with " ++ pretty a ++ ", " ++ pretty b ++ " and " ++ pretty c))
|
||||
|
||||
commandStringCount :: CommandCallback
|
||||
commandStringCount [a] =
|
||||
commandStringLength :: CommandCallback
|
||||
commandStringLength [a] =
|
||||
return $ case a of
|
||||
XObj (Str s) _ _ ->
|
||||
Right (XObj (Num IntTy (fromIntegral (length s))) (Just dummyInfo) (Just IntTy))
|
||||
_ ->
|
||||
Left (EvalError ("Can't call count with " ++ pretty a))
|
||||
Left (EvalError ("Can't call length with " ++ pretty a))
|
||||
|
||||
commandStringJoin :: CommandCallback
|
||||
commandStringJoin [a] =
|
||||
|
@ -45,7 +45,7 @@ arrayModule = Env { envBindings = bindings, envParent = Nothing, envModuleName =
|
||||
, templateAset
|
||||
, templateAsetBang
|
||||
, templateAsetUninitializedBang
|
||||
, templateCount
|
||||
, templateLength
|
||||
, templatePushBack
|
||||
, templatePopBack
|
||||
, templateDeleteArray
|
||||
@ -156,7 +156,7 @@ dynamicModule = Env { envBindings = bindings, envParent = Nothing, envModuleName
|
||||
[ addCommand "list?" 1 commandIsList
|
||||
, addCommand "array?" 1 commandIsArray
|
||||
, addCommand "symbol?" 1 commandIsSymbol
|
||||
, addCommand "count" 1 commandCount
|
||||
, addCommand "length" 1 commandLength
|
||||
, addCommand "car" 1 commandCar
|
||||
, addCommand "cdr" 1 commandCdr
|
||||
, addCommand "last" 1 commandLast
|
||||
@ -203,7 +203,7 @@ dynamicStringModule = Env { envBindings = bindings, envParent = Nothing, envModu
|
||||
where bindings = Map.fromList [ addCommand "char-at" 2 commandCharAt
|
||||
, addCommand "index-of" 2 commandIndexOf
|
||||
, addCommand "substring" 3 commandSubstring
|
||||
, addCommand "count" 1 commandStringCount
|
||||
, addCommand "length" 1 commandStringLength
|
||||
, addCommand "join" 1 commandStringJoin
|
||||
, addCommand "directory" 1 commandStringDirectory
|
||||
]
|
||||
|
@ -41,11 +41,11 @@
|
||||
x)))
|
||||
|
||||
(defn all-eq [a b]
|
||||
(if (/= (Array.count a) (Array.count b))
|
||||
(if (/= (Array.length a) (Array.length b))
|
||||
false
|
||||
(let [eq true]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(for [i 0 (Array.length a)]
|
||||
(if (/= @(Array.nth a i) @(Array.nth b i))
|
||||
(set! eq false)
|
||||
()))
|
||||
|
@ -279,11 +279,11 @@
|
||||
;; TODO! Failing to compile this...
|
||||
;; (defmodule ArrayCompareExtension
|
||||
;; (defn < [a b]
|
||||
;; (Int.< (Array.count a)
|
||||
;; (Array.count b)))
|
||||
;; (Int.< (Array.length a)
|
||||
;; (Array.length b)))
|
||||
;; (defn > [a b]
|
||||
;; (Int.> (Array.count a)
|
||||
;; (Array.count b))))
|
||||
;; (Int.> (Array.length a)
|
||||
;; (Array.length b))))
|
||||
;; (defn array-sort-1 []
|
||||
;; (let [xs [[0 0] [0 0 0] [0 0 0 0] [0]]
|
||||
;; ys (Array.sort xs)]
|
||||
@ -306,7 +306,7 @@
|
||||
|
||||
(defn array-copy-map-1 []
|
||||
(let [xs [@"a" @"bb" @"ccc"]
|
||||
ys (Array.copy-map String.count &xs)]
|
||||
ys (Array.copy-map String.length &xs)]
|
||||
(assert (= &[1 2 3] &ys))))
|
||||
|
||||
(defn str-ref [r]
|
||||
|
@ -4,22 +4,22 @@
|
||||
(use-all Double Test Statistics)
|
||||
|
||||
(defn all-eq [a b]
|
||||
(if (Int./= (Array.count a) (Array.count b))
|
||||
(if (Int./= (Array.length a) (Array.length b))
|
||||
false
|
||||
(let [res true]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(for [i 0 (Array.length a)]
|
||||
(if (not (Double.= @(Array.nth a i) @(Array.nth b i)))
|
||||
(set! res false)
|
||||
()))
|
||||
res))))
|
||||
|
||||
(defn all-approx [a b]
|
||||
(if (Int./= (Array.count a) (Array.count b))
|
||||
(if (Int./= (Array.length a) (Array.length b))
|
||||
false
|
||||
(let [res true]
|
||||
(do
|
||||
(for [i 0 (Array.count a)]
|
||||
(for [i 0 (Array.length a)]
|
||||
(if (not (Double.approx @(Array.nth a i) @(Array.nth b i)))
|
||||
(set! res false)
|
||||
()))
|
||||
|
@ -290,7 +290,7 @@
|
||||
)
|
||||
(assert-equal test
|
||||
5
|
||||
(count &(allocate 5 \a))
|
||||
(length &(allocate 5 \a))
|
||||
"allocate works correctly III"
|
||||
)
|
||||
(assert-equal test
|
||||
|
Loading…
Reference in New Issue
Block a user