]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/IterN.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / IterN.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/IterN.scl b/bundles/org.simantics.scl.runtime/scl/IterN.scl
new file mode 100644 (file)
index 0000000..b5d69f9
--- /dev/null
@@ -0,0 +1,76 @@
+include "Prelude"
+
+@inline
+iterN :: (Integer -> <e> b) -> Integer -> <e> ()
+iterN f n = loop 0
+  where
+    loop i = if i==n
+             then ()
+             else do { f i ; loop (i+1) }
+
+@inline
+anyN :: (Integer -> <e> Boolean) -> Integer -> <e> Boolean
+anyN f n = loop 0
+  where
+    loop i = if i==n
+             then False
+             else f i || loop (i+1)
+
+@inline
+allN :: (Integer -> <e> Boolean) -> Integer -> <e> Boolean
+allN f n = loop 0
+  where
+    loop i = if i==n
+             then True
+             else f i && loop (i+1)
+
+@inline
+mapFirstN :: (Integer -> <e> Maybe b) -> Integer -> <e> Maybe b
+mapFirstN f n = loop 0
+  where
+    loop i = if i==n
+             then Nothing
+             else match f i with
+                 r @ (Just _) -> r
+                 Nothing -> loop (i+1)
+
+@inline
+concatMapN :: (Integer -> <e> [b]) -> Integer -> <e> [b]
+concatMapN f n = sum (mapN f n)
+
+@inline
+mapN :: (Integer -> <e> b) -> Integer -> <e> [b]
+mapN f n = build (\empty cons -> 
+    let loop i accum = if i==n
+                       then accum
+                       else loop (i+1) (cons accum (f i)) 
+    in  loop 0 empty)
+
+@inline
+filterN :: (Integer -> <e> Boolean) -> Integer -> <e> [Integer]
+filterN f n = build (\empty cons -> 
+    let loop i accum = if i==n
+                       then accum
+                       else loop (i+1) (
+                           if f i 
+                           then cons accum i
+                           else accum) 
+    in  loop 0 empty)
+
+@inline
+mapMaybeN :: (Integer -> <e> Maybe b) -> Integer -> <e> [b]
+mapMaybeN f n = build (\empty cons -> 
+    let loop i accum = if i==n
+                       then accum
+                       else loop (i+1) (match f i with
+                           Just v -> cons accum v
+                           Nothing -> accum) 
+    in  loop 0 empty)
+
+@inline
+foldlN :: (a -> Integer -> <e> a) -> a -> Integer -> <e> a
+foldlN f initial n = loop initial 0
+  where
+    loop cur i = if i==n
+                 then cur
+                 else loop (f cur i) (i+1)