]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Prelude.scl
(refs #7489) Added replicate function to Prelude
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Prelude.scl
index c2307014a51ceec23d4c4e7d059667d0a414a015..83d84ccf2da505c6590b404c23c93a484fb98ad2 100644 (file)
@@ -1065,6 +1065,15 @@ mapEither f list = runProc do
         Right v -> addArrayList r v)
     (Java.unsafeCoerce l, Java.unsafeCoerce r)
 
+"`replicate n v` returns a list of length `n` such that each element is a copy of `v`."
+@inline
+replicate :: Integer -> a -> [a]
+replicate n v = build (\empty cons ->
+    let aux 0 l = l
+        aux i l = aux (i-1) (cons l v)
+    in aux n empty 
+    )
+
 /// FunctorM ///
 
 class (Functor f) => FunctorM f where
@@ -1273,7 +1282,14 @@ The Either type represents values with two possibilities: a value of type `Eithe
 The `Either` type is sometimes used to represent a value which is either correct or an error; by convention, the `Left` constructor
 is used to hold an error value and the `Right` constructor is used to hold a correct value (mnemonic: "right" also means "correct").
 """
-data Either a b = Left a | Right b
+@JavaType "org.simantics.scl.runtime.either.Either"
+data Either a b =
+    @JavaType "org.simantics.scl.runtime.either.Left"
+    @FieldNames [value]
+    Left a
+  | @JavaType "org.simantics.scl.runtime.either.Right"
+    @FieldNames [value]
+    Right b
 
 deriving instance (Ord a, Ord b) => Ord (Either a b)
 deriving instance (Show a, Show b) => Show (Either a b)
@@ -1396,6 +1412,7 @@ instance Show String where
 instance Read String where
     read str = str
     
+@deprecated "Instead of 'splitString text pattern', write 'split pattern text' (note change in the parameter order)." 
 "`splitString text pattern` splits the string into a list of string where the parts are sepratated in the original list by the given pattern."
 splitString :: String -> String -> [String]
 splitString source pattern = arrayToList $ splitString_ source pattern
@@ -1539,6 +1556,24 @@ joinWithSeparator :: Show a => String -> [a] -> String
 joinWithSeparator separator values = runProc ( 
     StringBuilder.toString $ printWithSeparator StringBuilder.new separator values)
 
+
+intercalate :: String -> [String] -> String
+intercalate separator strings = do
+    l = length strings
+    if l == 0
+    then ""
+    else if l == 1
+    then strings!0
+    else runProc do
+        sb = StringBuilder.new
+        sb << strings!0
+        loop i | i == l = ()
+               | otherwise = do
+            sb << separator << strings!i
+            loop (i+1)
+        loop 1
+        StringBuilder.toString sb
+
 instance (Show a) => Show [a] where
     sb <+ l = do 
         len = length l
@@ -2097,7 +2132,7 @@ importJava "org.simantics.scl.runtime.procedure.Procedures" where
     
     "Executes the given expression and catches certain class of exceptions (specified by the catch handler that is given as a second parameter.)"
     @JavaName catch_
-    catch :: VecComp ex => (<e> a) -> (ex -> <e> a) -> <e> a
+    catch :: VecComp ex => (<e,Exception> a) -> (ex -> <e> a) -> <e> a
 
 importJava "java.lang.Throwable" where
     data Throwable