Permalink
Browse files

[feature] list: assoc_custom_equality_opt

Adding a function as assoc_custom_equality, but returning an option and not raising an exception
  • Loading branch information...
1 parent 6f7b0d3 commit c60fe6fdd90682b8b8d819aa2c3ec0f9b42dd0a8 Arthur Milchior committed with Mathieu Barbin Jul 4, 2011
Showing with 13 additions and 0 deletions.
  1. +5 −0 libbase/baseList.ml
  2. +8 −0 libbase/baseList.mli
View
@@ -712,6 +712,11 @@ let rec assoc_custom_equality ~eq x = function
| (k, v) :: rem ->
if eq k x then v else assoc_custom_equality ~eq x rem
+let rec assoc_custom_equality_opt ~eq x = function
+ | [] -> None
+ | (k, v) :: rem ->
+ if eq k x then Some v else assoc_custom_equality_opt ~eq x rem
+
(* ************************************************************************** *)
(** {b Descr}: Transforms a list of triplets into a triplet of lists. *)
View
@@ -383,6 +383,14 @@ val filterbounds : 'a option * int -> ('b -> 'a) -> 'b list -> 'b list
val assoc_custom_equality: eq:('a -> 'b -> bool) -> 'b -> ('a * 'c) list -> 'c
(* ************************************************************************** *)
+(** {b Descr}: Returns the value (second component) associated with the key
+ (first component) equal to [x] in a list of pairs if it exists, else None.
+ Equality test is performed with the provided function [eq] instead of the
+ general [=] function. *)
+(* ************************************************************************** *)
+val assoc_custom_equality_opt: eq:('a -> 'b -> bool) -> 'b -> ('a * 'c) list -> 'c option
+
+(* ************************************************************************** *)
(** {b Descr}: Transforms a list of triplets into a triplet of lists. *)
(* ************************************************************************** *)
val split3: ('a * 'b * 'c) list -> ('a list * 'b list * 'c list)

0 comments on commit c60fe6f

Please sign in to comment.