From: Hannu Niemistö Date: Mon, 25 Sep 2017 09:41:03 +0000 (+0300) Subject: Added MMap.getOrCreate X-Git-Tag: v1.31.0~171 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=8cab2461c7f244ac8fabf2c7e61f0753dab68d00 Added MMap.getOrCreate refs #7503 Change-Id: Ibc03b5c51365e4d5c2d2250fbf7265adc298a84a --- 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)