summaryrefslogtreecommitdiff
path: root/src/runtime/haskell/PGF/Utilities.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/runtime/haskell/PGF/Utilities.hs')
-rw-r--r--src/runtime/haskell/PGF/Utilities.hs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/runtime/haskell/PGF/Utilities.hs b/src/runtime/haskell/PGF/Utilities.hs
new file mode 100644
index 000000000..5af5b9b5d
--- /dev/null
+++ b/src/runtime/haskell/PGF/Utilities.hs
@@ -0,0 +1,20 @@
+-- | Basic utilities
+module PGF.Utilities where
+import Data.Set(empty,member,insert)
+
+
+-- | Like 'nub', but O(n log n) instead of O(n^2), since it uses a set to lookup previous things.
+-- The result list is stable (the elements are returned in the order they occur), and lazy.
+-- Requires that the list elements can be compared by Ord.
+-- Code ruthlessly taken from http://hpaste.org/54411
+nub' :: Ord a => [a] -> [a]
+nub' = loop empty
+ where loop _ [] = []
+ loop seen (x : xs)
+ | member x seen = loop seen xs
+ | otherwise = x : loop (insert x seen) xs
+
+
+-- | Replace all occurences of an element by another element.
+replace :: Eq a => a -> a -> [a] -> [a]
+replace x y = map (\z -> if z == x then y else z)