]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Java/Iterator.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Java / Iterator.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/Java/Iterator.scl b/bundles/org.simantics.scl.runtime/scl/Java/Iterator.scl
new file mode 100644 (file)
index 0000000..0e5476c
--- /dev/null
@@ -0,0 +1,75 @@
+
+importJava "java.util.Iterator" where
+    data Iterator a
+    
+    hasNext :: Iterator a -> <Proc> Boolean
+    next :: Iterator a -> <Proc> a
+
+@inline
+iter :: (a -> <e> dummy) -> Iterator a -> <Proc,e> ()
+iter f it = loop ()
+  where
+    loop _ = 
+        if hasNext it
+        then do
+            f (next it)
+            loop ()
+        else ()
+
+@inline
+find :: (a -> <e> Boolean) -> Iterator a -> <Proc,e> Maybe a
+find f it = loop ()
+  where
+    loop _ = 
+        if hasNext it 
+        then let el = next it
+             in if f el
+                then Just el
+                else loop ()
+        else Nothing
+
+@inline
+any :: (a -> <e> Boolean) -> Iterator a -> <Proc,e> Boolean
+any f it = loop ()
+  where
+    loop _ = 
+        if hasNext it 
+        then if f (next it)
+             then True
+             else loop ()
+        else False
+
+@inline
+all :: (a -> <e> Boolean) -> Iterator a -> <Proc,e> Boolean
+all f it = loop ()
+  where
+    loop _ = 
+        if hasNext it 
+        then if f (next it)
+             then loop ()
+             else False
+        else True
+
+@inline
+mapFirst :: (a -> <e> Maybe b) -> Iterator a -> <Proc,e> Maybe b
+mapFirst f it = loop ()
+  where
+    loop _ = 
+        if hasNext it 
+        then match f (next it) with
+                 Nothing -> loop ()
+                 r -> r
+        else Nothing
+
+@inline
+foldl :: (a -> b -> <e> a) -> a -> Iterator b -> <Proc,e> a
+foldl f init it = loop init
+  where
+    loop cur =
+        if hasNext it
+        then loop (f cur (next it))
+        else cur
+
+@inline
+foldl1 :: (a -> a -> <e> a) -> Iterator a -> <Proc,e> a
+foldl1 f it = foldl f (next it) it
\ No newline at end of file