elm/libraries/Set.elm

67 lines
2 KiB
Elm
Raw Normal View History

2012-10-10 21:37:42 +00:00
module Set (empty,singleton,insert,remove
,member
,foldl,foldr,map
,union,intersect,diff
,toList,fromList
) where
2012-10-10 21:37:42 +00:00
2013-07-26 17:05:48 +00:00
import Maybe (Maybe)
import Dict as Dict
import List as List
2013-07-26 17:05:48 +00:00
type Set t = Dict.Dict t ()
-- Create an empty set.
2013-07-26 17:05:48 +00:00
empty : Set comparable
empty = Dict.empty
-- Create a set with one value.
2013-07-26 17:05:48 +00:00
singleton : comparable -> Set comparable
singleton k = Dict.singleton k ()
-- Insert a value into a set.
2013-07-26 17:05:48 +00:00
insert : comparable -> Set comparable -> Set comparable
insert k = Dict.insert k ()
-- Remove a value from a set. If the value is not found, no changes are made.
2013-07-26 17:05:48 +00:00
remove : comparable -> Set comparable -> Set comparable
remove = Dict.remove
-- Determine if a value is in a set.
2013-07-26 17:05:48 +00:00
member : comparable -> Set comparable -> Bool
member = Dict.member
-- Get the union of two sets. Keep all values.
2013-07-26 17:05:48 +00:00
union : Set comparable -> Set comparable -> Set comparable
union = Dict.union
-- Get the intersection of two sets. Keeps values that appear in both sets.
2013-07-26 17:05:48 +00:00
intersect : Set comparable -> Set comparable -> Set comparable
intersect = Dict.intersect
-- Get the difference between the first set and the second. Keeps values
-- that do not appear in the second set.
2013-07-26 17:05:48 +00:00
diff : Set comparable -> Set comparable -> Set comparable
2013-05-06 09:30:50 +00:00
diff = Dict.diff
-- Convert a set into a list.
2013-07-26 17:05:48 +00:00
toList : Set comparable -> [comparable]
toList = Dict.keys
-- Convert a list into a set, removing any duplicates.
2013-07-26 17:05:48 +00:00
fromList : [comparable] -> Set comparable
fromList xs = List.foldl insert empty xs
-- Fold over the values in a set, in order from lowest to highest.
2013-07-26 17:05:48 +00:00
foldl : (comparable -> b -> b) -> b -> Set comparable -> b
foldl f b s = Dict.foldl (\k _ b -> f k b) b s
-- Fold over the values in a set, in order from highest to lowest.
2013-07-26 17:05:48 +00:00
foldr : (comparable -> b -> b) -> b -> Set comparable -> b
foldr f b s = Dict.foldr (\k _ b -> f k b) b s
-- Map a function onto a set, creating a new set with no duplicates.
2013-07-26 17:05:48 +00:00
map : (comparable -> comparable') -> Set comparable -> Set comparable'
map f s = fromList (List.map f (toList s))