Merge branch 'master' into lifetimez-with-lambdas

This commit is contained in:
Erik Svedäng 2019-11-25 12:22:04 +01:00
commit 576dad0984
62 changed files with 1326 additions and 977 deletions

19
.clang-format Normal file
View File

@ -0,0 +1,19 @@
---
BasedOnStyle: Google
AlignEscapedNewlinesLeft: 'true'
AlignTrailingComments: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'true'
AllowShortLoopsOnASingleLine: 'true'
BinPackParameters: 'true'
ColumnLimit: '80'
PointerAlignment: Left
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpacesInContainerLiterals: 'true'
Standard: Cpp11
TabWidth: '4'
IndentWidth: '4'
UseTab: Never

View File

@ -25,7 +25,7 @@ defaultProject =
, projectEchoC = False
, projectLibDir = "libs"
, projectCarpDir = "."
, projectOutDir = "out"
, projectOutDir = "out/"
, projectDocsDir = "docs"
, projectDocsLogo = ""
, projectDocsPrelude = ""

View File

@ -7,3 +7,7 @@ then
cabal build
fi
fi
if which clang-format > /dev/null
then
clang-format -i core/*.h
fi

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,19 +288,27 @@ 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))
(doc enumerated "creates a new array of `Pair`s where the first position is the index and the second position is the element from the original array `xs`.")
(defn enumerated [xs]
(zip &Pair.init-from-refs
&(range 0 (length xs) 1) ;; Inefficient, creates a temporary array.
&(range 0 (length xs) 1) ; Inefficient, creates a temporary array.
xs))
(doc nth "gets a reference to the `n`th element from an array `arr` wrapped on a `Maybe`.
If the `index` is out of bounds, return `Maybe.Nothing`")
(defn nth [xs index]
(if (and (>= index 0) (< index (length xs)))
(Maybe.Just @(unsafe-nth xs index)) ; the copy will go away with lifetimes
(Maybe.Nothing)))
(doc remove "removes all occurrences of the element `el` in the array `arr`, in place.")
(defn remove [el arr]
(endo-filter &(fn [x] (not (= el x)))
@ -312,7 +320,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 +332,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

@ -10,18 +10,19 @@
(doc meaning "converts a numerical char into the appropriate number (e.g. from `\1` to `1`).")
(defn meaning [char-ref]
(cond
(= @char-ref \0) 0
(= @char-ref \1) 1
(= @char-ref \2) 2
(= @char-ref \3) 3
(= @char-ref \4) 4
(= @char-ref \5) 5
(= @char-ref \6) 6
(= @char-ref \7) 7
(= @char-ref \8) 8
(= @char-ref \9) 9
-1))
(let [c @char-ref]
(case c
\0 0
\1 1
\2 2
\3 3
\4 4
\5 5
\6 6
\7 7
\8 8
\9 9
-1)))
(doc lower-case? "tests whether a character is lower case.")
(defn lower-case? [c]

View File

@ -12,6 +12,8 @@
(system-include "carp_stdbool.h")
(system-include "core.h")
(system-include "carp_memory.h")
(system-include "sys/wait.h")
(system-include "unistd.h")
(load "Interfaces.carp")
(load "Bool.carp")

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))
@ -33,7 +33,7 @@
(defn push-down-until! [heap i len ord]
(while true
(let [challenger (max-of-three-until! heap i len @ord)]
(let [challenger (max-of-three-until! heap i len ord)]
(if (= challenger i)
(break)
(do
@ -45,10 +45,10 @@
(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)]
(if (not (ord elem parent-elem))
parent-elem (Array.unsafe-nth heap parent-i)]
(if (not (~ord elem parent-elem))
(break)
(do (Array.swap! heap i parent-i)
(set! i parent-i))))))
@ -61,7 +61,7 @@
(defn heapify! [arr ord]
(let [len (Array.length arr)]
(for [i 1 len]
(push-up! arr i @ord))))
(push-up! arr i ord))))
(doc push! "Insert a new item onto the heap.")
(defn push! [heap item ord]
@ -92,7 +92,7 @@
(Heap.push-down-until! heap i len &ord))
(defn push-up! [heap i]
(Heap.push-up! heap i ord))
(Heap.push-up! heap i &ord))
(doc peek "Returns minimum item on min-heap.")
(defn peek [heap]
@ -104,7 +104,7 @@
(doc push! "Insert a new element onto the min-heap.")
(defn push! [heap item]
(Heap.push! heap item ord))
(Heap.push! heap item &ord))
(doc pop! "Remove and return the first item in the min-heap.")
(defn pop! [heap]
@ -123,7 +123,7 @@
(Heap.push-down-until! heap i len &ord))
(defn push-up! [heap i]
(Heap.push-up! heap i ord))
(Heap.push-up! heap i &ord))
(doc peek "Returns maximum first item on max-heap.")
(defn peek [heap]
@ -135,7 +135,7 @@
(doc push! "Insert a new element onto the max-heap.")
(defn push! [heap item]
(Heap.push! heap item ord))
(Heap.push! heap item &ord))
(doc pop! "Remove and return the first item in the max-heap.")
(defn pop! [heap]

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,21 +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)
b2 (Bucket.put @n k v)]
(Array.aset b idx b2))))))
(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]
@ -151,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
@ -169,8 +168,8 @@
(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)
i (Bucket.find n k)] ;; Change type signature for Bucket.find to take a ref instead!
(let [n (Array.unsafe-nth &b idx)
i (Bucket.find n k)]
(if (<= 0 i)
(let [new-b (Bucket.set-idx @n i &(~f (Bucket.get-idx n i)))]
(Array.aset b idx new-b))
@ -181,7 +180,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.")
@ -191,25 +190,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)
new-b (Bucket.shrink @n k)]
(Array.aset b idx new-b))))))
(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))
@ -222,22 +220,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))
@ -246,11 +244,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))
@ -270,7 +268,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))))
@ -308,7 +306,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))))
@ -317,7 +315,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))
@ -348,7 +346,7 @@
;; The lifetime system really doesn't like this function, had to put in a bunch of copying to make it compile:
]
(update-buckets s &(fn [b]
(let [n (Array.nth &b idx)]
(let [n (Array.unsafe-nth &b idx)]
(if (SetBucket.contains? n k)
b
(let [new-k @k
@ -359,7 +357,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)))))
@ -367,7 +365,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.")
@ -377,15 +375,14 @@
(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)
new-b (SetBucket.shrink n k)]
(Array.aset b idx new-b))))))
(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?")
(defn all? [pred set]
@ -409,18 +406,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))
@ -428,11 +425,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

@ -52,9 +52,9 @@ It is the inverse of [`just?`](#just?).")
(Nothing) false
(Just y) (= x y))))
(doc ptr "Creates a `(Ptr a)` from a `(Maybe a)`. If the `Maybe` was
(doc unsafe-ptr "Creates a `(Ptr a)` from a `(Maybe a)`. If the `Maybe` was
`Nothing`, this function will return a `NULL` value.")
(defn ptr [a]
(defn unsafe-ptr [a]
(the (Ptr a) (Unsafe.coerce
(match @a
(Nothing) NULL

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

@ -25,7 +25,7 @@ SDL_Event SDL_Event_copy(SDL_Event *e) {
// Keycode
SDL_Keycode SDL_Keycode_copy(SDL_Keycode* a) {
SDL_Keycode SDL_Keycode_copy(SDL_Keycode *a) {
return *a;
}
@ -86,10 +86,10 @@ int SDL_Color_a(SDL_Color *col) {
return (int)(col->a);
}
void* SDL_SurfacePixels(SDL_Surface* s) {
return s->pixels;
void *SDL_SurfacePixels(SDL_Surface *s) {
return s->pixels;
}
int SDL_SurfacePitch(SDL_Surface* s) {
return s->pitch;
int SDL_SurfacePitch(SDL_Surface *s) {
return s->pitch;
}

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

@ -29,10 +29,11 @@
(doc repeat "Returns a new string which is `inpt` repeated `n` times.")
(defn repeat [n inpt]
(let [str @""]
(let [l (length inpt)
str (String.allocate (* n l) \0)]
(do
(for [i 0 n]
(set! str (append &str inpt)))
(string-set-at! &str (* i l) inpt))
str)))
(doc pad-left "Pads the left of a string with len bytes using the padding pad.")
@ -88,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`.")
@ -101,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))))
@ -116,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
@ -134,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

@ -91,11 +91,11 @@
(if (Int.> (Int.+ passed failed) 0)
(do
(IO.color (Green))
(if (Int.> passed 0) (IO.print &(String.append "\t|" &(String.repeat passed "="))) ())
(if (Int.= failed 0) (IO.print "|") ())
(when (Int.> passed 0) (IO.print &(String.append "\t|" &(String.allocate passed \=))))
(when (Int.= failed 0) (IO.print "|"))
(IO.color (Red))
(if (Int.= passed 0) (IO.print "\t|") ())
(if (Int.> failed 0) (IO.print &(String.append &(String.repeat failed "=") "|")) ())
(when (Int.= passed 0) (IO.print "\t|"))
(when (Int.> failed 0) (IO.print &(String.append &(String.allocate failed \=) "|")))
(IO.println ""))
())
(IO.color (Green))

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

@ -1,5 +1,5 @@
double get_MINUS_time_MINUS_elapsed() {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
}

View File

@ -1,20 +1,20 @@
// Bool
bool Bool_copy(const bool* b) {
return *b;
return *b;
}
bool Bool__EQ_(bool a, bool b) {
return a == b;
return a == b;
}
bool Bool_not(bool a) {
return !a;
return !a;
}
bool Bool_and(bool a, bool b) {
return a && b;
return a && b;
}
bool Bool_or(bool a, bool b) {
return a && b;
return a && b;
}

View File

@ -1,29 +1,29 @@
bool Char__EQ_(char a, char b) {
return a == b;
return a == b;
}
bool Char__LT_(char a, char b) {
return a < b;
return a < b;
}
bool Char__GT_(char a, char b) {
return a > b;
return a > b;
}
int Char_to_MINUS_int(char c) {
return (int)(unsigned char)c;
return (int)(unsigned char)c;
}
char Char_from_MINUS_int(int i) {
return (char)i;
return (char)i;
}
char Char_copy(const char *c) {
return *c;
return *c;
}
String PtrChar_str(const char *c) {
size_t len = strlen(c) + 1;
String ptr = CARP_MALLOC(len);
return (String) memcpy(ptr, c, len);
return (String)memcpy(ptr, c, len);
}

View File

@ -1,15 +1,33 @@
const double CARP_DBL_MAX = DBL_MAX;
double Double__PLUS_(double x, double y) { return x + y; }
double Double__MINUS_(double x, double y) { return x - y; }
double Double__MUL_(double x, double y) { return x * y; }
double Double__DIV_(double x, double y) { return x / y; }
bool Double__LT_(double x, double y) { return x < y; }
bool Double__GT_(double x, double y) { return x > y; }
bool Double__EQ_(double x, double y) { return x == y; }
double Double_neg(double x) { return -x; }
double Double__PLUS_(double x, double y) {
return x + y;
}
double Double__MINUS_(double x, double y) {
return x - y;
}
double Double__MUL_(double x, double y) {
return x * y;
}
double Double__DIV_(double x, double y) {
return x / y;
}
bool Double__LT_(double x, double y) {
return x < y;
}
bool Double__GT_(double x, double y) {
return x > y;
}
bool Double__EQ_(double x, double y) {
return x == y;
}
double Double_neg(double x) {
return -x;
}
double Double_copy(const double *x) { return *x; }
double Double_copy(const double* x) {
return *x;
}
// Double.toInt : Double -> Int
int Double_to_MINUS_int(double x) {

View File

@ -1,15 +1,33 @@
const float CARP_FLT_MAX = FLT_MAX;
float Float__PLUS_(float x, float y) { return x + y; }
float Float__MINUS_(float x, float y) { return x - y; }
float Float__MUL_(float x, float y) { return x * y; }
float Float__DIV_(float x, float y) { return x / y; }
bool Float__LT_(float x, float y) { return x < y; }
bool Float__GT_(float x, float y) { return x > y; }
bool Float__EQ_(float x, float y) { return x == y; }
float Float_neg(float x) { return -x; }
float Float__PLUS_(float x, float y) {
return x + y;
}
float Float__MINUS_(float x, float y) {
return x - y;
}
float Float__MUL_(float x, float y) {
return x * y;
}
float Float__DIV_(float x, float y) {
return x / y;
}
bool Float__LT_(float x, float y) {
return x < y;
}
bool Float__GT_(float x, float y) {
return x > y;
}
bool Float__EQ_(float x, float y) {
return x == y;
}
float Float_neg(float x) {
return -x;
}
float Float_copy(const float *x) { return *x; }
float Float_copy(const float* x) {
return *x;
}
int Float_to_MINUS_int(float x) {
return (int)x;
@ -89,7 +107,7 @@ float Float_log10(float x) {
return log10f(x);
}
float Float_modf(float x, float * integer) {
float Float_modf(float x, float* integer) {
return modff(x, integer);
}

View File

@ -1,26 +1,62 @@
int CARP_INT_MAX = INT_MAX;
int CARP_INT_MIN = INT_MIN;
int Int__PLUS_(int x, int y) { return x + y; }
int Int__MINUS_(int x, int y) { return x - y; }
int Int__MUL_(int x, int y) { return x * y; }
int Int__DIV_(int x, int y) { return x / y; }
bool Int__EQ_(int x, int y) { return x == y; }
bool Int__LT_(int x, int y) { return x < y; }
bool Int__GT_(int x, int y) { return x > y; }
int Int_neg(int x) { return -x; }
int Int__PLUS_(int x, int y) {
return x + y;
}
int Int__MINUS_(int x, int y) {
return x - y;
}
int Int__MUL_(int x, int y) {
return x * y;
}
int Int__DIV_(int x, int y) {
return x / y;
}
bool Int__EQ_(int x, int y) {
return x == y;
}
bool Int__LT_(int x, int y) {
return x < y;
}
bool Int__GT_(int x, int y) {
return x > y;
}
int Int_neg(int x) {
return -x;
}
int Int_inc(int x) { return x + 1; }
int Int_dec(int x) { return x - 1; }
int Int_abs(int x) { return x > 0 ? x : -x; }
int Int_bit_MINUS_shift_MINUS_left(int x, int y) { return x << y; }
int Int_bit_MINUS_shift_MINUS_right(int x, int y) { return x >> y; }
int Int_bit_MINUS_and(int x, int y) { return x & y; }
int Int_bit_MINUS_or(int x, int y) { return x | y; }
int Int_bit_MINUS_xor(int x, int y) { return x ^ y; }
int Int_bit_MINUS_not(int x) { return ~x; }
int Int_inc(int x) {
return x + 1;
}
int Int_dec(int x) {
return x - 1;
}
int Int_abs(int x) {
return x > 0 ? x : -x;
}
int Int_bit_MINUS_shift_MINUS_left(int x, int y) {
return x << y;
}
int Int_bit_MINUS_shift_MINUS_right(int x, int y) {
return x >> y;
}
int Int_bit_MINUS_and(int x, int y) {
return x & y;
}
int Int_bit_MINUS_or(int x, int y) {
return x | y;
}
int Int_bit_MINUS_xor(int x, int y) {
return x ^ y;
}
int Int_bit_MINUS_not(int x) {
return ~x;
}
int Int_copy(const int *x) { return *x; }
int Int_copy(const int *x) {
return *x;
}
int Int_mod(int x, int divider) {
return x % divider;

View File

@ -1,10 +1,18 @@
void IO_println(String *s) { puts(*s); }
void IO_print(String *s) { printf("%s", *s); }
void IO_println(String *s) {
puts(*s);
}
void IO_print(String *s) {
printf("%s", *s);
}
void IO_errorln(String *s) { fprintf(stderr, "%s\n", *s); }
void IO_error(String *s) { fprintf(stderr, "%s", *s); }
void IO_errorln(String *s) {
fprintf(stderr, "%s\n", *s);
}
void IO_error(String *s) {
fprintf(stderr, "%s", *s);
}
char IO_EOF = (char) EOF;
char IO_EOF = (char)EOF;
#ifdef _WIN32
// getline isn't a C standard library function so it's missing on windows
@ -32,7 +40,7 @@ size_t getline(char **lineptr, size_t *n, FILE *stream) {
}
pos = 0;
while(c != EOF) {
while (c != EOF) {
if (pos + 1 >= *n) {
size_t new_size = *n + (*n >> 2);
if (new_size < 128) {
@ -46,7 +54,7 @@ size_t getline(char **lineptr, size_t *n, FILE *stream) {
*lineptr = new_ptr;
}
((unsigned char *)(*lineptr))[pos ++] = c;
((unsigned char *)(*lineptr))[pos++] = c;
if (c == '\n') {
break;
}
@ -70,30 +78,29 @@ String IO_read_MINUS_file(const String *filename) {
long length;
FILE *f = fopen(*filename, "rb");
if(f) {
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = CARP_MALLOC (length + 1);
if (buffer) {
fread (buffer, 1, length, f);
if (f) {
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = CARP_MALLOC(length + 1);
if (buffer) {
fread(buffer, 1, length, f);
buffer[length] = '\0';
} else {
printf("Failed to open buffer from file: %s\n", *filename);
buffer = String_empty();
}
fclose (f);
fclose(f);
} else {
printf("Failed to open file: %s\n", *filename);
buffer = String_empty();
}
return buffer;
}
char IO_fgetc(FILE *f) {
return (char) fgetc(f);
return (char)fgetc(f);
}
void IO_fclose(FILE *f) {

View File

@ -1,28 +1,70 @@
long Long__PLUS_(long x, long y) { return x + y; }
long Long__MINUS_(long x, long y) { return x - y; }
long Long__MUL_(long x, long y) { return x * y; }
long Long__DIV_(long x, long y) { return x / y; }
long Long__PLUS_(long x, long y) {
return x + y;
}
long Long__MINUS_(long x, long y) {
return x - y;
}
long Long__MUL_(long x, long y) {
return x * y;
}
long Long__DIV_(long x, long y) {
return x / y;
}
#ifndef _WIN32
bool Long_safe_MINUS_add(long x, long y, long* res) { return __builtin_saddl_overflow(x, y, res); }
bool Long_safe_MINUS_sub(long x, long y, long* res) { return __builtin_ssubl_overflow(x, y, res); }
bool Long_safe_MINUS_mul(long x, long y, long* res) { return __builtin_smull_overflow(x, y, res); }
bool Long_safe_MINUS_add(long x, long y, long* res) {
return __builtin_saddl_overflow(x, y, res);
}
bool Long_safe_MINUS_sub(long x, long y, long* res) {
return __builtin_ssubl_overflow(x, y, res);
}
bool Long_safe_MINUS_mul(long x, long y, long* res) {
return __builtin_smull_overflow(x, y, res);
}
#endif
bool Long__EQ_(long x, long y) { return x == y; }
bool Long__LT_(long x, long y) { return x < y; }
bool Long__GT_(long x, long y) { return x > y; }
long Long_neg(long x) { return -x; }
bool Long__EQ_(long x, long y) {
return x == y;
}
bool Long__LT_(long x, long y) {
return x < y;
}
bool Long__GT_(long x, long y) {
return x > y;
}
long Long_neg(long x) {
return -x;
}
long Long_inc(long x) { return x + 1; }
long Long_dec(long x) { return x - 1; }
long Long_abs(long x) { return x > 0 ? x : -x; }
long Long_bit_MINUS_shift_MINUS_left(long x, long y) { return x << y; }
long Long_bit_MINUS_shift_MINUS_right(long x, long y) { return x >> y; }
long Long_bit_MINUS_and(long x, long y) { return x & y; }
long Long_bit_MINUS_or(long x, long y) { return x | y; }
long Long_bit_MINUS_xor(long x, long y) { return x ^ y; }
long Long_bit_MINUS_not(long x) { return ~x; }
long Long_inc(long x) {
return x + 1;
}
long Long_dec(long x) {
return x - 1;
}
long Long_abs(long x) {
return x > 0 ? x : -x;
}
long Long_bit_MINUS_shift_MINUS_left(long x, long y) {
return x << y;
}
long Long_bit_MINUS_shift_MINUS_right(long x, long y) {
return x >> y;
}
long Long_bit_MINUS_and(long x, long y) {
return x & y;
}
long Long_bit_MINUS_or(long x, long y) {
return x | y;
}
long Long_bit_MINUS_xor(long x, long y) {
return x ^ y;
}
long Long_bit_MINUS_not(long x) {
return ~x;
}
long Long_copy(const long *x) { return *x; }
long Long_copy(const long* x) {
return *x;
}
long Long_mod(long x, long divider) {
return x % divider;
@ -37,9 +79,9 @@ bool Long_mask(long a, long b) {
}
int Long_to_MINUS_int(long a) {
return (int) a;
return (int)a;
}
long Long_from_MINUS_int(int a) {
return (long) a;
return (long)a;
}

View File

@ -5,7 +5,7 @@ bool log_memory_balance = false;
void *logged_malloc(size_t size) {
void *ptr = malloc(size);
if(log_memory_balance) {
if (log_memory_balance) {
printf("MALLOC: %p (%ld bytes)\n", ptr, size);
}
malloc_balance_counter++;
@ -13,13 +13,14 @@ void *logged_malloc(size_t size) {
}
void logged_free(void *ptr) {
if(log_memory_balance) {
if (log_memory_balance) {
printf("FREE: %p\n", ptr);
}
free(ptr);
malloc_balance_counter--;
/* if(malloc_balance_counter == 0) { */
/* printf("malloc is balanced! (this should be the last thing you see)\n"); */
/* printf("malloc is balanced! (this should be the last thing you
* see)\n"); */
/* } */
/* else if(malloc_balance_counter < 0) { */
/* printf("malloc is %ld, that is bad!\n", malloc_balance_counter); */
@ -46,14 +47,14 @@ void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
#ifdef CHECK_ALLOCATIONS
void* CARP_MALLOC(size_t size) {
void* res = malloc(size);
if (!res) abort();
return res;
void* res = malloc(size);
if (!res) abort();
return res;
}
void* CARP_REALLOC(void * ptr, size_t size) {
void* res = realloc(ptr, size);
if (!res) abort();
return res;
void* CARP_REALLOC(void* ptr, size_t size) {
void* res = realloc(ptr, size);
if (!res) abort();
return res;
}
#else
#define CARP_MALLOC(size) malloc(size)
@ -63,18 +64,24 @@ void* CARP_REALLOC(void * ptr, size_t size) {
#define CARP_FREE(ptr) free(ptr)
long Debug_memory_MINUS_balance() {
printf("Error - calling 'memory-balance' without compiling with LOG_MEMORY enabled (--log-memory).\n");
printf(
"Error - calling 'memory-balance' without compiling with LOG_MEMORY "
"enabled (--log-memory).\n");
exit(1);
return 0;
}
void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
printf("Error - calling 'reset-memory-balance!' without compiling with LOG_MEMORY enabled (--log-memory).\n");
printf(
"Error - calling 'reset-memory-balance!' without compiling with "
"LOG_MEMORY enabled (--log-memory).\n");
exit(1);
}
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
printf("Error - calling 'log-memory-balance!' without compiling with LOG_MEMORY enabled (--log-memory).\n");
printf(
"Error - calling 'log-memory-balance!' without compiling with "
"LOG_MEMORY enabled (--log-memory).\n");
exit(1);
}

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,9 @@
bool Int_safe_MINUS_add(int x, int y, int* res) { return __builtin_sadd_overflow(x, y, res); }
bool Int_safe_MINUS_sub(int x, int y, int* res) { return __builtin_ssub_overflow(x, y, res); }
bool Int_safe_MINUS_mul(int x, int y, int* res) { return __builtin_smul_overflow(x, y, res); }
bool Int_safe_MINUS_add(int x, int y, int* res) {
return __builtin_sadd_overflow(x, y, res);
}
bool Int_safe_MINUS_sub(int x, int y, int* res) {
return __builtin_ssub_overflow(x, y, res);
}
bool Int_safe_MINUS_mul(int x, int y, int* res) {
return __builtin_smul_overflow(x, y, res);
}

