From 19ab478d9c672c3d36035bb86f9407615f4bd28a Mon Sep 17 00:00:00 2001 From: craff Date: Sun, 19 Dec 2021 22:30:18 -1000 Subject: [PATCH] filter_map is only available in recent Stdlib --- src/Tiny_httpd.ml | 2 +- src/Tiny_httpd_util.ml | 12 ++++++++++++ src/Tiny_httpd_util.mli | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index 7fa86283..89c99233 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -396,7 +396,7 @@ module Headers = struct | _ -> None in let cookies = - List.filter_map fn_eq (String.split_on_char ';' v) @ cookies + U.filter_map fn_eq (String.split_on_char ';' v) @ cookies in (headers, cookies) end diff --git a/src/Tiny_httpd_util.ml b/src/Tiny_httpd_util.ml index 703bc329..a5a001d9 100644 --- a/src/Tiny_httpd_util.ml +++ b/src/Tiny_httpd_util.ml @@ -102,6 +102,18 @@ let split_on_slash s : _ list = done; List.rev !l +let filter_map f l = + let rec fn = function + | [] -> [] + | x::l -> + begin + match f x with + | None -> fn l + | Some x -> x::fn l + end + in + fn l + (*$= & ~printer:Q.Print.(list string) ["a"; "b"] (split_on_slash "/a/b") ["coucou"; "lol"] (split_on_slash "/coucou/lol") diff --git a/src/Tiny_httpd_util.mli b/src/Tiny_httpd_util.mli index d53ae32e..fffacfd5 100644 --- a/src/Tiny_httpd_util.mli +++ b/src/Tiny_httpd_util.mli @@ -40,3 +40,7 @@ val pp_date : Format.formatter -> Unix.tm -> unit for expiration date of cookies. @since 0.12 *) + +val filter_map : ('a -> 'b option) -> 'a list -> 'b list +(** filter_map, now in Stdlib + @since 0.12 *)