Skip to content

Commit

Permalink
Add Map.filterMap & fixed darklang#151
Browse files Browse the repository at this point in the history
  • Loading branch information
pm5 committed Jan 5, 2022
1 parent 126e8f9 commit 282fa4a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions native/src/TableclothMap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 12 additions & 2 deletions native/src/TableclothMap.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
]}
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions rescript/src/TableclothMap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions rescript/src/TableclothMap.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
]}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 282fa4a

Please sign in to comment.