]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Iterator.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Iterator.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/Iterator.scl b/bundles/org.simantics.scl.runtime/scl/Iterator.scl
new file mode 100644 (file)
index 0000000..be7fb90
--- /dev/null
@@ -0,0 +1,53 @@
+importJava "java.util.Iterator" where
+    data T a
+    
+    hasNext :: T a -> <Proc> Boolean
+    next :: T a -> <Proc> a
+    remove :: T a -> <Proc> ()
+
+@inline
+iter :: (a -> <e> b) -> T a -> <Proc,e> ()
+iter f it = loop ()
+  where
+    loop _ = 
+        if hasNext it
+        then do
+            f (next it)
+            loop ()
+        else ()
+
+@inline
+iterB :: (a -> <e> Boolean) -> T a -> <Proc,e> Boolean
+iterB 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) -> T 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
+fold :: (a -> b -> <e> a) -> a -> T b -> <Proc,e> a
+fold f init it = loop init
+  where
+    loop cur =
+        if hasNext it
+        then loop (f cur (next it))
+        else cur
+
+importJava "java.lang.Iterable" where
+    data Iterable a
+    
+    iterator :: Iterable a -> <Proc> T a