Skip to content

Commit 3d369f3

Browse files
committed
Use Util.Result.List.split instead of filter_map and concat_map
Signed-off-by: Nathan Rebours <[email protected]>
1 parent a2fb3c1 commit 3d369f3

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

lib/mdx.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,8 @@ let parse l =
6464
| `Block rb -> Block.from_raw rb >>= fun b -> Ok (Block b))
6565
l
6666
in
67-
let errors =
68-
Util.List.concat_map (function Ok _ -> [] | Error l -> l) results
69-
in
70-
let ok =
71-
List.filter_map (function Ok x -> Some x | Error _ -> None) results
72-
in
73-
match errors with [] -> Ok ok | _ -> Error errors
67+
let ok, errors = Util.Result.List.split results in
68+
match errors with [] -> Ok ok | _ -> Error (List.concat errors)
7469

7570
let parse_lexbuf file_contents syntax l =
7671
match syntax with

lib/util.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ module Result = struct
4545
let map ~f l =
4646
fold ~f:(fun acc elm -> f elm >>| fun elm' -> elm' :: acc) ~init:[] l
4747
>>| List.rev
48+
49+
let split l =
50+
let rec split_rec oks errors l =
51+
match l with
52+
| [] -> (List.rev oks, List.rev errors)
53+
| Ok x :: tl -> split_rec (x :: oks) errors tl
54+
| Error x :: tl -> split_rec oks (x :: errors) tl
55+
in
56+
split_rec [] [] l
4857
end
4958
end
5059

@@ -95,15 +104,6 @@ module List = struct
95104
| h :: t -> ( match f h with Some x -> Some x | None -> aux t)
96105
in
97106
aux l
98-
99-
let concat_map f l =
100-
let rec aux f acc = function
101-
| [] -> List.rev acc
102-
| x :: l ->
103-
let xs = f x in
104-
aux f (List.rev_append xs acc) l
105-
in
106-
aux f [] l
107107
end
108108

109109
module Array = struct

lib/util.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Result : sig
3636
('acc, 'err) result
3737

3838
val map : f:('a -> ('b, 'err) result) -> 'a list -> ('b list, 'err) result
39+
val split : ('a, 'err) result list -> 'a list * 'err list
3940
end
4041
end
4142

@@ -50,7 +51,6 @@ end
5051

5152
module List : sig
5253
val find_map : ('a -> 'b option) -> 'a list -> 'b option
53-
val concat_map : ('a -> 'b list) -> 'a list -> 'b list
5454
end
5555

5656
module String : sig

0 commit comments

Comments
 (0)