]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Prelude.scl
Added Prelude/maybe
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Prelude.scl
index 32582affa867947fbac5167b94c4d8919c0bd91c..03eabb66fb8ecd2ff7218fa5191db648350ee280 100644 (file)
@@ -1166,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
@@ -1288,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 -> <e> b) -> Maybe a -> <e> 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
@@ -1997,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 -> <e> b) -> [a] -> <e> [a]
 
     "Works like `unique` but uses the given function for equality tests."
     uniqueWith :: (a -> a -> Boolean) -> [a] -> [a]
@@ -2058,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]]