Permalink
Browse files

* commons/common.ml: find_some

  • Loading branch information...
aryx committed Feb 26, 2014
1 parent 0b06e07 commit aa178e261abd17f55d1cd6643f72d786a9c0a2e5
Showing with 11 additions and 8 deletions.
  1. +9 −7 commons/common.ml
  2. +2 −1 commons/common.mli
View
@@ -690,20 +690,22 @@ 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 ->
match p x with
| 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
@@ -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

0 comments on commit aa178e2

Please sign in to comment.