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
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user