In mathematics, Monoids can be considered as categories with a single object. They capture the idea of function composition within a set. In fact, all functions from a set into itself naturally form a Monoid with respect to function composition.
In computer science, there are different types of Monoid, such as free, transition, syntactic, trace, and history. A set of strings built from a given set of characters is a free Monoid. The transition Monoid and syntactic Monoid are used to describe finite state machines, whereas trace Monoids and history Monoids provide a foundation for process calculi and concurrent computing.
Simply put, in computer science, a Monoid is a set, a binary operation, and an element of the set with the following rules:
- Associativity of binary operations
- The element is the identity
Simply put, a structure is a Monoid if the structure is a Semigroup with an element that is the identity. So let's define a new protocol that extends our Semigroupprotocol:
protocol Monoid: Semigroup {
static func identity() -> Self
}
extension Int: Monoid {
static func identity() -> Int {
return 0
}
}
extension String: Monoid {
static func identity() -> String {
return ""
}
}
extension Array: Monoid {
static func identity() -> Array {
return []
}
}
We can test our structure as follows:
numberA <> Int.identity() // 3
"A" <> String.identity() // A
As a Monoid has an element, we can use this as an initial and simplify our reduce method as follows:
func mconcat <M: Monoid> (_ elements: [M]) -> M {
return elements.reduce(M.identity(), <>)
}
Let's test this:
print(mconcat([1, 2, 3])) // 6
print(mconcat(["A", "B", "C"])) // ABC
print(mconcat([[1, 2], [3, 4, 5]])) // [1, 2, 3, 4, 5]
For curious readers, the following references are great reads:
- http://www.fewbutripe.com/swift/math/algebra/2015/02/17/algebraic-structure-and-protocols.html
- http://www.fewbutripe.com/swift/math/algebra/monoid/2017/04/18/algbera-of-predicates-and-sorting-functions.html
Also, Kickstarter OSS code at https://github.com/kickstarter/ios-oss and https://github.com/kickstarter/Kickstarter-Prelude are great examples of real world, in production usages of FP paradigms.