erlandsona / assoc-set / AssocSet

Implementation of elm/core Set based on pzp1997/assoc-list

A set of unique values. The values can be any type that can be compared using (==). Insert, remove, and query operation performance subject to performance characteristics of the underlying assoc-list implementations.

Sets


type Set a

Represents a set of unique values. So (Set Id) is a set of custom Id types and (Set String) works as usual.

Build

empty : Set a

Create an empty set.

singleton : a -> Set a

Create a set with one value.

insert : a -> Set a -> Set a

Insert a value into a set.

remove : a -> Set a -> Set a

Remove a value from a set. If the value is not found, no changes are made.

Query

isEmpty : Set a -> Basics.Bool

Determine if a set is empty.

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

Determine if a value is in a set.

size : Set a -> Basics.Int

Determine the number of elements in a set.

eq : Set a -> Set a -> Basics.Bool

Compare two sets for equality, ignoring insertion order. Sets are defined to be equal when they have identical keys where the keys are compared using the built-in equality operator.

You should almost never use the built-in equality operator to compare sets from this module since association lists have no canonical form.

Combine

union : Set a -> Set a -> Set a

Get the union of two sets. Keep all values.

intersect : Set a -> Set a -> Set a

Get the intersection of two sets. Keeps values that appear in both sets.

diff : Set a -> Set a -> Set a

Get the difference between the first set and the second. Keeps values that do not appear in the second set.

Lists

toList : Set a -> List a

Convert a set into a list, sorted from lowest to highest.

fromList : List a -> Set a

Convert a list into a set, removing any duplicates.

Transform

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

Map a function onto a set, creating a new set with no duplicates.

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

Fold over the values in a set, in order from lowest to highest.

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

Fold over the values in a set, in order from highest to lowest.

filter : (a -> Basics.Bool) -> Set a -> Set a

Only keep elements that pass the given test.

import Set exposing (Set)

numbers : Set Int
numbers =
    Set.fromList [ -2, -1, 0, 1, 2 ]

positives : Set Int
positives =
    Set.filter (\x -> x > 0) numbers

-- positives == Set.fromList [ 1, 2 ]

partition : (a -> Basics.Bool) -> Set a -> ( Set a, Set a )

Create two new sets. The first contains all the elements that passed the given test, and the second contains all the elements that did not.