diff --git a/core/Sort.carp b/core/Sort.carp index a586d3af..f98e7897 100644 --- a/core/Sort.carp +++ b/core/Sort.carp @@ -1,4 +1,4 @@ -(defmodule Sort +(defmodule Array (doc sort! "Perform an in-place heapsort of a given array.") (defn sort! [arr] (HeapSort.sort! arr)) @@ -11,17 +11,3 @@ (defn sort [arr] (HeapSort.sort arr)) ) - -(defmodule Array - (doc sort! "Perform an in-place heapsort of a given array.") - (defn sort! [arr] - (Sort.sort! arr)) - - (doc sorted "Perform a heapsort in a new copy of given array.") - (defn sorted [arr] - (Sort.sorted arr)) - - (doc sort "Perform an in-place heapsort of a given owned array.") - (defn sort [arr] - (Sort.sort arr)) -) diff --git a/core/Statistics.carp b/core/Statistics.carp index c4617252..9af675a6 100644 --- a/core/Statistics.carp +++ b/core/Statistics.carp @@ -1,6 +1,6 @@ (use Int) (use Double) -(use Sort) +(use Array) (defmodule Statistics (deftype Summary [ @@ -50,7 +50,7 @@ (doc median "Compute the median of the samples data.") (defn median [data] (let [n (Array.length data) - sorted (Sort.sorted data)] + sorted (Array.sorted data)] (cond (= n 0) 0.0 (= (mod n 2) 1) @(Array.nth data (/ n 2)) (let [mid (/ n 2)] ; else @@ -61,7 +61,7 @@ (doc low-median "Compute the low median of the samples data.") (defn low-median [data] (let [n (Array.length data) - sorted (Sort.sorted 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 @@ -69,7 +69,7 @@ (doc high-median "Compute the high median of the samples data.") (defn high-median [data] (let [n (Array.length data) - sorted (Sort.sorted data)] + sorted (Array.sorted data)] (if (= n 0) 0.0 @(Array.nth data (/ n 2))))) @@ -77,7 +77,7 @@ (doc grouped-median "Compute the grouped median of the samples data.") (defn grouped-median [data interval] (let [n (Array.length data) - sorted (Sort.sorted data)] + sorted (Array.sorted data)] (cond (= n 0) 0.0 (= n 1) @(Array.nth data 0) (let [x @(Array.nth data (/ n 2)) ; else @@ -145,7 +145,7 @@ (doc quartiles "Compute the quartiles of the samples data.") (defn quartiles [data] - (let [tmp (Sort.sorted data) + (let [tmp (Array.sorted data) first 25.0 second 50.0 third 75.0 @@ -161,7 +161,7 @@ (hidden winsorize) (defn winsorize [samples pct] - (let [tmp &(Sort.sorted samples) + (let [tmp &(Array.sorted samples) lo (Statistics.percentile-of-sorted tmp pct) hi (Statistics.percentile-of-sorted tmp (Double.- 100.0 pct))] (do diff --git a/docs/core/Array.html b/docs/core/Array.html index 535c879b..5d9b04a5 100644 --- a/docs/core/Array.html +++ b/docs/core/Array.html @@ -1 +1 @@ -

Array

=

defn

(λ [(Ref (Array a)), (Ref (Array a))] Bool)

Compare two arrays.

allocate

template

(λ [Int] (Array t))

aset

template

(λ [(Array t), Int, t] (Array t))

aset!

template

(λ [(Ref (Array t)), Int, t] ())

aset-uninitialized!

template

(λ [(Ref (Array t)), Int, t] ())

aupdate

defn

(λ [(Array a), Int, (λ [&a] a)] (Array a))

Transmute the element at index i of array a using function f.

aupdate!

defn

(λ [(Ref (Array a)), Int, (λ [&a] a)] ())

Transmute the element at index i of array a using function f in place.

concat

defn

(λ [(Ref (Array (Array a)))] (Array a))

Returns a new Array which is the concatenation of the provided `xs`.

copy

template

(λ [(Ref (Array a))] (Array a))

copy-map

defn

(λ [(λ [&a] b), (Ref (Array a))] (Array b))

Map over array a using function f (copies the array).

delete

template

(λ [(Array a)] ())

element-count

defn

(λ [(Ref (Array a)), &a] Int)

Count occurrences of element e in an array.

endo-map

template

(λ [(λ [a] a), (Array a)] (Array a))

enumerated

defn

(λ [(Ref (Array a))] (Array (Pair Int a)))

Create a new array of Pair:s where the first position is the index and the second position is the element from the original array.

filter

template

(λ [(λ [&a] Bool), (Array a)] (Array a))

first

defn

(λ [(Ref (Array a))] a)

Take the first element of an array.

index-of

defn

(λ [(Ref (Array a)), a] Int)

Get the index of element e in an array.

last

defn

(λ [(Ref (Array a))] a)

Take the last element of an array.

length

template

(λ [(Ref (Array t))] Int)

maximum

defn

(λ [(Ref (Array a))] a)

Get the maximum in an array (elements must support <).

minimum

defn

(λ [(Ref (Array a))] a)

Sum an array (elements must support + and zero).

nth

template

(λ [(Ref (Array t)), Int] &t)

pop-back

template

(λ [(Array a)] (Array a))

pop-back!

template

(λ [(Ref (Array a))] a)

prefix-array

defn

(λ [(Ref (Array a)), Int] (Array a))

Get prefix-array to end-index.

prn

defn

(λ [(Ref (Array a))] String)

push-back

template

(λ [(Array a), a] (Array a))

push-back!

template

(λ [(Ref (Array a)), a] ())

range

defn

(λ [Int, Int, Int] (Array Int))

Create an array from start to end with step between them (the elements must support <, <=, and >=).

raw

template

(λ [(Array t)] (Ptr t))

reduce

defn

(λ [(λ [&a, &b] a), a, (Ref (Array b))] a)

Reduce an array, using the function f.

repeat

defn

(λ [Int, (λ [] a)] (Array a))

Repeat function f n times and store the results in an array.

repeat-indexed

defn

(λ [Int, (λ [Int] a)] (Array a))

Repeat function f n times and store the results in an array (will be supplied with the index).

replicate

defn

(λ [Int, &a] (Array a))

Repeat element e n times and store the results in an array.

reverse

defn

(λ [(Array a)] (Array a))

Reverse an array.

str

template

(λ [(Ref (Array a))] String)

subarray

defn

(λ [(Ref (Array a)), Int, Int] (Array a))

Get subarray from start-index to end-index.

suffix-array

defn

(λ [(Ref (Array a)), Int] (Array a))

Get subarray from start-index.

sum

defn

(λ [(Ref (Array a))] a)

sum-length

defn

(λ [(Ref (Array (Array a)))] Int)

Returns the sum of lengths from an Array of Arrays.

swap

defn

(λ [(Array a), Int, Int] (Array a))

Swap indices i and j of array a.

swap!

defn

(λ [(Ref (Array a)), Int, Int] ())

Swap indices i and j of array a in place.

zip

defn

(λ [(λ [&a, &b] c), (Ref (Array a)), (Ref (Array b))] (Array c))

Map over two arrays using a function that takes two arguments. Produces a new array with the length of the shorter input.

\ No newline at end of file +

Array

=

defn

(λ [(Ref (Array a)), (Ref (Array a))] Bool)

Compare two arrays.

allocate

template

(λ [Int] (Array t))

aset

template

(λ [(Array t), Int, t] (Array t))

aset!

template

(λ [(Ref (Array t)), Int, t] ())

aset-uninitialized!

template

(λ [(Ref (Array t)), Int, t] ())

aupdate

defn

(λ [(Array a), Int, (λ [&a] a)] (Array a))

Transmute the element at index i of array a using function f.

aupdate!

defn

(λ [(Ref (Array a)), Int, (λ [&a] a)] ())

Transmute the element at index i of array a using function f in place.

concat

defn

(λ [(Ref (Array (Array a)))] (Array a))

Returns a new Array which is the concatenation of the provided `xs`.

copy

template

(λ [(Ref (Array a))] (Array a))

copy-map

defn

(λ [(λ [&a] b), (Ref (Array a))] (Array b))

Map over array a using function f (copies the array).

delete

template

(λ [(Array a)] ())

element-count

defn

(λ [(Ref (Array a)), &a] Int)

Count occurrences of element e in an array.

endo-map

template

(λ [(λ [a] a), (Array a)] (Array a))

enumerated

defn

(λ [(Ref (Array a))] (Array (Pair Int a)))

Create a new array of Pair:s where the first position is the index and the second position is the element from the original array.

filter

template

(λ [(λ [&a] Bool), (Array a)] (Array a))

first

defn

(λ [(Ref (Array a))] a)

Take the first element of an array.

index-of

defn

(λ [(Ref (Array a)), a] Int)

Get the index of element e in an array.

last

defn

(λ [(Ref (Array a))] a)

Take the last element of an array.

length

template

(λ [(Ref (Array t))] Int)

maximum

defn

(λ [(Ref (Array a))] a)

Get the maximum in an array (elements must support <).

minimum

defn

(λ [(Ref (Array a))] a)

Sum an array (elements must support + and zero).

nth

template

(λ [(Ref (Array t)), Int] &t)

pop-back

template

(λ [(Array a)] (Array a))

pop-back!

template

(λ [(Ref (Array a))] a)

prefix-array

defn

(λ [(Ref (Array a)), Int] (Array a))

Get prefix-array to end-index.

prn

defn

(λ [(Ref (Array a))] String)

push-back

template

(λ [(Array a), a] (Array a))

push-back!

template

(λ [(Ref (Array a)), a] ())

range

defn

(λ [Int, Int, Int] (Array Int))

Create an array from start to end with step between them (the elements must support <, <=, and >=).

raw

template

(λ [(Array t)] (Ptr t))

reduce

defn

(λ [(λ [&a, &b] a), a, (Ref (Array b))] a)

Reduce an array, using the function f.

repeat

defn

(λ [Int, (λ [] a)] (Array a))

Repeat function f n times and store the results in an array.

repeat-indexed

defn

(λ [Int, (λ [Int] a)] (Array a))

Repeat function f n times and store the results in an array (will be supplied with the index).

replicate

defn

(λ [Int, &a] (Array a))

Repeat element e n times and store the results in an array.

reverse

defn

(λ [(Array a)] (Array a))

Reverse an array.

sort

defn

(λ [(Array a)] (Array a))

Perform an in-place heapsort of a given owned array.

sort!

defn

(λ [(Ref (Array a))] ())

Perform an in-place heapsort of a given array.

sorted

defn

(λ [(Ref (Array a))] (Array a))

Perform a heapsort in a new copy of given array.

str

template

(λ [(Ref (Array a))] String)

subarray

defn

(λ [(Ref (Array a)), Int, Int] (Array a))

Get subarray from start-index to end-index.

suffix-array

defn

(λ [(Ref (Array a)), Int] (Array a))

Get subarray from start-index.

sum

defn

(λ [(Ref (Array a))] a)

sum-length

defn

(λ [(Ref (Array (Array a)))] Int)

Returns the sum of lengths from an Array of Arrays.

swap

defn

(λ [(Array a), Int, Int] (Array a))

Swap indices i and j of array a.

swap!

defn

(λ [(Ref (Array a)), Int, Int] ())

Swap indices i and j of array a in place.

zip

defn

(λ [(λ [&a, &b] c), (Ref (Array a)), (Ref (Array b))] (Array c))

Map over two arrays using a function that takes two arguments. Produces a new array with the length of the shorter input.

\ No newline at end of file diff --git a/examples/sorting.carp b/examples/sorting.carp index e60df493..e9234cd4 100644 --- a/examples/sorting.carp +++ b/examples/sorting.carp @@ -1,5 +1,4 @@ (use Array) -(use Sort) (Project.no-echo) (deftype Age [x Int]) @@ -14,7 +13,7 @@ (Int.< @(Age.x a) @(Age.x b)))) (defn main [] - (let-do [ints (Sort.sort [10 3 75 40]) - ages (Sort.sort [(Age.init 10) (Age.init 3) (Age.init 75) (Age.init 40)])] + (let-do [ints (Array.sort [10 3 75 40]) + ages (Array.sort [(Age.init 10) (Age.init 3) (Age.init 75) (Age.init 40)])] (IO.println &(Array.str &ints)) (IO.println &(Array.str &ages)))) diff --git a/test/memory.carp b/test/memory.carp index bda329ad..bb43b1c3 100644 --- a/test/memory.carp +++ b/test/memory.carp @@ -1,5 +1,6 @@ (load "Test.carp") +(use Array) (use IO) (use Int) (use Float) @@ -7,7 +8,6 @@ (use Array) (use System) (use Char) -(use Sort) (use Test) (Debug.sanitize-addresses) @@ -287,12 +287,12 @@ ;; (Array.length b)))) ;; (defn array-sort-1 [] ;; (let [xs [[0 0] [0 0 0] [0 0 0 0] [0]] -;; ys (Sort.sort xs)] +;; ys (Array.sort xs)] ;; (assert (= &[[0] [0 0] [0 0 0] [0 0 0 0]] &ys)))) (defn array-sort-2 [] (let [xs [5 2 4 3 1] - ys (Sort.sort xs)] + ys (Array.sort xs)] (assert (= &[1 2 3 4 5] &ys)))) (defn f [] @"Hello") diff --git a/test/sort.carp b/test/sort.carp index 9ba285af..de23cf2f 100644 --- a/test/sort.carp +++ b/test/sort.carp @@ -1,4 +1,4 @@ -(use Sort) +(use Array) (load "Test.carp") (use Test) @@ -7,95 +7,95 @@ (with-test test (let-do [arr [1 3 4 2 6 1] exp [1 1 2 3 4 6]] - (Sort.sort! &arr) + (Array.sort! &arr) (assert-equal test &exp &arr - "Sort.sort! works with integers")) + "Array.sort! works with integers")) (let-do [arr [1 3 4 2 6 1] exp [1 1 2 3 4 6] - res (Sort.sorted &arr)] + res (Array.sorted &arr)] (assert-equal test &exp &res - "Sort.sorted works with integers")) + "Array.sorted works with integers")) - (let-do [res (Sort.sort [1 3 4 2 6 1]) + (let-do [res (Array.sort [1 3 4 2 6 1]) exp [1 1 2 3 4 6]] (assert-equal test &exp &res - "Sort.sort works with integers")) + "Array.sort works with integers")) (let-do [arr [1.0 0.8 12.4 3.2] exp [0.8 1.0 3.2 12.4]] - (Sort.sort! &arr) + (Array.sort! &arr) (assert-equal test &exp &arr - "Sort.sort! works with floats")) + "Array.sort! works with floats")) (let-do [arr [1.0 0.8 12.4 3.2] exp [0.8 1.0 3.2 12.4] - res (Sort.sorted &arr)] + res (Array.sorted &arr)] (assert-equal test &exp &res - "Sort.sorted works with floats")) + "Array.sorted works with floats")) - (let-do [res (Sort.sort [1.0 0.8 12.4 3.2]) + (let-do [res (Array.sort [1.0 0.8 12.4 3.2]) exp [0.8 1.0 3.2 12.4]] (assert-equal test &exp &res - "Sort.sort works with floats")) + "Array.sort works with floats")) (let-do [arr [@"aaac" @"aaaa" @"aaab" @"aaad"] exp [@"aaaa" @"aaab" @"aaac" @"aaad"]] - (Sort.sort! &arr) + (Array.sort! &arr) (assert-equal test &exp &arr - "Sort.sort! works with strings")) + "Array.sort! works with strings")) (let-do [arr [@"aaac" @"aaaa" @"aaab" @"aaad"] exp [@"aaaa" @"aaab" @"aaac" @"aaad"] - res (Sort.sorted &arr)] + res (Array.sorted &arr)] (assert-equal test &exp &res - "Sort.sorted works with strings")) + "Array.sorted works with strings")) - (let-do [res (Sort.sort [@"aaac" @"aaaa" @"aaab" @"aaad"]) + (let-do [res (Array.sort [@"aaac" @"aaaa" @"aaab" @"aaad"]) exp [@"aaaa" @"aaab" @"aaac" @"aaad"]] (assert-equal test &exp &res - "Sort.sort works with strings")) + "Array.sort works with strings")) (let-do [arr [\d \a \c \b] exp [\a \b \c \d]] - (Sort.sort! &arr) + (Array.sort! &arr) (assert-equal test &exp &arr - "Sort.sort! works with chars")) + "Array.sort! works with chars")) (let-do [arr [\d \a \c \b] exp [\a \b \c \d] - res (Sort.sorted &arr)] + res (Array.sorted &arr)] (assert-equal test &exp &res - "Sort.sorted works with chars")) + "Array.sorted works with chars")) - (let-do [res (Sort.sort [\d \a \c \b]) + (let-do [res (Array.sort [\d \a \c \b]) exp [\a \b \c \d]] (assert-equal test &exp &res - "Sort.sort works with chars")) + "Array.sort works with chars")) (print-test-results test)))