ocaml: phase 6 List.take/drop/filter_map/flat_map (+6 tests, 400 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s

Common functional helpers written in OCaml. flat_map / concat_map
share an implementation. 400-test milestone.
This commit is contained in:
2026-05-08 15:30:29 +00:00
parent 86343345dc
commit 9f05e24c52
3 changed files with 54 additions and 0 deletions

View File

@@ -198,6 +198,35 @@
match ys with
| [] -> failwith \"List.map2: unequal\"
| hy :: ty -> f hx hy :: map2 f tx ty
let rec take n xs =
if n <= 0 then []
else
match xs with
| [] -> []
| h :: t -> h :: take (n - 1) t
let rec drop n xs =
if n <= 0 then xs
else
match xs with
| [] -> []
| _ :: t -> drop (n - 1) t
let rec filter_map f xs =
match xs with
| [] -> []
| h :: t ->
match f h with
| None -> filter_map f t
| Some v -> v :: filter_map f t
let rec flat_map f xs =
match xs with
| [] -> []
| h :: t -> append (f h) (flat_map f t)
let concat_map = flat_map
end ;;
module Option = struct

View File

@@ -976,6 +976,20 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 2804)
(eval "(ocaml-run \"match Blue with | (Red | Green) -> 1 | Blue -> 2\")")
;; ── More List utilities (take/drop/filter_map/flat_map) ──────
(epoch 2900)
(eval "(ocaml-run \"List.take 3 [1;2;3;4;5]\")")
(epoch 2901)
(eval "(ocaml-run \"List.drop 2 [1;2;3;4;5]\")")
(epoch 2902)
(eval "(ocaml-run \"List.filter_map (fun x -> if x > 2 then Some (x * 10) else None) [1;2;3;4]\")")
(epoch 2903)
(eval "(ocaml-run \"List.flat_map (fun x -> [x; x]) [1;2;3]\")")
(epoch 2904)
(eval "(ocaml-run \"List.take 0 [1;2;3]\")")
(epoch 2905)
(eval "(ocaml-run \"List.take 100 [1;2;3]\")")
EPOCHS
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -1544,6 +1558,14 @@ check 2802 "(1|2|3) match 5" '0'
check 2803 "(Red|Green) Red" '1'
check 2804 "(Red|Green) Blue" '2'
# ── List.take/drop/filter_map/flat_map ─────────────────────────
check 2900 "List.take 3" '(1 2 3)'
check 2901 "List.drop 2" '(3 4 5)'
check 2902 "List.filter_map" '(30 40)'
check 2903 "List.flat_map double" '(1 1 2 2 3 3)'
check 2904 "List.take 0" '()'
check 2905 "List.take overflow" '(1 2 3)'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"