elm/libraries/Set.elm

91 lines
2.5 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-09-10 06:07:49 +00:00
{-| A set of unique values. The values can be any comparable type. This
includes `Int`, `Float`, `Time`, `Char`, `String`, and tuples or lists
of comparable types.
2013-09-10 02:12:27 +00:00
Insert, remove, and query operations all take *O(log n)* time.
# Build
@docs empty, singleton, insert, remove
# Query
@docs member
# Combine
@docs union, intersect, diff
# Lists
@docs toList, fromList
# Transform
@docs map, foldl, foldr
-}
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 ()
2013-09-10 02:12:27 +00:00
{-| Create an empty set. -}
2013-07-26 17:05:48 +00:00
empty : Set comparable
empty = Dict.empty
2013-09-10 02:12:27 +00:00
{-| Create a set with one value. -}
2013-07-26 17:05:48 +00:00
singleton : comparable -> Set comparable
singleton k = Dict.singleton k ()
2013-09-10 02:12:27 +00:00
{-| 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 ()
2013-09-10 02:12:27 +00:00
{-| 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
2013-09-10 02:12:27 +00:00
{-| Determine if a value is in a set. -}
2013-07-26 17:05:48 +00:00
member : comparable -> Set comparable -> Bool
member = Dict.member
2013-09-10 02:12:27 +00:00
{-| 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
2013-09-10 02:12:27 +00:00
{-| 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
2013-09-10 02:12:27 +00:00
{-| 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
2013-09-10 02:12:27 +00:00
{-| Convert a set into a list. -}
2013-07-26 17:05:48 +00:00
toList : Set comparable -> [comparable]
toList = Dict.keys
2013-09-10 02:12:27 +00:00
{-| 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
2013-09-10 02:12:27 +00:00
{-| 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
2013-09-10 02:12:27 +00:00
{-| 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
2013-09-10 02:12:27 +00:00
{-| 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))