Cindiary / elm-not-empty / NotEmpty.Set


type alias Set item =
( item, Set item )

Not empty version of Set

Create

singleton : a -> Set a

Create a non empty set with one value.

withValue : comparable -> Set comparable -> Set comparable

Create a non empty set from a regular set and a given value.

In contrast to fromSet this will always return a value since there is always at least 1 value.

set = withValue 5 ( Set.fromList [ 4, 3 ] )
toSet set == Set.fromList [ 3, 4, 5 ]

fromList : NotEmpty.List.List comparable -> Set comparable

Same as Set.fromList except using a NotEmpty.List

fromListWithValue : comparable -> List comparable -> Set comparable

Same as withValue but with a list instead of a set

Transforming from and to regular sets

fromSet : Set comparable -> Maybe (Set comparable)

Convert a regular set into a not empty set, or Nothing if the set is empty

fromSet ( Set.fromList [] ) == Nothing
fromSet ( Set.fromList [ 1, 2, 3 ] ) == Just ( fromListWithValue 1 [ 2, 3 ] )

toSet : Set comparable -> Set comparable

Convert a not empty set into a regular set

toSet ( fromListWithValue 1 [ 2, 3 ] ) == Set.fromList [ 1, 2, 3 ]
toSet ( singleton 1 ) == Set.singleton 1

New functions

head : Set a -> a

Extract the first value from a not empty set.

head ( fromListWithValue 1 [ 2, 3 ] ) == 1
head ( singleton 1 ) == 1

last : Set a -> a

Extract the last value from a not empty set.

head ( fromListWithValue 1 [ 2, 3 ] ) == 3
head ( singleton 1 ) == 1

tail : Set a -> Set a

Returns the elements after the first value as a regular set

tail ( fromListWithValue 1 [ 2, 3 ] ) == Set.fromList [ 2, 3 ]
tail ( singleton 1 ) == Set.fromList []

hasTail : Set a -> Basics.Bool

Returns wether the set has a tail with elements

hasTail ( fromListWithValue 1 [ 2, 3 ] ) == True
hasTail ( singleton 1 ) == False
hasTail ( fromListWithHead 1 [] ) == False

Modified functions

remove : comparable -> Set comparable -> Maybe (Set comparable)

Same as Set.remove but will return Nothing if the last value is removed

remove 1 ( fromListWithValue 1 [ 2, 3 ] ) == Just ( fromListWithValue 2 [ 3 ] )
remove 1 ( singleton 1 ) == Nothing
remove 2 ( singleton 1 ) == Just ( singleton 1 )

filter : (comparable -> Basics.Bool) -> Set comparable -> Maybe (Set comparable)

Same as Set.filter but will return Nothing if no values satisfy the test

filter isKeyEven ( fromListWithValue 1 [ 2, 3 ] ) == Just ( fromListWithValue 2 [] )
filter isKeyEven ( singleton 1 ) == Nothing

diff : Set comparable -> Set comparable -> Maybe (Set comparable)

Same as Set.diff but will return Nothing if the resulting set is empty

diff ( fromListWithValue 1 [ 2, 3 ] ) ( singleton 1 ) == Just ( fromListWithValue 2 2 [ ( 3, 3 ) ] )
diff ( singleton 1 ) ( singleton 1 ) == Nothing
diff ( singleton 2 ) ( singleton 1 ) == Just ( singleton 2 )

intersect : Set comparable -> Set comparable -> Maybe (Set comparable)

Same as Set.intersect but will return Nothing if the resulting set is empty

intersect ( fromListWithValue 1 [ 2, 3 ] ) ( singleton 1 ) == Just ( fromListWithValue 1 1 [] )
intersect ( singleton 1 ) ( singleton 1 ) == Just ( singleton 1 )
intersect ( singleton 2 ) ( singleton 1 ) == Nothing

Unchanged functions

member : comparable -> Set comparable -> Basics.Bool

Same as Set.member

insert : comparable -> Set comparable -> Set comparable

Same as Set.insert

size : Set a -> Basics.Int

Same as Set.size

map : (comparable1 -> comparable2) -> Set comparable1 -> Set comparable2

Same as Set.map

foldl : (a -> b -> b) -> b -> Set a -> b

Same as Set.foldl

foldr : (a -> b -> b) -> b -> Set a -> b

Same as Set.foldr

toList : Set a -> NotEmpty.List.List a

Same as Set.toList

union : Set comparable -> Set comparable -> Set comparable

Same as Set.union