]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Java/Collection.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Java / Collection.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/Java/Collection.scl b/bundles/org.simantics.scl.runtime/scl/Java/Collection.scl
new file mode 100644 (file)
index 0000000..c7a1e2f
--- /dev/null
@@ -0,0 +1,45 @@
+import "JavaBuiltin" as Java
+import "Java/Iterator" as Iterator
+
+importJava "java.util.Collection" where
+    data Collection a
+    
+    contains :: Collection a -> a -> Boolean
+    isEmpty :: Collection a -> Boolean
+    size :: Collection a -> Integer
+    @private
+    iterator :: Collection a -> <Proc> Iterator.Iterator a
+
+@inline
+iter :: (a -> <e> dummy) -> Collection a -> <e> ()
+iter f c = runProc (Iterator.iter f (iterator c))
+
+@inline
+any :: (a -> <e> Boolean) -> Collection a -> <e> Boolean
+any f c = runProc (Iterator.any f (iterator c))
+
+@inline
+all :: (a -> <e> Boolean) -> Collection a -> <e> Boolean
+all f c = runProc (Iterator.all f (iterator c))
+
+@inline
+foldl :: (a -> b -> <e> a) -> a -> Collection b -> <e> a
+foldl f i c = runProc (Iterator.foldl f i (iterator c))
+
+@inline
+foldl1 :: (a -> a -> <e> a) -> Collection a -> <e> a
+foldl1 f c = runProc (Iterator.foldl1 f (iterator c))
+
+@inline
+mapFirst :: (a -> <e> Maybe b) -> Collection a -> <e> Maybe b
+mapFirst f c = runProc (Iterator.mapFirst f (iterator c))
+
+@inline
+find :: (a -> <e> Boolean) -> Collection a -> <e> Maybe a
+find f c = runProc (Iterator.find f (iterator c))
+
+@inline
+uniqueElement :: Collection a -> a
+uniqueElement c = if Java.icmpeq (size c) 1
+                  then runProc (Iterator.next (iterator c))
+                  else fail "Collection is not a singleton."
\ No newline at end of file