JohnBugner/elm-bag/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

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.

repeat : Int -> comparable -> Bag comparable

Create a bag with n copies of a value.

insert : Int -> comparable -> Bag comparable -> Bag comparable

Insert n copies of a value into a bag.

remove : 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', 'a', 'b']


remove 3 'a' bag == fromList ['b']

Query

isEmpty : Bag a -> Bool

Determine if a bag is empty.

member : comparable -> Bag comparable -> Bool

Determine if a value is in a bag.

count : comparable -> Bag comparable -> Int

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

size : Bag a -> Int

Determine the number of values in a bag.

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 comparable -> List comparable

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

fromList : List comparable -> Bag comparable

Convert a list into a bag.

Transform

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

Map a function onto a bag, creating a new bag.

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

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

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

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

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

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

(comparable -> 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.