Orasund / elm-bag / List.Bag

Use an association list as a bag

Constructor

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

Count the amounts of an element in the list

count "🍎" [] --> 0

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

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

empty : List ( a, Basics.Int )

Construct an empty list

empty --> []

singleton : a -> List ( a, Basics.Int )

Construct a list with a single element

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

Operations

insert : Basics.Int -> a -> List ( a, Basics.Int ) -> List ( a, Basics.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 : Basics.Int -> a -> List ( a, Basics.Int ) -> List ( a, Basics.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, Basics.Int ) -> List ( a, Basics.Int ) -> List ( a, Basics.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, Basics.Int ) -> List ( a, Basics.Int ) -> List ( a, Basics.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, Basics.Int ) -> List ( a, Basics.Int ) -> List ( a, Basics.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, Basics.Int ) -> Basics.Bool

Check if the list is empty

isEmpty [] --> True

isEmpty [ ( "🍎", 42 ) ] --> False

member : a -> List ( a, Basics.Int ) -> Basics.Bool

Compute if the element is a member

member "🍎" [] --> False

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

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

size : List ( a, Basics.Int ) -> Basics.Int

Counts all elements in the list

size [] --> 0

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

Conversion

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

Counts the occurrences of elements in a list.

fromList [] --> []

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

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

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

toList : List ( a, Basics.Int ) -> List a

Convert into a list with repeating elements

toList [] --> []

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

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