List.filter_map (#2185)
parent
9f074433d3
commit
742c65d30b
3
Changes
3
Changes
|
@ -184,6 +184,9 @@ Working version
|
|||
hint that can emit non-whitespace characters.
|
||||
(Vladimir Keleshev and Pierre Weis, review by Josh Berdine, Gabriel Radanne)
|
||||
|
||||
- GPR#2185: Add `List.filter_map`
|
||||
(Thomas Refis, review by Alain Frisch and Gabriel Scherer)
|
||||
|
||||
### Other libraries:
|
||||
|
||||
- GPR#1061: Add ?follow parameter to Unix.link. This allows hardlinking
|
||||
|
|
|
@ -649,7 +649,7 @@ let to_clambda_program t env constants (program : Flambda.program) =
|
|||
fields
|
||||
in
|
||||
let init_fields =
|
||||
Misc.Stdlib.List.filter_map (function
|
||||
List.filter_map (function
|
||||
| (i, field, None) -> Some (i, field)
|
||||
| (_, _, Some _) -> None)
|
||||
fields
|
||||
|
|
|
@ -527,7 +527,7 @@ let constant_dependencies ~backend:_
|
|||
| Allocated_const _ -> Symbol.Set.empty
|
||||
| Block (_, fields) ->
|
||||
let symbol_fields =
|
||||
Misc.Stdlib.List.filter_map
|
||||
List.filter_map
|
||||
(function
|
||||
| (Symbol s : Flambda.constant_defining_value_block_field) -> Some s
|
||||
| Flambda.Const _ -> None)
|
||||
|
|
|
@ -28,7 +28,7 @@ let constant_dependencies (const:Flambda.constant_defining_value) =
|
|||
| Allocated_const _ -> Symbol.Set.empty
|
||||
| Block (_, fields) ->
|
||||
let symbol_fields =
|
||||
Misc.Stdlib.List.filter_map (function
|
||||
List.filter_map (function
|
||||
| (Symbol s : Flambda.constant_defining_value_block_field) ->
|
||||
Some s
|
||||
| Flambda.Const _ -> None)
|
||||
|
|
|
@ -97,7 +97,7 @@ let alert_attr x =
|
|||
| _ -> None
|
||||
|
||||
let alert_attrs l =
|
||||
Misc.Stdlib.List.filter_map alert_attr l
|
||||
List.filter_map alert_attr l
|
||||
|
||||
let alerts_of_attrs l =
|
||||
List.fold_left
|
||||
|
|
|
@ -425,7 +425,7 @@ let highlight_quote ppf
|
|||
highlight_tag
|
||||
locs
|
||||
=
|
||||
let iset = ISet.of_intervals @@ Misc.Stdlib.List.filter_map (fun loc ->
|
||||
let iset = ISet.of_intervals @@ List.filter_map (fun loc ->
|
||||
let s, e = loc.loc_start, loc.loc_end in
|
||||
if s.pos_cnum = -1 || e.pos_cnum = -1 then None
|
||||
else Some ((s, s.pos_cnum), (e, e.pos_cnum - 1))
|
||||
|
|
|
@ -236,6 +236,16 @@ let find_all p =
|
|||
|
||||
let filter = find_all
|
||||
|
||||
let filter_map f =
|
||||
let rec aux accu = function
|
||||
| [] -> rev accu
|
||||
| x :: l ->
|
||||
match f x with
|
||||
| None -> aux accu l
|
||||
| Some v -> aux (v :: accu) l
|
||||
in
|
||||
aux []
|
||||
|
||||
let partition p l =
|
||||
let rec part yes no = function
|
||||
| [] -> (rev yes, rev no)
|
||||
|
|
|
@ -134,6 +134,13 @@ val rev_map : ('a -> 'b) -> 'a list -> 'b list
|
|||
{!List.rev}[ (]{!List.map}[ f l)], but is tail-recursive and
|
||||
more efficient. *)
|
||||
|
||||
val filter_map : ('a -> 'b option) -> 'a list -> 'b list
|
||||
(** [filter_map f l] applies [f] to every element of [l], filters
|
||||
out the [None] elements and returns the list of the arguments of
|
||||
the [Some] elements.
|
||||
@since 4.08.0
|
||||
*)
|
||||
|
||||
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
|
||||
(** [List.fold_left f a [b1; ...; bn]] is
|
||||
[f (... (f (f a b1) b2) ...) bn]. *)
|
||||
|
|
|
@ -136,6 +136,13 @@ val rev_map : f:('a -> 'b) -> 'a list -> 'b list
|
|||
{!List.rev}[ (]{!List.map}[ f l)], but is tail-recursive and
|
||||
more efficient. *)
|
||||
|
||||
val filter_map : f:('a -> 'b option) -> 'a list -> 'b list
|
||||
(** [filter_map f l] applies [f] to every element of [l], filters
|
||||
out the [None] elements and returns the list of the arguments of
|
||||
the [Some] elements.
|
||||
@since 4.08.0
|
||||
*)
|
||||
|
||||
val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b list -> 'a
|
||||
(** [List.fold_left f a [b1; ...; bn]] is
|
||||
[f (... (f (f a b1) b2) ...) bn]. *)
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
(* TEST
|
||||
*)
|
||||
|
||||
let string_of_even_opt x =
|
||||
if x mod 2 = 0 then
|
||||
Some (string_of_int x)
|
||||
else
|
||||
None
|
||||
|
||||
(* Standard test case *)
|
||||
let () =
|
||||
let l = List.init 10 (fun x -> x) in
|
||||
|
@ -35,6 +41,7 @@ let () =
|
|||
assert (List.compare_length_with [1] 0 > 0);
|
||||
assert (List.compare_length_with ['1'] 1 = 0);
|
||||
assert (List.compare_length_with ['1'] 2 < 0);
|
||||
assert (List.filter_map string_of_even_opt l = ["0";"2";"4";"6";"8"]);
|
||||
()
|
||||
;;
|
||||
|
||||
|
|
|
@ -112,17 +112,6 @@ module Stdlib = struct
|
|||
| (hd1 :: tl1, hd2 :: tl2) -> eq hd1 hd2 && equal eq tl1 tl2
|
||||
| (_, _) -> false
|
||||
|
||||
let filter_map f l =
|
||||
let rec aux acc l =
|
||||
match l with
|
||||
| [] -> List.rev acc
|
||||
| h :: t ->
|
||||
match f h with
|
||||
| None -> aux acc t
|
||||
| Some v -> aux (v :: acc) t
|
||||
in
|
||||
aux [] l
|
||||
|
||||
let rec find_map f = function
|
||||
| x :: xs ->
|
||||
begin match f x with
|
||||
|
|
|
@ -98,11 +98,6 @@ module Stdlib : sig
|
|||
(** Returns [true] iff the given lists have the same length and content
|
||||
with respect to the given equality function. *)
|
||||
|
||||
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
||||
(** [filter_map f l] applies [f] to every element of [l], filters
|
||||
out the [None] elements and returns the list of the arguments of
|
||||
the [Some] elements. *)
|
||||
|
||||
val find_map : ('a -> 'b option) -> 'a t -> 'b option
|
||||
(** [find_map f l] returns the first evaluation of [f] that returns [Some],
|
||||
or returns None if there is no such element. *)
|
||||
|
|
Loading…
Reference in New Issue