ocaml: phase 6 Map/Set extensions iter/fold/filter/union/inter (+4 tests, 422 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Map.Make: iter, fold, map, filter, is_empty added.
Set.Make: iter, fold, filter, is_empty, union, inter added.
All written in OCaml inside the existing functor bodies. Tested:
IntMap.fold (fun k v acc -> acc + v) m 0 = 30
IntSet.elements (IntSet.union {1,2} {2,3}) = [1; 2; 3]
IntSet.elements (IntSet.inter {1,2,3} {2,3,4}) = [2; 3]
This commit is contained in:
@@ -435,6 +435,32 @@
|
||||
match m with
|
||||
| [] -> 0
|
||||
| _ :: t -> 1 + cardinal t
|
||||
|
||||
let rec iter f m =
|
||||
match m with
|
||||
| [] -> ()
|
||||
| (k, v) :: t -> f k v; iter f t
|
||||
|
||||
let rec fold f m acc =
|
||||
match m with
|
||||
| [] -> acc
|
||||
| (k, v) :: t -> fold f t (f k v acc)
|
||||
|
||||
let rec map f m =
|
||||
match m with
|
||||
| [] -> []
|
||||
| (k, v) :: t -> (k, f v) :: map f t
|
||||
|
||||
let rec filter p m =
|
||||
match m with
|
||||
| [] -> []
|
||||
| (k, v) :: t ->
|
||||
if p k v then (k, v) :: filter p t else filter p t
|
||||
|
||||
let rec is_empty m =
|
||||
match m with
|
||||
| [] -> true
|
||||
| _ -> false
|
||||
end
|
||||
end ;;
|
||||
|
||||
@@ -472,6 +498,36 @@
|
||||
match s with
|
||||
| [] -> 0
|
||||
| _ :: t -> 1 + cardinal t
|
||||
|
||||
let rec iter f s =
|
||||
match s with
|
||||
| [] -> ()
|
||||
| h :: t -> f h; iter f t
|
||||
|
||||
let rec fold f s acc =
|
||||
match s with
|
||||
| [] -> acc
|
||||
| h :: t -> fold f t (f h acc)
|
||||
|
||||
let rec filter p s =
|
||||
match s with
|
||||
| [] -> []
|
||||
| h :: t -> if p h then h :: filter p t else filter p t
|
||||
|
||||
let rec is_empty s =
|
||||
match s with
|
||||
| [] -> true
|
||||
| _ -> false
|
||||
|
||||
let rec union a b =
|
||||
match b with
|
||||
| [] -> a
|
||||
| h :: t -> union (add h a) t
|
||||
|
||||
let rec inter a b =
|
||||
match a with
|
||||
| [] -> []
|
||||
| h :: t -> if mem h b then h :: inter t b else inter t b
|
||||
end
|
||||
end")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user