JohnBugner / elm-bag / Bag

A set of values where, unlike Set, each value can appear multiple times.

It's basically just a fancy wrapper for Dict, so like Dict, insert, remove, and query operations all take O(log n) time.

Bags


type Bag a

A set of possibly multiple values.

Build

empty : Bag a

Create an empty bag.

singleton : comparable -> Bag comparable

Create a bag with a single copy of a value.

repeat : Basics.Int -> comparable -> Bag comparable

Create a bag with n copies of a value.

insert : Basics.Int -> comparable -> Bag comparable -> Bag comparable

Insert n copies of a value into a bag. If n is negative, then it removes -n copies of the value from the bag.

remove : Basics.Int -> comparable -> Bag comparable -> Bag comparable

Remove n copies of a value from a bag. If n is greater than the numbers of copies that are in the bag, then all copies are simply removed.

bag = fromList [('a',2),('b',1)]

remove 3 'a' bag == fromList [('b',1)]

If n is negative, then it inserts -n copies of the value into the bag.

removeAll : comparable -> Bag comparable -> Bag comparable

Remove all copies of a value from a bag.

Query

isEmpty : Bag a -> Basics.Bool

Determine if a bag is empty.

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

Determine if a value is in a bag.

count : comparable -> Bag comparable -> Basics.Int

Determine the number of copies of a value in a bag.

size : Bag a -> Basics.Int

Determine the number of values in a bag.

bag = fromList [('a',2),('b',1)]

size bag == 3

Combine

union : Bag comparable -> Bag comparable -> Bag comparable

Get the union of two bags. For a value, its two counts are added.

intersect : Bag comparable -> Bag comparable -> Bag comparable

Get the intersection of two bags. For a value, the lesser of its two counts is taken.

diff : Bag comparable -> Bag comparable -> Bag comparable

Get the difference between of two bags. For a value, the count of the second is removed from the count of the first.

Lists

toList : Bag a -> List a

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

fromList : List comparable -> Bag comparable

Convert a list into a bag.

toAssociationList : Bag a -> List ( a, Basics.Int )

Convert a bag into an association list, sorted from lowest to highest.

fromAssociationList : List ( comparable, Basics.Int ) -> Bag comparable

Convert an association list into a bag.

toSet : Bag comparable -> Set comparable

Convert a bag into a set.

fromSet : Set comparable -> Bag comparable

Convert a set into a bag.

Transform

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

Map a function onto a bag, creating a new bag. If keys clash after mapping, their counts are simply added.

bag = fromList [('a',2),('b',1)]

map (always 'c') bag == fromList [('c',3)]

foldl : (a -> Basics.Int -> b -> b) -> b -> Bag a -> b

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

foldr : (a -> Basics.Int -> b -> b) -> b -> Bag a -> b

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

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

Create a new bag consisting only of values which satisfy a predicate.

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

Create two new bags; the first consisting of values which satisfy a predicate, the second consisting of values which do not.