From f5505297de4dbf630222b2a5630af249843b855e Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Jul 2021 14:40:15 -0400 Subject: [PATCH] add `CCHash.map` --- src/core/CCHash.ml | 1 + src/core/CCHash.mli | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/core/CCHash.ml b/src/core/CCHash.ml index 45cc96215..181bf2c1e 100644 --- a/src/core/CCHash.ml +++ b/src/core/CCHash.ml @@ -124,6 +124,7 @@ let array f l = Array.fold_left (combine f) 0x42 l let pair f g (x,y) = combine2 (f x) (g y) let triple f g h (x,y,z) = combine2 (combine2 (f x) (g y)) (h z) let quad f g h i (x,y,z,w) = combine2 (combine2 (f x) (g y)) (combine2 (h z) (i w)) +let map f h x = h (f x) let if_ b then_ else_ h = if b then then_ h else else_ h diff --git a/src/core/CCHash.mli b/src/core/CCHash.mli index e3f7a6ca3..65e800d41 100644 --- a/src/core/CCHash.mli +++ b/src/core/CCHash.mli @@ -50,6 +50,19 @@ val pair : 'a t -> 'b t -> ('a * 'b) t val triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t val quad : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t +val map : ('a -> 'b) -> 'b t -> 'a t +(** [map f h] is the hasher that takes [x], + and uses [h] to hash [f x]. + + For example: + {[ + module Str_set = Set.Make(String) + + let hash_str_set : Str_set.t CCHash.t = CCHash.(map Str_set.to_seq @@ seq string) + ]} + + @since NEXT_RELEASE *) + val if_ : bool -> 'a t -> 'a t -> 'a t (** Decide which hash function to use depending on the boolean. *)