X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fscl%2FPrelude.scl;h=03eabb66fb8ecd2ff7218fa5191db648350ee280;hp=bb6452429b39141a10a45ae19dbf4ad0736d0745;hb=77e4291d372e7b4b40ee868ad61cc07c51de8c6b;hpb=8568b9f55095fcab15eb28d16bcf67b64844eca3 diff --git a/bundles/org.simantics.scl.runtime/scl/Prelude.scl b/bundles/org.simantics.scl.runtime/scl/Prelude.scl index bb6452429..03eabb66f 100644 --- a/bundles/org.simantics.scl.runtime/scl/Prelude.scl +++ b/bundles/org.simantics.scl.runtime/scl/Prelude.scl @@ -38,11 +38,16 @@ class Serializable a binding :: Serializable a => Binding a ***********************************************************/ +type BooleanArray = Vector Boolean +type ByteArray = Vector Byte +type CharacterArray = Vector Character +type ShortArray = Vector Short +type IntegerArray = Vector Integer +type LongArray = Vector Long +type FloatArray = Vector Float +type DoubleArray = Vector Double + importJava "java.util.Arrays" where - @private - @JavaName toString - showDoubleArray :: DoubleArray -> String - "Converts an array to a list." @JavaName asList arrayToList :: Array a -> [a] @@ -52,9 +57,6 @@ importJava "java.util.List" where @JavaName toArray listToArray :: [a] -> Array a -instance Show DoubleArray where - show = showDoubleArray - importJava "org.simantics.scl.runtime.Coercion" where "Converts a list of doubles to a double array." toDoubleArray :: [Double] -> DoubleArray @@ -1164,10 +1166,12 @@ class IndexedSequence f where "Returns the first element of a sequence" @inline +first :: [a] -> a first l = l!0 "Returns the last element of a sequence" @inline +last :: [a] -> a last l = l!(length l-1) instance IndexedSequence [] where @@ -1286,8 +1290,12 @@ fromMaybe :: a -> Maybe a -> a fromMaybe default maybeValue = match maybeValue with Just v -> v _ -> default - - + +"`maybe def f v` returns `def` if `v=Nothing` and `f x` if `v=Just x`." +maybe :: b -> (a -> b) -> Maybe a -> b +maybe n _ Nothing = n +maybe _ f (Just x) = f x + """ Provides a default value if the first parameter is Nothing. The default value is evaluated only if needed. The function @@ -1303,6 +1311,11 @@ orElse :: Maybe a -> ( a) -> a orElse (Just x) _ = x orElse Nothing def = def +@inline +orElseM :: Maybe a -> ( Maybe a) -> Maybe a +orElseM mx@(Just x) _ = mx +orElseM Nothing def = def + /// Either /// """ @@ -1958,6 +1971,11 @@ importJava "org.simantics.scl.runtime.Lists" where """ index :: [(a,b)] -> a -> Maybe b + """ + Given a list of elements, the function produces its characteristic function. + """ + indexSet :: [a] -> a -> Boolean + """ Given a list of values and a function computing a key for each value, the function produces a function that finds a value effeciently for the given key. @@ -1985,7 +2003,7 @@ importJava "org.simantics.scl.runtime.Lists" where unique :: [a] -> [a] "Like `unique`, but uses the given function for finding the key values used for uniqueness testing." - uniqueBy :: (a -> b) -> [a] -> [a] + uniqueBy :: (a -> b) -> [a] -> [a] "Works like `unique` but uses the given function for equality tests." uniqueWith :: (a -> a -> Boolean) -> [a] -> [a] @@ -2046,6 +2064,7 @@ Transposes the rows and columns of its argument. For example, transpose [[1,2,3],[4,5,6]] == [[1,4],[2,5],[3,6]] transpose [[1,2],[3,4,5]] == [[1,3],[2,4],[5]] """ +transpose :: [[a]] -> [[a]] transpose xss = [[xs!i | xs <- xss, i < length xs] | i <- [0..maximum [length xs | xs <- xss]-1]] @@ -2281,16 +2300,6 @@ instance Show TypeRep where isSpecialType (TApply a _) = isSpecialType a */ -// ByteArray - -importJava "java.util.Arrays" where - @private - @JavaName toString - byteArrayToString :: ByteArray -> String - -instance Show ByteArray where - show = byteArrayToString - // Type @private