]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/MMap.scl
Added MMap.getOrCreate
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / MMap.scl
index 18e5866c080c05d35a6598c16e5382b63baa1a3f..085728c5830d14db6f991dca5a0c4b7a623d69a4 100644 (file)
@@ -35,6 +35,15 @@ importJava "java.util.Map" where
     remove :: T a b -> a -> <Proc> (Maybe b)
     clear :: T a b -> <Proc> ()
 
+@inline
+getOrCreate :: T a b -> (a -> <e> b) ->  a -> <Proc,e> 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 -> <e> ()) -> T a b -> <e,Proc> ()
 iter f m = MSet.iter (\entry -> f (keyOf entry) (valueOf entry)) (entrySet m)