Implementation of a Bag using a dictionary and an encoding function to turn any value into a comparable one.
Count the amounts of an element in the bag
empty identity
|> count "🍎"
--> 0.
[ ( "🍎", 42 ) ]
|> fromAssociationList identity
|> count "🍎"
--> 42.
[ ( "🍊", 42 ) ]
|> fromAssociationList identity
|> count "🍎"
--> 0Construct an empty bag
empty identity |> toAssociationList --> []Construct a bag with a single element
singleton identity "🍎" |> toAssociationList --> [ ( "🍎", 1 ) ]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 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)]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)]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)]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)]Check if the bag is empty
empty identity
|> isEmpty
--> True
singleton identity "🍎"
|> isEmpty
--> FalseCompute if the element is a member
member "🍎" (empty identity) --> False.
[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
|> fromAssociationList identity
|> member "🍎"
--> True.
[ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ]
|> fromAssociationList identity
|> member "🍒"
--> FalseCounts all elements in the bag
size (empty identity) --> 0.
[ ( "🍇", 1 ), ( "🍎", 2 ), ( "🍊", 3 ) ]
|> fromAssociationList identity
|> size
--> 6Counts 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)]Convert into a list with repeating elements
empty identity
|> toList
--> [].
[ ( "🍎", 2 ) ]
|> fromAssociationList identity
|> toList
--> ["🍎","🍎"].
[ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 1 ) ]
|> fromAssociationList identity
|> toList
--> ["🍇","🍊","🍎","🍎"]Convert into an association list
Construct from an association list
type alias of a bag.
Use the encode function to access elements in the dictionary.