Orasund/elm-bag/1.0.0

Bag

Implementation of a Bag using a dictionary and an encoding function to turn any value into a comparable one.

Type

type alias Bag comparable a =
{ content : Dict comparable ( a, Int )
, encode : a -> comparable
}

type alias of a bag.

Use the encode function to access elements in the dictionary.

Constructor

count : a -> Bag comparable a -> Int

Count the amounts of an element in the bag

empty identity
    |> count "🍎"
    --> 0

.

[ ( "🍎", 42 ) ]
    |> fromAssociationList identity
    |> count "🍎"
    --> 42

.

[ ( "🍊", 42 ) ]
    |> fromAssociationList identity
    |> count "🍎"
    --> 0
empty : (a -> comparable) -> Bag comparable a

Construct an empty bag

empty identity |> toAssociationList --> []
singleton : (a -> comparable) -> a -> Bag comparable a

Construct a bag with a single element

singleton identity "🍎" |> toAssociationList --> [ ( "🍎", 1 ) ]

Operations

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

Insert an element into the bag

empty identity
    |> insert 1 "🍎"
    |> toAssociationList
    --> [("🍎",1)]

.

[ ( "🍊", 1 ), ( "🍎", 1 ), ( "🍇", 1 ) ]
    |> fromAssociationList identity
    |> insert 2 "🍎"
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",3)]

.

[ ( "🍊", 1 ), ( "🍇", 1 ) ]
    |> fromAssociationList identity
    |> insert 1 "🍎"
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",1)]
remove : Int -> a -> Bag comparable a -> Bag comparable a

Remove an element from the bag

empty identity
    |> remove 1 "🍎"
    |> toAssociationList
    --> []

.

[ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 1 ) ]
    |> fromAssociationList identity
    |> remove 1 "🍎"
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",1)]

.

[ ( "🍊", 1 ), ( "🍇", 1 ) ]
    |> fromAssociationList identity
    |> remove 1 "🍎"
    |> toAssociationList
    --> [("🍇",1),("🍊",1)]

.

[ ( "🍊", 1 ), ( "🍎", 1 ), ( "🍇", 1 ) ]
    |> fromAssociationList identity
    |> remove 1 "🍎"
    |> toAssociationList
    --> [("🍇",1),("🍊",1)]
plus : Bag comparable a -> Bag comparable a -> Bag comparable a

Combine two bags.

[ ( "🍇", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> plus (
        [ ( "🍒", 1 ), ( "🍎", 1 ) ]
            |> fromAssociationList identity
    )
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",1),("🍒",1)]

.

[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> plus (singleton identity "🍎")
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",2)]

.

[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> plus (
        [ ( "🍎", 1 ), ( "🍒", 1 ) ]
            |> fromAssociationList identity
    )
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",2),("🍒",1)]
minus : Bag comparable a -> Bag comparable a -> Bag comparable a

subtracts the first bag from the second.

[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> minus (empty identity)
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",1)]

.

empty identity
    |> minus (
        [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
            |> fromAssociationList identity
    )
    |> toAssociationList
    --> []

.

[ ( "🍇", 2 ), ( "🍎", 2 ), ( "🍊", 2 ) ]
    |> fromAssociationList identity
    |> minus (
        [ ( "🍎", 2 ), ( "🍊", 1 ) ]
            |> fromAssociationList identity
    )
    |> toAssociationList
    --> [("🍇",2),("🍊",1)]
intersection : Bag comparable a -> Bag comparable a -> Bag comparable a

Intersect two bags.

[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> intersection (empty identity)
    |> toAssociationList
    --> []

.

empty identity
    |> intersection (
        [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
            |> fromAssociationList identity
    )
    |> toAssociationList
    --> []

.

[ ( "🍎", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> intersection (
        [ ( "🍇", 1 ), ( "🍎", 1 ) ]
            |> fromAssociationList identity
    )
    |> toAssociationList
    --> [("🍎",1)]

.

[ ( "🍇", 1 ), ( "🍎", 2 ), ( "🍊", 3 ) ]
    |> fromAssociationList identity
    |> intersection (
        [ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 3 ) ]
            |> fromAssociationList identity
    )
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",2)]

Methods

isEmpty : Bag comparable a -> Bool

Check if the bag is empty

empty identity
    |> isEmpty
    --> True


singleton identity "🍎"
    |> isEmpty
    --> False
member : a -> Bag comparable a -> Bool

Compute if the element is a member

member "🍎" (empty identity) --> False

.

[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> member "🍎"
    --> True

.

[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
    |> fromAssociationList identity
    |> member "🍒"
    --> False
size : Bag comparable a -> Int

Counts all elements in the bag

size (empty identity) --> 0

.

[ ( "🍇", 1 ), ( "🍎", 2 ), ( "🍊", 3 ) ]
    |> fromAssociationList identity
    |> size
    --> 6

Conversion

fromList : (a -> comparable) -> List a -> Bag comparable a

Counts the occurrences of elements in a bag.

[]
    |> fromList identity
    |> toAssociationList
    --> []

.

[ "🍎", "🍎" ]
    |> fromList identity
    |> toAssociationList
    --> [("🍎",2)]

.

[ "🍇", "🍎", "🍊" ]
    |> fromList identity
    |> toAssociationList
    --> [("🍇",1),("🍊",1),("🍎",1)]

.

[ "🍇", "🍎", "🍊", "🍎", "🍊", "🍎" ]
    |> fromList identity
    |> toAssociationList
    --> [("🍇",1),("🍊",2),("🍎",3)]
toList : Bag comparable a -> List a

Convert into a list with repeating elements

empty identity
    |> toList
     --> []

.

[ ( "🍎", 2 ) ]
    |> fromAssociationList identity
    |> toList
    --> ["🍎","🍎"]

.

[ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 1 ) ]
    |> fromAssociationList identity
    |> toList
    --> ["🍇","🍊","🍎","🍎"]
toAssociationList : Bag comparable a -> List ( a, Int )

Convert into an association list

fromAssociationList : (a -> comparable) -> List ( a, Int ) -> Bag comparable a

Construct from an association list