fun_land v0.9.3 FunLand.Combinable behaviour
If an operation is can combine two elements, and there is a clearly defined empty
empty version
that can be used to keep the same element when used on an element.
In Category Theory, something that is Combinable is called a Monoid.
By doing use FunLand.Combinable
you get an implementation of the Collectable
protocol for free.
If you want to implement your own version of Collectable, add Combinable with use FunLand.Combinable, auto_collectable: false
.
Examples
- Integer-addition with 0 as empty element forms a Monoid, also known as the Sum.
- Integer-multiplication with 1 as empty element forms a Monoid, also known as the Product.
- List concatenation with
[]
as empty element. - String concatenation with the empty string
""
as empty element. - Set union with the empty set as empty element.
Fruit Salad Example
Bowls that you can use to mix fruits in, are a monoid:
The combine
operation would be to put the fruits from Bowl A into Bowl B, keeping that one.
The empty
operation would be to take an emtpy bowl.
As can be seen, this follows the Combinable laws:
- left-identity: putting the contents of an empty bowl into a bowl with apples, would be the same as doing nothing (you still have 'a bowl with apples')
- right-identity: putting the contents a bowl filled with apples into an empty bowl, would be the same as doing nothing (you still have 'a bowl with apples')
Link to this section Summary
Link to this section Types
Link to this type
combinable(a)
combinable(a)
combinable(a) :: FunLand.adt(a)
combinable(a) :: FunLand.adt(a)
Link to this section Functions
Link to this function
combine(a, b)
Link to this function
empty(combinable)
Link to this section Callbacks
Link to this callback
empty()
empty()
empty() :: combinable(a) when a: any()
empty() :: combinable(a) when a: any()