View File

@ -5,7 +5,7 @@ String String_allocate(int len, char byte) {
*
* String_alloc(10, "a") == "aaaaaaaaaa"
*/
String ptr = CARP_MALLOC(len+1);
String ptr = CARP_MALLOC(len + 1);
memset(ptr, byte, len);
ptr[len] = '\0';
return ptr;
@ -20,7 +20,8 @@ void String_string_MINUS_set_BANG_(String *s, int i, char ch) {
(*s)[i] = ch;
}
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, const String *src) {
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i,
const String *src) {
char *dest = (*into) + i;
int lsrc = strlen(*src);
/* given a string and indices
@ -52,14 +53,14 @@ void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, const String *s
*
* so this write is safe
*/
CHK_INDEX(i+lsrc, strlen(*into)+1);
CHK_INDEX(i + lsrc, strlen(*into) + 1);
memcpy(dest, *src, lsrc);
}
String String_copy(const String *s) {
size_t len = strlen(*s) + 1;
String ptr = CARP_MALLOC(len);
return (String) memcpy(ptr, *s, len);
return (String)memcpy(ptr, *s, len);
}
bool String__EQ_(const String *a, const String *b) {
@ -87,7 +88,7 @@ int String_length(const String *s) {
return strlen(*s);
}
char* String_cstr(const String *s) {
char *String_cstr(const String *s) {
return *s;
}
@ -102,12 +103,12 @@ String String_prn(const String *s) {
return buffer;
}
char String_char_MINUS_at(const String* s, int i) {
return (*s)[i];
char String_char_MINUS_at(const String *s, int i) {
return (*s)[i];
}
String String_format(const String *str, const String *s) {
int size = snprintf(NULL, 0, *str, *s)+1;
int size = snprintf(NULL, 0, *str, *s) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, *s);
return buffer;
@ -122,17 +123,17 @@ Array String_chars(const String *s) {
}
String String_from_MINUS_chars(const Array *a) {
String s = CARP_MALLOC(a->len+1);
String s = CARP_MALLOC(a->len + 1);
memcpy(s, a->data, a->len);
s[a->len] = '\0';
return s;
}
String String_tail(const String* s) {
String String_tail(const String *s) {
int len = strlen(*s);
String news = CARP_MALLOC(len);
memcpy(news, (*s)+1, len-1);
news[len-1] = '\0';
memcpy(news, (*s) + 1, len - 1);
news[len - 1] = '\0';
return news;
}
@ -145,15 +146,15 @@ String String_empty() {
String Bool_str(bool b) {
const String true_str = "true";
const String false_str = "false";
if(b) {
if (b) {
return String_copy(&true_str);
} else {
return String_copy(&false_str);
}
}
String Bool_format(const String* str, bool b) {
int size = snprintf(NULL, 0, *str, b)+1;
String Bool_format(const String *str, bool b) {
int size = snprintf(NULL, 0, *str, b) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
return buffer;
@ -171,50 +172,50 @@ String Char_prn(char c) {
return buffer;
}
String Char_format(const String* str, char b) {
int size = snprintf(NULL, 0, *str, b)+1;
String Char_format(const String *str, char b) {
int size = snprintf(NULL, 0, *str, b) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
return buffer;
}
String Double_str(double x) {
int size = snprintf(NULL, 0, "%g", x)+1;
int size = snprintf(NULL, 0, "%g", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%g", x);
return buffer;
}
String Double_format(const String* s, double x) {
int size = snprintf(NULL, 0, *s, x)+1;
String Double_format(const String *s, double x) {
int size = snprintf(NULL, 0, *s, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *s, x);
return buffer;
}
String Float_str(float x) {
int size = snprintf(NULL, 0, "%gf", x)+1;
int size = snprintf(NULL, 0, "%gf", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%gf", x);
return buffer;
}
String Float_format(const String* str, float x) {
int size = snprintf(NULL, 0, *str, x)+1;
String Float_format(const String *str, float x) {
int size = snprintf(NULL, 0, *str, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
String Int_str(int x) {
int size = snprintf(NULL, 0, "%d", x)+1;
int size = snprintf(NULL, 0, "%d", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%d", x);
return buffer;
}
String Int_format(const String* str, int x) {
int size = snprintf(NULL, 0, *str, x)+1;
String Int_format(const String *str, int x) {
int size = snprintf(NULL, 0, *str, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
@ -225,14 +226,14 @@ int Int_from_MINUS_string(const String *s) {
}
String Long_str(long x) {
int size = snprintf(NULL, 0, "%ldl", x)+1;
int size = snprintf(NULL, 0, "%ldl", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%ldl", x);
return buffer;
}
String Long_format(const String* str, long x) {
int size = snprintf(NULL, 0, *str, x)+1;
String Long_format(const String *str, long x) {
int size = snprintf(NULL, 0, *str, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
@ -246,12 +247,12 @@ int String_index_MINUS_of_MINUS_from(const String *s, char c, int i) {
/* Return index of first occurrence of `c` in `s` AFTER index i
* Returns -1 if not found
*/
++i; // skip first character as we want AFTER i
++i; // skip first character as we want AFTER i
int len = strlen(*s);
for (; i<len; ++i) {
if (c == (*s)[i]) {
return i;
}
for (; i < len; ++i) {
if (c == (*s)[i]) {
return i;
}
}
return -1;
}

View File

@ -29,9 +29,9 @@ void System_sleep_MINUS_micros(int t) {
}
double System_nanotime() {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
}
#endif
@ -41,9 +41,9 @@ void System_system(const String *command) {
Array System_args;
String* System_get_MINUS_arg(int idx) {
String *System_get_MINUS_arg(int idx) {
assert(idx < System_args.len);
return &(((String*)System_args.data)[idx]);
return &(((String *)System_args.data)[idx]);
}
int System_get_MINUS_args_MINUS_len() {

View File

@ -1,26 +1,27 @@
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#if defined(WIN32) || defined(_WIN32) || \
defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#endif
typedef char* String;
typedef char* Pattern;
typedef char *String;
typedef char *Pattern;
#if defined NDEBUG
#define CHK_INDEX(i,n)
#define CHK_INDEX(i, n)
#else
#define CHK_INDEX(i,n) \
do { \
size_t __si = (size_t)i; \
size_t __ni = (size_t)n; \
if (!(__si < __ni)) { \
printf(__FILE__ ":%u: bad index: %zd < %zd\n", \
__LINE__, (ssize_t)i, (ssize_t)n); \
abort(); \
} \
}while (0)
#define CHK_INDEX(i, n) \
do { \
size_t __si = (size_t)i; \
size_t __ni = (size_t)n; \
if (!(__si < __ni)) { \
printf(__FILE__ ":%u: bad index: %zd < %zd\n", __LINE__, \
(ssize_t)i, (ssize_t)n); \
abort(); \
} \
} while (0)
#endif
// Array
@ -38,4 +39,4 @@ typedef struct {
void *copy;
} Lambda;
typedef void* LambdaEnv;
typedef void *LambdaEnv;

View File

@ -25,7 +25,7 @@ To learn more about the details of memory management, check out [Memory.md](http
10.0 ;; Double
true ;; Bool
"hello" ;; &String
\#"hello" ;; &Pattern
#"hello" ;; &Pattern
\e ;; Char
[1 2 3] ;; (Array Int)
{1 1.0 2 2.0} ;; (Map Int Double)
@ -61,6 +61,7 @@ foo ; symbol
(definterface interface-name (Fn [<t1> <t2>] <return>)) ;; Define a generic function that can have multiple implementations
(def variable-name value) ;; Define a global variable (only handles primitive constants for the moment)
(defmacro <name> [<arg1> <arg2> ...] <macro-body>) ;; Define a macro, its argument will not be evaluated when called
(defdynamic <name> <value>) ;; A variable that can only be used at the REPL or during compilation
(defndynamic <name> [<arg1> <arg2> ...] <function-body>) ;; A function that can only be used at the REPL or during compilation
(defmodule <name> <definition1> <definition2> ...) ;; The main way to organize your program into smaller parts
```
@ -75,6 +76,9 @@ and other static analysis. The first three of them are also available in dynamic
(do <expr1> <expr2> ... <return-expression>) ;; Perform side-effecting functions, then return a value
(if <expression> <true-branch> <false-branch>) ;; Branching
(while <expression> <body>) ;; Loop until expression is false
(use <module>) ;; Brings all symbols inside <module> into the scope
(with <module> <expr1> <expr2> ...) ;; Locally scoped `use` statement where all expressions after it will look up symbols in the <module>
(match <expression> <case1> <expr1> <case2> <expr2> ...) ;; Pattern matches <expression> against a set of sumtype constructors
(ref <expression>) ;; Borrow an owned value
(address <expression>) ;; Takes the memory address of a value, returns a C-style pointer
(set! <variable> <expression>) ;; Mutate a variable
@ -172,17 +176,17 @@ When you `use` a module, its declarations are brought into the current scope. If
```clojure
(use String)
;; Only the `String` module is used in the global scope,
;; Only the `String` module is used in the global scope,
;; so we can refer to `length` without a module qualifier.
(defn f [x]
(length x))
(defmodule Foo
(use Array)
;; Since the the `String` module is used in the global scope,
;; and the Foo module `use`s `Array`, we again need to qualify calls to `length`
;; Since the the `String` module is used in the global scope,
;; and the Foo module `use`s `Array`, we again need to qualify calls to `length`
;; to disambiguite which declaration we're referring to.
(defn g [xs]
(defn g [xs]
(Array.length xs)))
```
@ -190,15 +194,15 @@ Sometimes, it's more convenient to bring a module's declarations into scope only
```clojure
(defmodule Foo
;; we need to use a module qualifier here,
;; we need to use a module qualifier here,
;; since there's no call to `use` in the `Foo` module scope.
(defn f [x]
(defn f [x]
(String.length x))
;; Using the `with` form, we can reference the module's declarations
;; Using the `with` form, we can reference the module's declarations
;; unqualified in all the forms contained in the `with`'s scope.
(with String
(defn g [x]
(defn g [x]
(length x)))
)
```
@ -217,6 +221,55 @@ Sometimes, it's more convenient to bring a module's declarations into scope only
(Vector2.update-x my-pos inc) ;; => (Vector2 11 20)
```
### Sumtypes
There are two ways to define `sumtypes`:
**Enumeration:**
```clojure
(deftype MyEnum
Kind1
Kind2
Kind3)
```
**Data:**
```clojure
(deftype (Either a b)
(Left [a])
(Right [b]))
```
A Variant can be created with the same syntax as call expression
```clojure
(MyEnum.Kind1)
(Either.Left 10)
(Either.Right 11)
;; Or use `use` statement
(use Either)
(Left 10)
(Right 11)
(use MyEnum)
(Kind1)
(Kind2)
(Kind3)
```
You can use pattern matching to extract values in a safe way
```clojure
(defn get [either]
(match either
(Either.Left a) a
(Either.Right b) b))
(with MyEnum
;; You can give a generic "otherwise" statement as well
(match myenum
(Kind1) (logic1)
_ (logic-other)))
```
### C Interop
```clojure
(system-include "math.h") ;; compiles to #include <math.h>

View File

@ -54,23 +54,25 @@ Please note that for private repos only loading through SSH is supported. For pu
* [Dynamic ⦁](http://carp-lang.github.io/Carp/core/Dynamic.html) (only available in the repl and at compile time)
* [Maybe ⦁](http://carp-lang.github.io/Carp/core/Maybe.html)
* [Either ⦁](http://carp-lang.github.io/Carp/core/Either.html)
* [Result ⦁](http://carp-lang.github.io/Carp/core/Result.html)
### Numerics
* [Int ⦁](http://carp-lang.github.io/Carp/core/Int.html)
* SafeInt
<!-- * SafeInt -->
* [Long ⦁](http://carp-lang.github.io/Carp/core/Long.html)
* [Bool ⦁](http://carp-lang.github.io/Carp/core/Bool.html)
* [Float ⦁](http://carp-lang.github.io/Carp/core/Float.html)
* [Double ⦁](http://carp-lang.github.io/Carp/core/Double.html)
* [Vector](http://carp-lang.github.io/Carp/core/Vector.html)
* [Vector2](http://carp-lang.github.io/Carp/core/Vector2.html)
* [Vector3](http://carp-lang.github.io/Carp/core/Vector3.html)
* [VectorN](http://carp-lang.github.io/Carp/core/VectorN.html)
* [Geometry](http://carp-lang.github.io/Carp/core/Geometry.html)
* [Statistics](http://carp-lang.github.io/Carp/core/Statistics.html)
### Text
* [String ⦁](http://carp-lang.github.io/Carp/core/String.html)
* [Char ⦁](http://carp-lang.github.io/Carp/core/Char.html)
* Format ⦁
<!-- * Format ⦁ -->
* [Pattern ⦁](http://carp-lang.github.io/Carp/core/Pattern.html)
### Collections
@ -82,9 +84,9 @@ Please note that for private repos only loading through SSH is supported. For pu
* [System ⦁](http://carp-lang.github.io/Carp/core/System.html)
### Development
* [Debug ⦁](http://carp-lang.github.io/Carp/core/Debug.html)
* [Test](http://carp-lang.github.io/Carp/core/Test.html)
* [Bench](http://carp-lang.github.io/Carp/core/Bench.html)
* [Debug ⦁](http://carp-lang.github.io/Carp/core/Debug.html)
* [Test ⦁](http://carp-lang.github.io/Carp/core/Test.html)
### Graphics, sound and interaction
* [SDL](http://carp-lang.github.io/Carp/sdl/SDL_index.html)

View File

@ -27,6 +27,9 @@ For Flycheck:
To start an interactive session, make sure `carp` is in your path (inside Emacs) and execute `M-x run-carp`.
# Atom
[language-carp](https://atom.io/packages/language-carp) offers highlight with both TextMate and Tree-sitter, where tree-sitter grammar is more powerful.
## Other editors
Clojure syntax highlighting works very well with Carp since it uses the same symbols for most things.
If you have written an editor mode, please tell us and it will be added here!

View File

@ -151,7 +151,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), (Ref (Array a) b)] Bool)
(λ [(Ref (Array a) StaticLifetime), (Ref (Array a) StaticLifetime)] Bool)
</p>
<pre class="args">
(= a b)
@ -171,7 +171,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c)] Bool) d), (Ref (Array b) c)] Bool)
(λ [(Ref ((λ a) [(Ref b StaticLifetime)] Bool) c), (Ref (Array b) StaticLifetime)] Bool)
</p>
<pre class="args">
(all? f a)
@ -211,7 +211,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c)] Bool) d), (Ref (Array b) c)] Bool)
(λ [(Ref ((λ a) [(Ref b StaticLifetime)] Bool) c), (Ref (Array b) StaticLifetime)] Bool)
</p>
<pre class="args">
(any? f a)
@ -291,7 +291,7 @@
defn
</div>
<p class="sig">
(λ [(Array a), Int, (Ref ((λ b) [(Ref a c)] a) d)] (Array a))
(λ [(Array a), Int, (Ref ((λ b) [(Ref a StaticLifetime)] a) c)] (Array a))
</p>
<pre class="args">
(aupdate a i f)
@ -311,7 +311,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), Int, (Ref ((λ c) [(Ref a b)] a) d)] ())
(λ [(Ref (Array a) StaticLifetime), Int, (Ref ((λ b) [(Ref a StaticLifetime)] a) c)] ())
</p>
<pre class="args">
(aupdate! a i f)
@ -331,7 +331,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array (Array a)) b)] (Array a))
(λ [(Ref (Array (Array a)) StaticLifetime)] (Array a))
</p>
<pre class="args">
(concat xs)
@ -351,7 +351,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), (Ref a b)] Bool)
(λ [(Ref (Array a) StaticLifetime), (Ref a StaticLifetime)] Bool)
</p>
<pre class="args">
(contains? arr el)
@ -412,7 +412,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c)] d) e), (Ref (Array b) c)] (Array d))
(λ [(Ref ((λ a) [(Ref b StaticLifetime)] c) d), (Ref (Array b) StaticLifetime)] (Array c))
</p>
<pre class="args">
(copy-map f a)
@ -453,7 +453,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), (Ref a b)] Int)
(λ [(Ref (Array a) StaticLifetime), (Ref a StaticLifetime)] Int)
</p>
<pre class="args">
(element-count a e)
@ -533,7 +533,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] (Array (Pair Int a)))
(λ [(Ref (Array a) StaticLifetime)] (Array (Pair Int a)))
</p>
<pre class="args">
(enumerated xs)
@ -553,7 +553,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c)] Bool) d), (Ref (Array b) c)] (Maybe b))
(λ [(Ref ((λ a) [(Ref b StaticLifetime)] Bool) c), (Ref (Array b) StaticLifetime)] (Maybe b))
</p>
<pre class="args">
(find f a)
@ -574,7 +574,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c)] Bool) d), (Ref (Array b) c)] (Maybe Int))
(λ [(Ref ((λ a) [(Ref b StaticLifetime)] Bool) c), (Ref (Array b) StaticLifetime)] (Maybe Int))
</p>
<pre class="args">
(find-index f a)
@ -595,7 +595,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] (Maybe a))
(λ [(Ref (Array a) StaticLifetime)] (Maybe a))
</p>
<pre class="args">
(first a)
@ -616,7 +616,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), (Ref a b)] (Maybe Int))
(λ [(Ref (Array a) StaticLifetime), (Ref a StaticLifetime)] (Maybe Int))
</p>
<pre class="args">
(index-of a e)
@ -637,7 +637,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] (Maybe a))
(λ [(Ref (Array a) StaticLifetime)] (Maybe a))
</p>
<pre class="args">
(last a)
@ -678,7 +678,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] (Maybe a))
(λ [(Ref (Array a) StaticLifetime)] (Maybe a))
</p>
<pre class="args">
(maximum xs)
@ -699,7 +699,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] (Maybe a))
(λ [(Ref (Array a) StaticLifetime)] (Maybe a))
</p>
<pre class="args">
(minimum xs)
@ -717,16 +717,17 @@
</h3>
</a>
<div class="description">
template
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), Int] (Ref a b))
(λ [(Ref (Array a) StaticLifetime), Int] (Maybe a))
</p>
<span>
</span>
<pre class="args">
(nth xs index)
</pre>
<p class="doc">
<p>gets a reference to the <code>n</code>th element from an array <code>a</code>.</p>
<p>gets a reference to the <code>n</code>th element from an array <code>arr</code> wrapped on a <code>Maybe</code>.</p>
<p>If the <code>index</code> is out of bounds, return <code>Maybe.Nothing</code></p>
</p>
</div>
@ -780,7 +781,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), (Ref ((λ c) [(Ref a b)] Bool) d)] Int)
(λ [(Ref (Array a) StaticLifetime), (Ref ((λ b) [(Ref a StaticLifetime)] Bool) c)] Int)
</p>
<pre class="args">
(predicate-count a pred)
@ -800,7 +801,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), Int] (Array a))
(λ [(Ref (Array a) StaticLifetime), Int] (Array a))
</p>
<pre class="args">
(prefix-array xs end-index)
@ -919,7 +920,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [b, (Ref c d)] b) e), b, (Ref (Array c) d)] b)
(λ [(Ref ((λ a) [b, (Ref c StaticLifetime)] b) d), b, (Ref (Array c) StaticLifetime)] b)
</p>
<pre class="args">
(reduce f x xs)
@ -1085,7 +1086,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] ())
(λ [(Ref (Array a) StaticLifetime)] ())
</p>
<pre class="args">
(sort! arr)
@ -1105,7 +1106,7 @@
defn
</div>
<p class="sig">
(λ [(Array a), (Ref ((λ b) [(Ref a c), (Ref a c)] Bool) d)] (Array a))
(λ [(Array a), (Ref ((λ b) [(Ref a StaticLifetime), (Ref a StaticLifetime)] Bool) c)] (Array a))
</p>
<pre class="args">
(sort-by arr f)
@ -1125,7 +1126,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), (Ref ((λ c) [(Ref a b), (Ref a b)] Bool) d)] ())
(λ [(Ref (Array a) StaticLifetime), (Ref ((λ b) [(Ref a StaticLifetime), (Ref a StaticLifetime)] Bool) c)] ())
</p>
<pre class="args">
(sort-by! arr f)
@ -1165,7 +1166,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), (Ref ((λ c) [(Ref a d), (Ref a d)] Bool) e)] (Array a))
(λ [(Ref (Array a) b), (Ref ((λ c) [(Ref a StaticLifetime), (Ref a StaticLifetime)] Bool) d)] (Array a))
</p>
<pre class="args">
(sorted-by arr f)
@ -1205,7 +1206,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), Int, Int] (Array a))
(λ [(Ref (Array a) StaticLifetime), Int, Int] (Array a))
</p>
<pre class="args">
(subarray xs start-index end-index)
@ -1225,7 +1226,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), Int] (Array a))
(λ [(Ref (Array a) StaticLifetime), Int] (Array a))
</p>
<pre class="args">
(suffix-array xs start-index)
@ -1245,7 +1246,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] a)
(λ [(Ref (Array a) StaticLifetime)] a)
</p>
<pre class="args">
(sum xs)
@ -1265,7 +1266,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array (Array a)) b)] Int)
(λ [(Ref (Array (Array a)) StaticLifetime)] Int)
</p>
<pre class="args">
(sum-length xs)
@ -1305,7 +1306,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b), Int, Int] ())
(λ [(Ref (Array a) StaticLifetime), Int, Int] ())
</p>
<pre class="args">
(swap! a i j)
@ -1325,7 +1326,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] a)
(λ [(Ref (Array a) StaticLifetime)] a)
</p>
<pre class="args">
(unsafe-first a)
@ -1346,7 +1347,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] a)
(λ [(Ref (Array a) StaticLifetime)] a)
</p>
<pre class="args">
(unsafe-last a)
@ -1357,6 +1358,26 @@
</p>
</div>
<div class="binder">
<a class="anchor" href="#unsafe-nth">
<h3 id="unsafe-nth">
unsafe-nth
</h3>
</a>
<div class="description">
template
</div>
<p class="sig">
(λ [(Ref (Array a) StaticLifetime), Int] (Ref a StaticLifetime))
</p>
<span>
</span>
<p class="doc">
<p>gets a reference to the <code>n</code>th element from an array <code>a</code>.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#zero">
<h3 id="zero">
@ -1387,7 +1408,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c), (Ref d e)] f) g), (Ref (Array b) c), (Ref (Array d) e)] (Array f))
(λ [(Ref ((λ a) [(Ref b StaticLifetime), (Ref c StaticLifetime)] d) e), (Ref (Array b) StaticLifetime), (Ref (Array c) StaticLifetime)] (Array d))
</p>
<pre class="args">
(zip f a b)

View File

@ -268,7 +268,7 @@
external
</div>
<p class="sig">
(λ [&amp;String] ())
(λ [(Ref String a)] ())
</p>
<span>

View File

@ -151,7 +151,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c), (Ref (Map a b) c)] Bool)
(λ [(Ref (Map a b) StaticLifetime), (Ref (Map a b) StaticLifetime)] Bool)
</p>
<pre class="args">
(= m1 m2)
@ -170,7 +170,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c), (Ref d c)] Bool) e), (Ref (Map b d) c)] Bool)
(λ [(Ref ((λ a) [(Ref b StaticLifetime), (Ref c StaticLifetime)] Bool) d), (Ref (Map b c) StaticLifetime)] Bool)
</p>
<pre class="args">
(all? pred m)
@ -210,7 +210,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c), (Ref a c)] Bool)
(λ [(Ref (Map a b) StaticLifetime), (Ref a StaticLifetime)] Bool)
</p>
<pre class="args">
(contains? m k)
@ -330,7 +330,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [(Ref b c), (Ref d c)] d) e), (Map b d)] (Map b d))
(λ [(Ref ((λ a) [(Ref b StaticLifetime), (Ref c StaticLifetime)] c) d), (Map b c)] (Map b c))
</p>
<pre class="args">
(endo-map f m)
@ -350,7 +350,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c), (Ref ((λ d) [(Ref a c), (Ref b c)] ()) e)] ())
(λ [(Ref (Map a b) StaticLifetime), (Ref ((λ c) [(Ref a StaticLifetime), (Ref b StaticLifetime)] ()) d)] ())
</p>
<pre class="args">
(for-each m f)
@ -370,7 +370,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array (Pair a b)) c)] (Map a b))
(λ [(Ref (Array (Pair a b)) StaticLifetime)] (Map a b))
</p>
<pre class="args">
(from-array a)
@ -390,7 +390,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c), (Ref a c)] b)
(λ [(Ref (Map a b) StaticLifetime), (Ref a StaticLifetime)] b)
</p>
<pre class="args">
(get m k)
@ -410,7 +410,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c), (Ref a c)] (Maybe b))
(λ [(Ref (Map a b) StaticLifetime), (Ref a StaticLifetime)] (Maybe b))
</p>
<pre class="args">
(get-maybe m k)
@ -430,7 +430,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c), (Ref a c), (Ref b d)] b)
(λ [(Ref (Map a b) StaticLifetime), (Ref a StaticLifetime), (Ref b c)] b)
</p>
<pre class="args">
(get-with-default m k default-value)
@ -470,7 +470,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c)] (Array a))
(λ [(Ref (Map a b) StaticLifetime)] (Array a))
</p>
<pre class="args">
(keys m)
@ -490,7 +490,7 @@
defn
</div>
<p class="sig">
(λ [(Ref ((λ a) [b, (Ref c d), (Ref e d)] b) f), b, (Ref (Map c e) d)] b)
(λ [(Ref ((λ a) [b, (Ref c StaticLifetime), (Ref d StaticLifetime)] b) e), b, (Ref (Map c d) StaticLifetime)] b)
</p>
<pre class="args">
(kv-reduce f init m)
@ -510,7 +510,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c)] Int)
(λ [(Ref (Map a b) StaticLifetime)] Int)
</p>
<pre class="args">
(length m)
@ -570,7 +570,7 @@
defn
</div>
<p class="sig">
(λ [(Map a b), (Ref a c), (Ref b c)] (Map a b))
(λ [(Map a b), (Ref a StaticLifetime), (Ref b StaticLifetime)] (Map a b))
</p>
<pre class="args">
(put m k v)
@ -590,7 +590,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c), (Ref a d), (Ref b e)] ())
(λ [(Ref (Map a b) StaticLifetime), (Ref a StaticLifetime), (Ref b c)] ())
</p>
<pre class="args">
(put! m k v)
@ -610,7 +610,7 @@
defn
</div>
<p class="sig">
(λ [(Map a b), (Ref a c)] (Map a b))
(λ [(Map a b), (Ref a StaticLifetime)] (Map a b))
</p>
<pre class="args">
(remove m k)
@ -630,7 +630,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c)] (Map b a))
(λ [(Ref (Map a b) StaticLifetime)] (Map b a))
</p>
<pre class="args">
(reverse m)
@ -730,7 +730,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c)] String)
(λ [(Ref (Map a b) StaticLifetime)] String)
</p>
<pre class="args">
(str m)
@ -750,7 +750,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c)] (Array (Pair a b)))
(λ [(Ref (Map a b) StaticLifetime)] (Array (Pair a b)))
</p>
<pre class="args">
(to-array m)
@ -770,7 +770,7 @@
defn
</div>
<p class="sig">
(λ [(Map a b), (Ref a c), (Ref ((λ d) [b] b) c)] (Map a b))
(λ [(Map a b), (Ref a StaticLifetime), (Ref ((λ c) [b] b) StaticLifetime)] (Map a b))
</p>
<pre class="args">
(update m k f)
@ -830,7 +830,7 @@
defn
</div>
<p class="sig">
(λ [(Map a b), (Ref a c), (Ref ((λ d) [b] b) c), b] (Map a b))
(λ [(Map a b), (Ref a StaticLifetime), (Ref ((λ c) [b] b) StaticLifetime), b] (Map a b))
</p>
<pre class="args">
(update-with-default m k f v)
@ -850,7 +850,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Map a b) c)] (Array b))
(λ [(Ref (Map a b) StaticLifetime)] (Array b))
</p>
<pre class="args">
(vals m)

View File

@ -405,27 +405,6 @@ a value using <code>zero</code> if a <code>Nothing</code> is passed.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#ptr">
<h3 id="ptr">
ptr
</h3>
</a>
<div class="description">
defn
</div>
<p class="sig">
(λ [(Ref (Maybe a) b)] (Ptr a))
</p>
<pre class="args">
(ptr a)
</pre>
<p class="doc">
<p>Creates a <code>(Ptr a)</code> from a <code>(Maybe a)</code>. If the <code>Maybe</code> was
<code>Nothing</code>, this function will return a <code>NULL</code> value.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#str">
<h3 id="str">
@ -487,6 +466,27 @@ a value using <code>zero</code> if a <code>Nothing</code> is passed.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#unsafe-ptr">
<h3 id="unsafe-ptr">
unsafe-ptr
</h3>
</a>
<div class="description">
defn
</div>
<p class="sig">
(λ [(Ref (Maybe a) b)] (Ptr a))
</p>
<pre class="args">
(unsafe-ptr a)
</pre>
<p class="doc">
<p>Creates a <code>(Ptr a)</code> from a <code>(Maybe a)</code>. If the <code>Maybe</code> was
<code>Nothing</code>, this function will return a <code>NULL</code> value.</p>
</p>
</div>
</div>
</body>
</html>

View File

@ -170,7 +170,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a), Int] Double)
(λ [(Ref (Array Double) StaticLifetime), Int] Double)
</p>
<pre class="args">
(grouped-median data interval)
@ -190,7 +190,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(high-median data)
@ -230,7 +230,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(low-median data)
@ -250,7 +250,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array a) b)] a)
(λ [(Ref (Array a) StaticLifetime)] a)
</p>
<pre class="args">
(mean data)
@ -270,7 +270,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(median data)
@ -290,7 +290,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(pstdev data)
@ -330,7 +330,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(pvariance data)
@ -370,7 +370,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(stdev data)
@ -390,7 +390,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(stdev-pct data)
@ -409,7 +409,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Summary)
(λ [(Ref (Array Double) StaticLifetime)] Summary)
</p>
<pre class="args">
(summary samples)
@ -429,7 +429,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array Double) a)] Double)
(λ [(Ref (Array Double) StaticLifetime)] Double)
</p>
<pre class="args">
(variance data)

View File

@ -364,7 +364,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array String) a)] String)
(λ [(Ref (Array String) StaticLifetime)] String)
</p>
<pre class="args">
(concat strings)
@ -638,7 +638,7 @@
defn
</div>
<p class="sig">
(λ [(Ref String a), (Ref (Array String) b)] String)
(λ [(Ref String a), (Ref (Array String) StaticLifetime)] String)
</p>
<pre class="args">
(join sep strings)
@ -658,7 +658,7 @@
defn
</div>
<p class="sig">
(λ [Char, (Ref (Array String) a)] String)
(λ [Char, (Ref (Array String) StaticLifetime)] String)
</p>
<pre class="args">
(join-with-char sep strings)
@ -1031,7 +1031,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (Array String) a)] Int)
(λ [(Ref (Array String) StaticLifetime)] Int)
</p>
<pre class="args">
(sum-length strings)

View File

@ -151,7 +151,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) b)] Bool)
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] Bool)
</p>
<pre class="args">
(= a b)
@ -170,7 +170,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) c)] (Maybe (VectorN a)))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe (VectorN a)))
</p>
<pre class="args">
(add a b)
@ -189,7 +189,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) b)] (Maybe a))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe a))
</p>
<pre class="args">
(angle-between a b)
@ -209,7 +209,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) b)] (Maybe Bool))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe Bool))
</p>
<pre class="args">
(anti-parallel? a b)
@ -269,7 +269,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) c)] (Maybe a))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe a))
</p>
<pre class="args">
(dist a b)
@ -289,7 +289,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), a] (VectorN a))
(λ [(Ref (VectorN a) StaticLifetime), a] (VectorN a))
</p>
<pre class="args">
(div a n)
@ -308,7 +308,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) c)] (Maybe a))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe a))
</p>
<pre class="args">
(dot x y)
@ -348,7 +348,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b)] a)
(λ [(Ref (VectorN a) StaticLifetime)] a)
</p>
<pre class="args">
(mag o)
@ -368,7 +368,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b)] a)
(λ [(Ref (VectorN a) StaticLifetime)] a)
</p>
<pre class="args">
(mag-sq o)
@ -388,7 +388,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), a] (VectorN a))
(λ [(Ref (VectorN a) StaticLifetime), a] (VectorN a))
</p>
<pre class="args">
(mul a n)
@ -427,7 +427,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b)] (VectorN a))
(λ [(Ref (VectorN a) StaticLifetime)] (VectorN a))
</p>
<pre class="args">
(normalize o)
@ -447,7 +447,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) b)] (Maybe Bool))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe Bool))
</p>
<pre class="args">
(parallel? a b)
@ -467,7 +467,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) b)] (Maybe Bool))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe Bool))
</p>
<pre class="args">
(perpendicular? a b)
@ -626,7 +626,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) c)] (Maybe (VectorN a)))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime)] (Maybe (VectorN a)))
</p>
<pre class="args">
(sub a b)
@ -705,7 +705,7 @@
defn
</div>
<p class="sig">
(λ [(Ref (VectorN a) b), (Ref (VectorN a) c), a] (Maybe (VectorN a)))
(λ [(Ref (VectorN a) StaticLifetime), (Ref (VectorN a) StaticLifetime), a] (Maybe (VectorN a)))
</p>
<pre class="args">
(vlerp a b amnt)
@ -744,7 +744,7 @@
defn
</div>
<p class="sig">
(λ [((λ a) [b, c] d), (Ref (VectorN b) e), (Ref (VectorN c) f)] (Maybe (VectorN d)))
(λ [((λ a) [b, c] d), (Ref (VectorN b) StaticLifetime), (Ref (VectorN c) StaticLifetime)] (Maybe (VectorN d)))
</p>
<pre class="args">
(zip f a b)
@ -763,7 +763,7 @@
defn
</div>
<p class="sig">
(λ [((λ a) [b, c] d), (Ref (Array b) e), (Ref (Array c) f)] (VectorN d))
(λ [((λ a) [b, c] d), (Ref (Array b) StaticLifetime), (Ref (Array c) StaticLifetime)] (VectorN d))
</p>
<pre class="args">
(zip- f a b)

View File

@ -226,7 +226,7 @@
defn
</div>
<p class="sig">
(λ [(Ref SDLApp a), ((λ b) [(Ref SDLApp a), c, (Ref SDL_Event d)] c), ((λ e) [c] c), ((λ e) [(Ref SDLApp a), (Ptr SDL_Renderer), (Ref c f)] ()), c] ())
(λ [(Ref SDLApp a), ((λ b) [(Ref SDLApp a), c, (Ref SDL_Event StaticLifetime)] c), ((λ d) [c] c), ((λ d) [(Ref SDLApp a), (Ptr SDL_Renderer), (Ref c e)] ()), c] ())
</p>
<pre class="args">
(run-with-callbacks app event-fn update-fn draw-fn initial-state)

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))
@ -141,7 +141,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

@ -176,8 +176,8 @@ templateNth :: (String, Binder)
templateNth =
let t = VarTy "t"
in defineTemplate
(SymPath ["Array"] "nth")
(FuncTy StaticLifetimeTy [RefTy (StructTy "Array" [t]) (VarTy "q"), IntTy] (RefTy t (VarTy "q")))
(SymPath ["Array"] "unsafe-nth")
(FuncTy StaticLifetimeTy [RefTy (StructTy "Array" [t]) StaticLifetimeTy, IntTy] (RefTy t StaticLifetimeTy))
"gets a reference to the `n`th element from an array `a`."
(toTemplate "$t* $NAME (Array *aRef, int n)")
(toTemplate $ unlines ["$DECL {"

View File

@ -724,6 +724,12 @@ commandSymFrom [XObj (Bol b) i t] = return $ Right $ XObj (sFrom_ (show b)) i t
commandSymFrom [x] =
return $ Left (EvalError ("Cant call `from` with " ++ pretty x) (info x))
commandSymStr :: CommandCallback
commandSymStr [XObj (Sym s _) i _] =
return $ Right $ XObj (Str (show s)) i (Just StringTy)
commandSymStr [x] =
return $ Left (EvalError ("Cant call `str` with " ++ pretty x) (info x))
sFrom_ s = Sym (SymPath [] s) (LookupGlobal CarpLand AVariable)
simpleFromNum (Num IntTy num) = show (round num :: Int)

View File

@ -737,10 +737,12 @@ toDeclaration (Binder meta xobj@(XObj (Lst xobjs) _ t)) =
toDeclaration _ = error "Missing case."
paramListToC :: [XObj] -> String
paramListToC xobjs = intercalate ", " (map getParam xobjs)
paramListToC xobjs = intercalate ", " (map getParam (filter notUnit xobjs))
where getParam :: XObj -> String
getParam (XObj (Sym (SymPath _ name) _) _ (Just t)) = tyToCLambdaFix t ++ " " ++ mangle name
getParam invalid = error (show (InvalidParameter invalid))
notUnit (XObj _ _ (Just UnitTy)) = False
notUnit _ = True
projectIncludesToC :: Project -> String
projectIncludesToC proj = intercalate "\n" (map includerToC includes) ++ "\n\n"

View File

@ -147,6 +147,11 @@ eval env xobj =
_ -> return (makeEvalError ctx Nothing ("`if` condition contains non-boolean value: " ++ pretty okCondition) (info okCondition))
Left err -> return (Left err)
[XObj (Fn _ _) _ _, args@(XObj (Arr a) _ _), _] ->
if all isUnqualifiedSym a
then return (Right listXObj)
else return (makeEvalError ctx Nothing ("`fn` requires all arguments to be unqualified symbols, but it got `" ++ pretty args ++ "`") (info xobj))
[defnExpr@(XObj (Defn _) _ _), name, args@(XObj (Arr a) _ _), body] ->
case obj name of
(Sym (SymPath [] _) _) ->

View File

@ -278,6 +278,7 @@ dynamicSymModule = Env { envBindings = bindings
where bindings = Map.fromList [ addCommand "join" 1 commandSymJoin
, addCommand "prefix" 2 commandSymPrefix
, addCommand "from" 1 commandSymFrom
, addCommand "str" 1 commandSymStr
]
-- | A submodule of the Dynamic module. Contains functions for working with the active Carp project.

View File

@ -23,9 +23,8 @@ memberPrn typeEnv env (memberName, memberTy) =
]
Nothing ->
if isExternalType typeEnv memberTy
then unlines [ " tempsize = snprintf(NULL, 0, \"%p\", p->" ++ memberName ++ ");"
, " temp = malloc(tempsize);"
, " snprintf(temp, tempsize, \"%p\", p->" ++ memberName ++ ");"
then unlines [ " temp = malloc(11);"
, " snprintf(temp, 11, \"<external>\");"
, " snprintf(bufferPtr, size, \"%s \", temp);"
, " bufferPtr += strlen(temp) + 1;"
, " if(temp) { CARP_FREE(temp); temp = NULL; }"
@ -44,7 +43,7 @@ memberPrnSize typeEnv env (memberName, memberTy) =
]
Nothing ->
if isExternalType typeEnv memberTy
then unlines [" size += snprintf(NULL, 0, \"%p \", p->" ++ memberName ++ ");"
then unlines [" size += 11;"
," if(temp) { CARP_FREE(temp); temp = NULL; }"
]
else " // Failed to find str function for " ++ memberName ++ " : " ++ show memberTy ++ "\n"

View File

@ -2,7 +2,7 @@
(defn f []
(let [xs [@"A" @"B" @"C"]
r (Array.nth &xs 0) ;; The lifetime has to be propagated via the call to 'nth' for this to work
r (Array.unsafe-nth &xs 0) ;; The lifetime has to be propagated via the call to 'nth' for this to work
q r] ;; An extra copy of the ref just to make things more tricky
(do
(Array.delete xs)

View File

@ -116,13 +116,21 @@
&(suffix-array &(range 1 10 1) 7)
"suffix-array works as expected")
(assert-equal test
5
@(nth &a 5)
&(Maybe.Nothing)
&(nth &a 100)
"nth works as expected")
(assert-equal test
&[1 2 3]
(nth &(nested) 0)
&(Maybe.Just 0)
&(nth &a 0)
"nth works as expected")
(assert-equal test
5
@(unsafe-nth &a 5)
"unsafe-nth works as expected")
(assert-equal test
&[1 2 3]
(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

@ -75,12 +75,12 @@
)
(assert-equal test
1
@(Pointer.to-ref (ptr &(Just 1)))
"ptr works on Just"
@(Pointer.to-ref (unsafe-ptr &(Just 1)))
"unsafe-ptr works on Just"
)
(assert-true test
(null? (ptr &(the (Maybe Int) (Nothing))))
"ptr works on Nothing"
(null? (unsafe-ptr &(the (Maybe Int) (Nothing))))
"unsafe-ptr works on Nothing"
)
(assert-equal test
&(Just 0)

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))))