#readwise
# Semigroups

## Metadata
- Author: [[Mark Seemann]]
- Full Title: Semigroups
- URL: https://blog.ploeh.dk/2017/11/27/semigroups/
## Summary
This is article is a part of a bigger blog series that covers various topics from Category theory. The series includes other articles on the topic of semigroups. Here's a list, retrieved on March 17, 2023.
- Bounding box semigroup
- Semigroups accumulate
## Highlights
- Semigroups form a superset of monoids. They are associative binary operations. **While monoids additionally require that an identity element exists, no such requirement exist for semigroups. In other words, all monoids are semigroups, but not all semigroups are monoids.**
- **An operation that returns the smallest of two values form a semigroup.** ... For mathematical integers, no identity element exists, so the minimum operation doesn't form a monoid. In practice, however, .NET 32-bit integers do have an identity element ... All 32-bit numbers are smaller than, or equal to, Int32.MaxValue. This effectively makes Math.Min(int, int) a monoid, but conceptually, it's not
- Perhaps you think that operations like First and Last seem useless in practice, but **when you have a semigroup, you can reduce any non-empty sequence to a single value.** In C#, you can use the Aggregate LINQ method for this. ... LINQ has specialised methods like Min, Last, and so on, but from the perspective of behaviour, Aggregate would have been enough. While there may be performance reasons why some of those specialised methods exist, you can think of all of them as being based on the same abstraction: that of a semigroup.
**Aggregate, and many of the specialised methods, throw an exception if the input sequence is empty. This is because there's no identity element in a semigroup.** The method doesn't know how to create a value of the type T from an empty list.
If, on the other hand, you have a monoid, you can return the identity element in case of an empty sequence.