y0hy0h / ordered-containers / OrderedSet

A set of unique values that remembers their insertion order. The values can be any comparable type. This includes Int, Float, Time, Char, String, and tuples or lists of comparable types.

The insertion order is reflected in the functions under "Conversions" and "Transform". The list order and the iteration order will be the order of insertion.

The API mirrors the core Set's API, with exception for the functions listed there under "Combine", because these functions do not have an obvious way to handle the order between the combined sets.

Sets


type OrderedSet comparable

Represents a set of unique values that remembers insertion order.

Build

empty : OrderedSet comparable

Create an empty set.

singleton : comparable -> OrderedSet comparable

Create a set with one value.

insert : comparable -> OrderedSet comparable -> OrderedSet comparable

Insert a value into a set. If the key already exists, the old value will be forgotten and the new value will be inserted at the end.

import OrderedSet

OrderedSet.empty
    |> OrderedSet.insert 1
    |> OrderedSet.insert 2
    |> OrderedSet.insert 1
    |> OrderedSet.toList
--> [ 2, 1 ]

remove : comparable -> OrderedSet comparable -> OrderedSet comparable

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

Query

isEmpty : OrderedSet comparable -> Basics.Bool

Determine if a set is empty.

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

Determine if a value is in a set.

size : OrderedSet comparable -> Basics.Int

Determine the number of elements in a set.

Conversions

toList : OrderedSet comparable -> List comparable

Convert a set into a list in insertion order.

fromList : List comparable -> OrderedSet comparable

Convert a list into a set, removing any duplicates.

toSet : OrderedSet comparable -> Set comparable

Convert an ordered set into a regular Set.

Transform

map : (comparable -> comparable2) -> OrderedSet comparable -> OrderedSet comparable2

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

foldl : (comparable -> b -> b) -> b -> OrderedSet comparable -> b

Fold over the values in a set, in insertion order.

foldr : (comparable -> b -> b) -> b -> OrderedSet comparable -> b

Fold over the values in a set, in reverse insertion order.

filter : (comparable -> Basics.Bool) -> OrderedSet comparable -> OrderedSet comparable

Only keep elements that pass the given test.

partition : (comparable -> Basics.Bool) -> OrderedSet comparable -> ( OrderedSet comparable, OrderedSet comparable )

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.

The order will be preserved in these new sets in the sense that elements that are inserted after each other will remain ordered after each other.