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
|
||||
|
||||
@@ -1248,6 +1248,12 @@ cat > "$TMPFILE" << 'EPOCHS'
|
||||
(epoch 4982)
|
||||
(eval "(ocaml-run \"String.of_seq (List.rev (String.to_seq \\\"hello\\\"))\")")
|
||||
|
||||
;; ── List.sort_uniq / List.find_map ───────────────────────────
|
||||
(epoch 4990)
|
||||
(eval "(ocaml-run \"List.sort_uniq compare [3;1;2;1;3;2;4]\")")
|
||||
(epoch 4991)
|
||||
(eval "(ocaml-run \"List.find_map (fun x -> if x > 5 then Some (x * 2) else None) [1;2;3;6;7]\")")
|
||||
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
@@ -1981,6 +1987,10 @@ check 4980 "String.iter sum codes abc" '294'
|
||||
check 4981 "String.fold_left sum hi" '209'
|
||||
check 4982 "String.of_seq (rev to_seq)" '"olleh"'
|
||||
|
||||
# ── List.sort_uniq / List.find_map ──────────────────────────────
|
||||
check 4990 "sort_uniq dedupes & sorts" '(1 2 3 4)'
|
||||
check 4991 "find_map first >5 doubled" '("Some" 12)'
|
||||
|
||||
TOTAL=$((PASS + FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
||||
|
||||
Reference in New Issue
Block a user