From 8cab2461c7f244ac8fabf2c7e61f0753dab68d00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Mon, 25 Sep 2017 12:41:03 +0300 Subject: [PATCH] Added MMap.getOrCreate refs #7503 Change-Id: Ibc03b5c51365e4d5c2d2250fbf7265adc298a84a --- bundles/org.simantics.scl.runtime/scl/MMap.scl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bundles/org.simantics.scl.runtime/scl/MMap.scl b/bundles/org.simantics.scl.runtime/scl/MMap.scl index 18e5866c0..085728c58 100644 --- a/bundles/org.simantics.scl.runtime/scl/MMap.scl +++ b/bundles/org.simantics.scl.runtime/scl/MMap.scl @@ -35,6 +35,15 @@ importJava "java.util.Map" where remove :: T a b -> a -> (Maybe b) clear :: T a b -> () +@inline +getOrCreate :: T a b -> (a -> b) -> a -> b +getOrCreate m f k = match get m k with + Just v -> v + Nothing -> do + v = f k + put m k v + v + @inline iter :: (a -> b -> ()) -> T a b -> () iter f m = MSet.iter (\entry -> f (keyOf entry) (valueOf entry)) (entrySet m) -- 2.47.1