Skip to content

feat: add Monoid algebraic structure - #596

Open
ghoullier wants to merge 2 commits into
ghoullier-feat-semigroupfrom
ghoullier-feat-monoid
Open

feat: add Monoid algebraic structure#596
ghoullier wants to merge 2 commits into
ghoullier-feat-semigroupfrom
ghoullier-feat-monoid

Conversation

@ghoullier

Copy link
Copy Markdown
Owner

Summary

Adds the Monoid algebraic structure, which extends Semigroup with an identity element (empty).

A Monoid satisfies two additional laws on top of Semigroup's associativity:

  • Right identity: a.concat(empty) === a
  • Left identity: empty.concat(a) === a

What's included

  • Monoid<T> interface extending Semigroup<T>
  • MonoidConstructor<T> type providing the empty() factory
  • SumMonoid — identity is Sum(0)
  • ProductMonoid — identity is Product(1)
  • StrMonoid — identity is Str('')
  • concatAll<T>(monoid, values) — folds an array using the monoid's identity and concat, enabling safe reduction of collections (including empty ones)

Why

Having a Monoid enables folding/reducing collections without requiring a non-empty input — the identity element serves as the base case. This is a fundamental building block for many FP patterns.

ghoullier and others added 2 commits June 30, 2026 17:19
Add Semigroup interface and concrete implementations (Sum, Product, Str)
with full test coverage including associativity law verification.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Implement the Monoid type which extends Semigroup with an identity
element (empty). Includes SumMonoid, ProductMonoid, StrMonoid
constructors and a generic concatAll function for folding collections.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@ghoullier
ghoullier force-pushed the ghoullier-feat-semigroup branch 5 times, most recently from 1aa6c9b to 47d0c98 Compare July 1, 2026 17:09
@ghoullier
ghoullier force-pushed the ghoullier-feat-semigroup branch 2 times, most recently from 366cccb to f4db211 Compare July 10, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant