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 Apr 9, 2022
1 parent 66b820f commit 7837b49
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions native/src/TableclothMap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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 -> '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)
Expand Down
9 changes: 9 additions & 0 deletions rescript/src/TableclothMap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 -> '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)
Expand Down

0 comments on commit 7837b49

Please sign in to comment.