Orasund/elm-bag/1.0.0

List.Bag

Use an association list as a bag

Constructor

count : a -> List ( a, Int ) -> Int

Count the amounts of an element in the list

count "🍎" [] --> 0


count "🍎" [ ( "🍎", 42 ) ] --> 42


count "🍎" [ ( "🍊", 42 ) ] --> 0
empty : List ( a, Int )

Construct an empty list

empty --> []
singleton : a -> List ( a, Int )

Construct a list with a single element

singleton "🍎" --> [ ( "🍎", 1 ) ]

Operations

insert : Int -> a -> List ( a, Int ) -> List ( a, Int )

Insert an element into the list

insert 1 "🍎" [] --> [("🍎",1)]


insert 2 "🍎" [ ( "🍊", 1 ), ( "🍎", 1 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍎",3),("🍇",1)]


insert 1 "🍎" [ ( "🍊", 1 ), ( "🍇", 1 ) ] --> [("🍎",1),("🍊",1),("🍇",1)]
remove : Int -> a -> List ( a, Int ) -> List ( a, Int )

Remove an element from the list

remove 1 "🍎" [] --> []


remove 1 "🍎" [ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍎",1),("🍇",1)]


remove 1 "🍎" [ ( "🍊", 1 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍇",1)]


remove 1 "🍎" [ ( "🍊", 1 ), ( "🍎", 1 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍇",1)]
plus : List ( a, Int ) -> List ( a, Int ) -> List ( a, Int )

Combine two lists. The order of the second list gets preserved.

plus [ ( "🍒", 1 ), ( "🍎", 1 ) ] [ ( "🍇", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍊",1),("🍒",1),("🍎",1)]


plus [ ( "🍎", 1 ) ] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍎",2),("🍊",1)]


plus [ ( "🍎", 1 ), ( "🍒", 1 ) ] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍎",2),("🍊",1),("🍒",1)]
minus : List ( a, Int ) -> List ( a, Int ) -> List ( a, Int )

subtracts the first list from the second.

minus [] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍎",1),("🍊",1)]


minus [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] [] --> []


minus [ ( "🍎", 2 ), ( "🍊", 1 ) ] [ ( "🍇", 2 ), ( "🍎", 2 ), ( "🍊", 2 ) ] --> [("🍇",2),("🍊",1)]
intersection : List ( a, Int ) -> List ( a, Int ) -> List ( a, Int )

Intersect two lists. The order of the second list gets preserved.

intersection [] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> []


intersection [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] [] --> []


intersection [ ( "🍇", 1 ), ( "🍎", 1 ) ] [ ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍎",1)]


intersection [ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 3 ) ] [ ( "🍇", 1 ), ( "🍎", 2 ), ( "🍊", 3 ) ] --> [("🍇",1),("🍎",2),("🍊",1)]

Methods

isEmpty : List ( a, Int ) -> Bool

Check if the list is empty

isEmpty [] --> True


isEmpty [ ( "🍎", 42 ) ] --> False
member : a -> List ( a, Int ) -> Bool

Compute if the element is a member

member "🍎" [] --> False


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


member "🍒" [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> False
size : List ( a, Int ) -> Int

Counts all elements in the list

size [] --> 0


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

Conversion

fromList : List a -> List ( a, Int )

Counts the occurrences of elements in a list.

fromList [] --> []


fromList [ "🍎", "🍎" ] --> [("🍎",2)]


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


fromList [ "🍇", "🍎", "🍊", "🍎", "🍊", "🍎" ] --> [("🍇",1),("🍎",3),("🍊",2)]
toList : List ( a, Int ) -> List a

Convert into a list with repeating elements

toList [] --> []


toList [ ( "🍎", 2 ) ] --> ["🍎","🍎"]


toList [ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 1 ) ] --> ["🍊","🍎","🍎","🍇"]