ocaml: phase 6 Array.sort/sub/append/exists/for_all/mem (+5 tests, 520 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Eight new Array functions, all in OCaml syntax inside runtime.sx, delegating to the corresponding List operation on the cell's underlying list: sort cmp a -> a := List.sort cmp !a (* mutates the cell *) stable_sort = sort fast_sort = sort append a b -> ref (List.append !a !b) sub a pos n -> ref (take n (drop pos !a)) exists p -> List.exists p !a for_all p -> List.for_all p !a mem x a -> List.mem x !a Round-trip: let a = Array.of_list [3;1;4;1;5;9;2;6] in Array.sort compare a; Array.to_list a = [1;1;2;3;4;5;6;9]
This commit is contained in:
@@ -560,6 +560,31 @@
|
||||
let to_list a = !a
|
||||
let of_list xs = ref xs
|
||||
let copy a = ref !a
|
||||
let sort cmp a = a := List.sort cmp !a
|
||||
let stable_sort = sort
|
||||
let fast_sort = sort
|
||||
|
||||
let append a b = ref (List.append !a !b)
|
||||
|
||||
let sub a pos n =
|
||||
let rec take xs k =
|
||||
if k = 0 then []
|
||||
else match xs with
|
||||
| [] -> []
|
||||
| h :: t -> h :: take t (k - 1)
|
||||
in
|
||||
let rec drop xs k =
|
||||
if k = 0 then xs
|
||||
else match xs with
|
||||
| [] -> []
|
||||
| _ :: t -> drop t (k - 1)
|
||||
in
|
||||
ref (take (drop !a pos) n)
|
||||
|
||||
let exists p a = List.exists p !a
|
||||
let for_all p a = List.for_all p !a
|
||||
let mem x a = List.mem x !a
|
||||
|
||||
let blit src si dst di n =
|
||||
for k = 0 to n - 1 do
|
||||
set dst (di + k) (get src (si + k))
|
||||
|
||||
Reference in New Issue
Block a user