From 5efab5d6da2be1e4f44fc24227a8f1a98cd272c6 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 | 11 +++++++++++ rescript/src/TableclothMap.mli | 16 ++++++++++++++-- 4 files changed, 43 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..3d89e657 100644 --- a/rescript/src/TableclothMap.ml +++ b/rescript/src/TableclothMap.ml @@ -57,6 +57,17 @@ let map_with_index = mapWithIndex let filter m ~f = Belt.Map.keep m (fun _ value -> f value) +let filterMap m ~f = + let items = Belt.Map.toArray m in + let f' (key, value) = + let result = f ~key ~value in + Belt.Option.map result (fun value -> (key, value)) + in + Belt.Array.keepMap items f' + |> Belt.Map.(fromArray ~id:(getId m)) + +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..637b90e4 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,18 @@ 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)