Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* commons/common.ml: find_some

  • Loading branch information...
commit aa178e261abd17f55d1cd6643f72d786a9c0a2e5 1 parent 0b06e07
@aryx aryx authored
Showing with 11 additions and 8 deletions.
  1. +9 −7 commons/common.ml
  2. +2 −1  commons/common.mli
View
16 commons/common.ml
@@ -690,13 +690,6 @@ let rec filter_some = function
let map_filter f xs = xs +> List.map f +> filter_some
-let rec find_some p = function
- | [] -> raise Not_found
- | x :: l ->
- match p x with
- | Some v -> v
- | None -> find_some p l
-
let rec find_some_opt p = function
| [] -> None
| x :: l ->
@@ -704,6 +697,15 @@ let rec find_some_opt p = function
| Some v -> Some v
| None -> find_some_opt p l
+let find_some p xs =
+ match find_some_opt p xs with
+ | None -> raise Not_found
+ | Some x -> x
+
+let rec find_opt f xs =
+ find_some_opt (fun x -> if f x then Some x else None) xs
+
+
(*****************************************************************************)
(* Regexp, can also use PCRE *)
(*****************************************************************************)
View
3  commons/common.mli
@@ -69,8 +69,8 @@ val null : 'a list -> bool
val exclude : ('a -> bool) -> 'a list -> 'a list
val sort : 'a list -> 'a list
-val (>>=): 'a option -> ('a -> 'b option) -> 'b option
val map_filter : ('a -> 'b option) -> 'a list -> 'b list
+val find_opt: ('a -> bool) -> 'a list -> 'a option
val find_some : ('a -> 'b option) -> 'a list -> 'b
val find_some_opt : ('a -> 'b option) -> 'a list -> 'b option
val filter_some: 'a option list -> 'a list
@@ -108,6 +108,7 @@ val hashset_to_list : 'a hashset -> 'a list
val map_opt: ('a -> 'b) -> 'a option -> 'b option
val opt: ('a -> unit) -> 'a option -> unit
val do_option : ('a -> unit) -> 'a option -> unit
+val (>>=): 'a option -> ('a -> 'b option) -> 'b option
val (|||): 'a option -> 'a -> 'a
Please sign in to comment.
Something went wrong with that request. Please try again.