From 3b1471eac606d2b2b1ebd927d7fdba29c8886ce3 Mon Sep 17 00:00:00 2001 From: Andrei Borzenkov Date: Wed, 9 Oct 2024 16:26:50 +0400 Subject: [PATCH] Implement mapFilter using foldLeft instad of iter --- src/PersistentOrderedMap.mo | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) 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 {