ocaml: phase 6 List.sort_uniq + List.find_map (+2 tests, 503 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 41s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 41s
sort_uniq:
Sort with the user comparator, then walk the sorted list dropping
any element equal to its predecessor. Output is sorted and unique.
List.sort_uniq compare [3;1;2;1;3;2;4] = [1;2;3;4]
find_map:
Walk until the user fn returns Some v; return that. If all None,
return None.
List.find_map (fun x -> if x > 5 then Some (x * 2) else None)
[1;2;3;6;7]
= Some 12
Both defined in OCaml syntax in runtime.sx — no host primitive
needed since they're pure list traversals over existing operations.
This commit is contained in:
@@ -158,6 +158,26 @@
|
||||
|
||||
let stable_sort = sort
|
||||
|
||||
let rec sort_uniq cmp xs =
|
||||
let sorted = sort cmp xs in
|
||||
let rec dedup ys =
|
||||
match ys with
|
||||
| [] -> []
|
||||
| [x] -> [x]
|
||||
| a :: b :: rest ->
|
||||
if cmp a b = 0 then dedup (b :: rest)
|
||||
else a :: dedup (b :: rest)
|
||||
in
|
||||
dedup sorted
|
||||
|
||||
let rec find_map f xs =
|
||||
match xs with
|
||||
| [] -> None
|
||||
| h :: t ->
|
||||
match f h with
|
||||
| Some v -> Some v
|
||||
| None -> find_map f t
|
||||
|
||||
let rec combine xs ys =
|
||||
match xs with
|
||||
| [] -> (match ys with
|
||||
|
||||
Reference in New Issue
Block a user