]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Prelude.scl
Improvement of index and group implementations and indexGroup(By)
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Prelude.scl
index 907d6e00d74084dfc12dddedd205448624ecc2a8..f06acb29fce55e74e98c111364ede07908947df9 100644 (file)
@@ -1930,14 +1930,51 @@ importJava "org.simantics.scl.runtime.Lists" where
     
     "Sorts the list using the given comparator."
     sortWith :: (a -> a -> <e> Integer) -> [a] -> <e> [a]
+    
+    """
+    Given a list of key-value pairs, the function produces a function that finds a value
+    efficiently for the given key.
+    """
+    index :: [(a,b)] -> a -> Maybe b
+    
+    """
+    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.
+    """
+    indexBy ::  (a -> b) -> [a] -> b -> Maybe a
+    
     "Works like `index` but uses the given functions as hash codes and equality."
     indexWith :: (a -> Integer) -> (a -> a -> Boolean) -> [(a,b)] -> a -> Maybe b
+    
+    "Groups a list values by a key computed by the given function."
+    groupBy :: (a -> <e> b) -> [a] -> <e> [(b, [a])]
+    
+    "Groups a list of key-value pairs by the keys."
+    group :: [(a,b)] -> [(a, [b])]
+
+    "Composition of index and groupBy."
+    indexGroupBy :: (a -> <e> b) -> [a] -> <e> (b -> [a])
+    
+    "Composition of index and group."
+    indexGroup :: [(a,b)] -> a -> [b]
+    
     groupWith :: (b -> Integer) -> (b -> b -> Boolean) -> (a -> <e> b) -> (a -> <e> c) -> [a] -> <e> [(b, [c])]
+    
+    "Removes duplicates (all but the first occurrence) from the list but otherwise preserves the order of the elements."
+    unique :: [a] -> [a]
+    
+    "Like `unique`, but uses the given function for finding the key values used for uniqueness testing."
+    uniqueBy :: (a -> b) -> [a] -> [a]
+
     "Works like `unique` but uses the given function for equality tests."
     uniqueWith :: (a -> a -> Boolean) -> [a] -> [a]
+    
     "Works like `\\\\` but uses the given function for equality tests."
     deleteAllBy :: (a -> a -> Boolean) -> [a] -> [a] -> [a]
     
+    @private
+    listDifference :: [a] -> [a] -> [a]
+    
     //range :: Integer -> Integer -> [Integer]
     
     //build :: (forall a. a -> (a -> b -> <e> a) -> <e> a) -> <e> [b]
@@ -2062,42 +2099,9 @@ sortBy f l = sortWith (\x y -> compare (f x) (f y)) l
 // This is faster if f is slow, but will generate more auxiliary structures
 //sortBy f l = map snd (sortWith (\(x,_) (y,_) -> compare x y) [(f x, x) | x <- l])
 
-"""
-Given a list of key-value pairs, the function produces a function that finds a value
-efficiently for the given key.
-"""
-index :: [(a,b)] -> a -> Maybe b
-index = indexWith hashCode (==)
-
-"""
-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.
-"""
-indexBy ::  (a -> b) -> [a] -> b -> Maybe a
-indexBy f l = index [(f x, x) | x <- l]
-
-"Groups a list values by a key computed by the given function."
-groupBy :: (a -> <e> b) -> [a] -> <e> [(b, [a])]
-groupBy f l = groupWith hashCode (==) f id l
-
-"Groups a list of key-value pairs by the keys."
-group :: [(a,b)] -> [(a, [b])]
-group = groupWith hashCode (==) fst snd
-
-"Removes duplicates (all but the first occurrence) from the list but otherwise preserves the order of the elements."
-unique ::  [a] -> [a]
-unique = uniqueWith (==)
-
-"Like `unique`, but uses the given function for finding the key values used for uniqueness testing."
-uniqueBy :: (a -> b) -> [a] -> [a]
-uniqueBy f = uniqueWith (\a b -> f a == f b)
-
-//sortAndUniqueBy :: Ord b => (a -> b) -> [a] -> [a]
-//sortAndUniqueBy f = map snd . uniqueWith (\a b -> fst a == fst b) . sortBy fst . map (\x -> (f x, x))
-
 "`a \\\\ b` removes all elements of `b` from the list `a`."
 (\\) :: [a] -> [a] -> [a]
-(\\) = deleteAllBy (==)
+(\\) = listDifference
 
 /// Dynamic ///