From 282fa4abbf4d80a50249f361f0e1f31347c114ed Mon Sep 17 00:00:00 2001 From: Pomin Wu Date: Thu, 6 Jan 2022 06:52:55 +0800 Subject: [PATCH] Add Map.filterMap & fixed #151 --- native/src/TableclothMap.ml | 5 +++++ native/src/TableclothMap.mli | 14 ++++++++++++-- rescript/src/TableclothMap.ml | 7 +++++++ rescript/src/TableclothMap.mli | 14 ++++++++++++-- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/native/src/TableclothMap.ml b/native/src/TableclothMap.ml index 271fbd7b..3459f071 100644 --- a/native/src/TableclothMap.ml +++ b/native/src/TableclothMap.ml @@ -94,6 +94,11 @@ let map_with_index = mapWithIndex let filter = Base.Map.filter +let filterMap m ~f = + Base.Map.filter_mapi m ~f:(fun ~key ~data -> f ~key ~value:data) + +let filter_map = filterMap + let partition m ~f = Base.Map.partitioni_tf m ~f:(fun ~key ~data -> f ~key ~value:data) diff --git a/native/src/TableclothMap.mli b/native/src/TableclothMap.mli index 62d08fe8..e643560a 100644 --- a/native/src/TableclothMap.mli +++ b/native/src/TableclothMap.mli @@ -18,12 +18,12 @@ end) end - type animal = + type animal = | Cow | Pig | Alpacca - let pointToAnimal : animal Map.Of(Point).t = + let pointToAnimal : animal Map.Of(Point).t = Map.fromList (module Points) [((0, 0), Alpacca); ((3, 4), Cow); ((6, 7), Sheep)] ]} @@ -409,6 +409,16 @@ val filter : ]} *) +val filterMap : + ('key, 'value, 'id) t + -> f:(key:'key -> value:'value -> 'value option) + -> ('key, 'value, 'id) t + +val filter_map : + ('key, 'value, 'id) t + -> f:(key:'key -> value:'value -> 'value option) + -> ('key, 'value, 'id) t + val partition : ('key, 'value, 'id) t -> f:(key:'key -> value:'value -> bool) diff --git a/rescript/src/TableclothMap.ml b/rescript/src/TableclothMap.ml index ae2f2555..6022eb8a 100644 --- a/rescript/src/TableclothMap.ml +++ b/rescript/src/TableclothMap.ml @@ -57,6 +57,13 @@ let map_with_index = mapWithIndex let filter m ~f = Belt.Map.keep m (fun _ value -> f value) +let filterMap m ~f = + Belt.Map.mapWithKey m (fun key value -> f ~key ~value) + |> fun m -> Belt.Map.keep m (fun _ value -> Belt.Option.isSome value) + |> fun m -> Belt.Map.map m Belt.Option.getExn + +let filter_map = filterMap + let partition m ~f = Belt.Map.partition m (fun key value -> f ~key ~value) let find m ~f = Belt.Map.findFirstBy m (fun key value -> f ~key ~value) diff --git a/rescript/src/TableclothMap.mli b/rescript/src/TableclothMap.mli index c9fb3a83..868d32e6 100644 --- a/rescript/src/TableclothMap.mli +++ b/rescript/src/TableclothMap.mli @@ -18,12 +18,12 @@ end) end - type animal = + type animal = | Cow | Pig | Alpacca - let pointToAnimal : animal Map.Of(Point).t = + let pointToAnimal : animal Map.Of(Point).t = Map.fromList (module Points) [((0, 0), Alpacca); ((3, 4), Cow); ((6, 7), Sheep)] ]} @@ -409,6 +409,16 @@ val filter : ]} *) +val filterMap : + ('key, 'value, 'id) t + -> f:(key:'key -> value:'value -> 'value option) + -> ('key, 'value, 'id) t + +val filter_map : + ('key, 'value, 'id) t + -> f:(key:'key -> value:'value -> 'value option) + -> ('key, 'value, 'id) t + val partition : ('key, 'value, 'id) t -> f:(key:'key -> value:'value -> bool)