on fbad8ea help sign in
elk/libraries/Set.elm
66 lines 2.1 kB history raw
2module Set (empty,singleton,insert,remove
3 ,member
4 ,foldl,foldr,map
5 ,union,intersect,diff
6 ,toList,fromList
7 ) where
9import Maybe (Maybe)
10import Dict as Dict
11import List as List
13type Set t = Dict.Dict t ()
15-- Create an empty set.
16empty : Set comparable
17empty = Dict.empty
19-- Create a set with one value.
20singleton : comparable -> Set comparable
21singleton k = Dict.singleton k ()
23-- Insert a value into a set.
24insert : comparable -> Set comparable -> Set comparable
25insert k = Dict.insert k ()
27-- Remove a value from a set. If the value is not found, no changes are made.
28remove : comparable -> Set comparable -> Set comparable
29remove = Dict.remove
31-- Determine if a value is in a set.
32member : comparable -> Set comparable -> Bool
33member = Dict.member
35-- Get the union of two sets. Keep all values.
36union : Set comparable -> Set comparable -> Set comparable
37union = Dict.union
39-- Get the intersection of two sets. Keeps values that appear in both sets.
40intersect : Set comparable -> Set comparable -> Set comparable
41intersect = Dict.intersect
43-- Get the difference between the first set and the second. Keeps values
44-- that do not appear in the second set.
45diff : Set comparable -> Set comparable -> Set comparable
46diff = Dict.diff
48-- Convert a set into a list.
49toList : Set comparable -> [comparable]
50toList = Dict.keys
52-- Convert a list into a set, removing any duplicates.
53fromList : [comparable] -> Set comparable
54fromList xs = List.foldl insert empty xs
56-- Fold over the values in a set, in order from lowest to highest.
57foldl : (comparable -> b -> b) -> b -> Set comparable -> b
58foldl f b s = Dict.foldl (\k _ b -> f k b) b s
60-- Fold over the values in a set, in order from highest to lowest.
61foldr : (comparable -> b -> b) -> b -> Set comparable -> b
62foldr f b s = Dict.foldr (\k _ b -> f k b) b s
64-- Map a function onto a set, creating a new set with no duplicates.
65map : (comparable -> comparable') -> Set comparable -> Set comparable'
66map f s = fromList (List.map f (toList s))