Multi-element data-structures, like Streams
and Lists
, seem to have some distinct properties. For example,
Monoids
Semigroup
implementation that merges the elements, without knowing about the element type) Single-element data-structures are also Monoids
, but cannot have a semi-group implementation without knowing about the type
they contain (eg Option
, Future
, etc.) They also do not guarantee that the element still "exists" after it has been added/combined.
Is there a name for this type of data-structure that contains all elements that have been added (combined)? My aim is to create a distinct type-class for these types of data-structures to distinguish them from other Monoids/Semigroups.
Technically what you ask is sill a monoid. If you want to be able to combine A
and A
into another A
, there is some idea of empty A
that doesn't change the result, and the operations is associative - then this is monoid no matter if:
A
is some plain type that you combine as numbers A
is some F[B]
where monoidal properties belong to F
and would be true for all possible B
For practical reasons there are 2 type classes in Cats:
Monoid[A]
which gives you combine(a1: A, a2: A): A
, empty: A
and everything you can achieve with these two MonoidK[F[_]]
, which is basically a factory: [A] => Monoid[F[A]]
, but with utilities that allow you to defer the application of A
eg combineK[A](fa1: F[A], fa2: F[A]): F[A]
, emptyK[A]: F[A]
Same is true for semigroup, where you have Semigroup
, SemigroupK
and also Semigrupal
, which is a semigroup on types (addition of A
and B
, means creation of a tuple (A, B)
, and (A + B) + C
= A + (B + C)
if we assume equality up to isomorphism). This shows that we don't need to invent new algebras, old one works just fine, though sometimes different use cases might require a slightly different implementations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.