diff --git a/src/PersistentOrderedMap.mo b/src/PersistentOrderedMap.mo index f93e4a61..1fa00545 100644 --- a/src/PersistentOrderedMap.mo +++ b/src/PersistentOrderedMap.mo @@ -602,19 +602,15 @@ module { }; public func mapFilter(t : Map, compare : (K, K) -> O.Order, f : (K, V1) -> ?V2) : Map{ - var map = #leaf : Map; - for(kv in iter(t, #fwd)) - { - switch(f kv){ - case null {}; - case (?v1) { - // The keys still are monotonic, so we can - // merge trees using `append` and avoid compare here - map := put(map, compare, kv.0, v1); + func combine(key : K, value1 : V1, acc : Map) : Map { + switch (f(key, value1)){ + case null { acc }; + case (?value2) { + put(acc, compare, key, value2) } } }; - map + foldLeft(t, #leaf, combine) }; public func get(t : Map, compare : (K, K) -> O.Order, x : K) : ?V {