rtfeldman/elm-sorter-experiment/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Sort.Set

A set of unique values.

Insert, remove, and query operations all take O(log n) time.

This implementation is based on Skinney/elm-dict-exploration, except that it uses Sorter instead of comparable, so it permits keys that are not comparable.

Sets

type Set t

Represents a set of unique values. So (Set Int) is a set of integers and (Set String) is a set of strings.

Build

empty : Sorter a -> Set a

Create an empty set.

singleton : Sorter a -> a -> Set a

Create a set with one value.

add : a -> Set a -> Set a

Add a value to 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 -> Bool

Determine if a set is empty.

member : a -> Set a -> Bool

Determine if a value is in a set.

size : Set a -> Int

Determine the number of elements in a set.

Combine

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

Get the union of two sets. Keep all values.

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

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

{ original : Set a
, other : Set a
}
-> Set a

Keep a value when it appears in the original set but not in the other set.

The original set's Sorter will be used.

Lists

toList : Set a -> List a

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

fromList : Sorter a -> List a -> Set a

Convert a list into a set, removing any duplicates.

Transform

map : Sorter b -> (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 -> 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 -> 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.