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=c175f95a30d01bd7a15c0c6e9d8d4fd1dc36ef1d;hp=2be6ed745e7a9edf90c5519d99291763c007b33c;hb=e1b1c30f512d3c196c86c4a1f0eaf8b7dd461a9a;hpb=2a293ea594bdaa652bbd0d8271f56ead806c4cbd diff --git a/bundles/org.simantics.scl.runtime/scl/Prelude.scl b/bundles/org.simantics.scl.runtime/scl/Prelude.scl index 2be6ed745..c175f95a3 100644 --- a/bundles/org.simantics.scl.runtime/scl/Prelude.scl +++ b/bundles/org.simantics.scl.runtime/scl/Prelude.scl @@ -963,6 +963,9 @@ A class of monads with zero element satisfying """ class (Monad m) => MonadZero m where mzero :: m a + mfilter :: (a -> Boolean) -> m a -> m a + + mfilter p m = m >>= (\x -> if p x then return x else mzero) "Injects a boolean test to a type beloning to `MonadZero`." guard :: MonadZero m => Boolean -> m () @@ -1105,7 +1108,21 @@ instance MonadE (Either a) where instance MonadE [] where bindE l f = concatMap f l + +/// MZeroE /// + +class (MonadE m, MonadZero m) => MonadZeroE m where + filter :: (a -> Boolean) -> m a -> m a + filter p m = m `bindE` (\x -> if p x then return x else mzero) + +instance MonadZeroE [] where + filter = filterList + +instance MonadZeroE Maybe where + filter p (Just x) | not (p x) = Nothing + filter _ m = m + /// Category /// "Identity function." @@ -1166,10 +1183,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 @@ -1288,8 +1307,13 @@ 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`." +@inline +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 @@ -1834,10 +1858,10 @@ foldr1 f l = loop (l!(len-1)) (len-2) `filter pred lst` returns those elements of `lst` that the predicate `pred` accepts. For example filter (> 3) [1, 2, 3, 4, 5, 6] = [4, 5, 6] -""" +""" @inline -filter :: (a -> Boolean) -> [a] -> [a] -filter p l = build (\empty cons -> foldl (\cur x -> if p x then cons cur x else cur) empty l) +filterList :: (a -> Boolean) -> [a] -> [a] +filterList p l = build (\empty cons -> foldl (\cur x -> if p x then cons cur x else cur) empty l) """ Takes those elements of the input list that match `(Just x)` and adds the contents to the resulting list. For example, @@ -1965,6 +1989,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. @@ -1992,7 +2021,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] @@ -2053,6 +2082,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]] @@ -2288,16 +2318,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