From 7837b4933ec413d7207cc9aa1ef0eed859c9157f 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 | 6 ++++++ native/src/TableclothMap.mli | 14 ++++++++++++-- rescript/src/TableclothMap.ml | 9 +++++++++ rescript/src/TableclothMap.mli | 14 ++++++++++++-- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/native/src/TableclothMap.ml b/native/src/TableclothMap.ml index 271fbd7b..0f12dcca 100644 --- a/native/src/TableclothMap.ml +++ b/native/src/TableclothMap.ml @@ -94,6 +94,12 @@ let map_with_index = mapWithIndex let filter = Base.Map.filter +let filterMap m ~f = + Base.Map.filter_mapi m ~f:(fun ~key ~data:value -> f ~key ~value) + + +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..78ba74f2 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 -> 'b option) + -> ('key, 'b, 'id) t + +val filter_map : + ('key, 'value, 'id) t + -> f:(key:'key -> value:'value -> 'b option) + -> ('key, 'b, '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..659efe77 100644 --- a/rescript/src/TableclothMap.ml +++ b/rescript/src/TableclothMap.ml @@ -57,6 +57,15 @@ let map_with_index = mapWithIndex let filter m ~f = Belt.Map.keep m (fun _ value -> f value) +let filterMap m ~f = + let id = Belt.Map.getId m in + Belt.Map.( + reduce m (make ~id) (fun m' key value -> + Belt.Map.update m' key (fun _ -> f ~key ~value) )) + + +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..2333dd54 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 -> 'b option) + -> ('key, 'b, 'id) t + +val filter_map : + ('key, 'value, 'id) t + -> f:(key:'key -> value:'value -> 'b option) + -> ('key, 'b, 'id) t + val partition : ('key, 'value, 'id) t -> f:(key:'key -> value:'value -> bool)