Chadtech/unique-list/2.1.1

NOTE: the latest version is 2.1.5

List.Unique

A list that can only contain unique elements

UniqueList

type UniqueList a

An arrangement of things in an order. An UniqueList is kind of like a List, except there cant be duplicate elements. And, its kind of like a Set, except an UniqueLists elements go from first to last.

Basics

Create an empty `UniqueList

List.Unique.empty == List.Unique.fromList []
isEmpty : UniqueList a -> Bool

Check if an Order is empty

Order.isEmpty Order.empty == True
fromList : List a -> UniqueList a

Create a UniqueList from a List

toList : UniqueList a -> List a

Turn a UniqueList into a List

let
    u =
        [ 'u' ]


    doubleU =
        [ 'u', 'u' ]
in
u == (List.Unique.toList <| List.Unique.fromList doubleU)
length : UniqueList a -> Int

Get the length of a UniqueList

nobleGases = List.Unique.fromList
    [ helium
    , neon
    , argon
    , krypton
    , xenon
    , radon
    ]


List.Unique.length nobleGases == 6

Reverse a UniqueList

bestCoffeeDrinks ==
    List.Unique.fromList
        [ cortado
        , latte
        , coldbrew
        , coffee
        , mocha
        ]


worstCoffeeDrinks : UniqueList Coffee
worstCoffeeDrinks =
    List.Unique.reverse bestCoffeeDrinks
member : a -> UniqueList a -> Bool

Check if something is in a UniqueList

List.Unique.member "Grover Cleveland" usPresidents == True

Modifying UniqueLists

Remove an element from a `UniqueList

cons : a -> UniqueList a -> UniqueList a

Add an element to the beginning of a UniqueList

addBefore : a -> a -> UniqueList a -> UniqueList a

Add an element to a UniqueList before another element

addBefore `c` `b` (List.Unique.fromList [ `a`, `c`, `d` ])
    == List.Unique.fromList [ `a`, `b`, `c`, `d` ]
addAfter : a -> a -> UniqueList a -> UniqueList a

Add an element to a UniqueList before another element

addAfter `b` `c` (List.Unique.fromList [ `a`, `b`, `d` ])
    == List.Unique.fromList [ `a`, `b`, `c`, `d` ]
isBefore : a -> a -> UniqueList a -> Maybe Bool

Check if an element is before another, if it is in the UniqueList at all.

germanStates = List.Unique.fromList [ "Bavaria", "Brandenberg" ]


("Bavaria" |> List.Unique.isBefore "Brandenberg") germanStates == Just True


("Bavaria" |> List.Unique.isBefore "New York City") germanStates == Nothing
isAfter : a -> a -> UniqueList a -> Maybe Bool

Check if an element is after another, if it is in the UniqueList at all.

isFirst : a -> UniqueList a -> Maybe Bool

Check is an element is the first in a UniqueList

Util

filterDuplicates : List a -> List a

If you just need to filter out duplicates, but dont want to use th UniqueList type, use this function. Its just like the unique function in elm-community/list-extra, except for the fact that List.Extra.unique only works on List comparable.

filterDuplicates [ True, True ] == [ True ]


filterDuplicates [ 1, 1, 2, 3, 5 ] == [ 1, 2, 3, 5 ]