embracing-nondeterminism-code/src/main/scala/green/thisfieldwas/embracingnondeterminism/data/Semigroup.scala

44 lines
1.1 KiB
Scala

package green.thisfieldwas.embracingnondeterminism.data
/** Semigroups are simple structures that have an additive or multiplicative
* operation referred to as "combine". Many structures have the properties of a
* semigroup, and you've probably used a number of them. Some common structures
* and their operations include:
*
* - Lists under concatenation
* - Strings under concatenation
* - Integers under addition
* - Booleans under &&
* - Sets under union
*
* Look under
* `src/test/scala/green/thisfieldwas/embracingnondeterminism/stdlib` for
* property checks highlighting real-life Semigroups:
* - BooleanSpec
* - IntegerSpec
* - ListSpec
* - SetSpec
* - StringSpec
*
* @tparam S
* The semigroup type.
*/
trait Semigroup[S] {
/** Combine type values of the semigroup into a new value.
*
* @param left
* The left value.
* @param right
* The right value.
* @return
* The combined value.
*/
def combine(left: S, right: S): S
}
object Semigroup {
def apply[S: Semigroup]: Semigroup[S] = implicitly[Semigroup[S]]
}