feat: add Monoid algebraic structure - #596
Open
ghoullier wants to merge 2 commits into
Open
Conversation
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
force-pushed
the
ghoullier-feat-semigroup
branch
5 times, most recently
from
July 1, 2026 17:09
1aa6c9b to
47d0c98
Compare
ghoullier
force-pushed
the
ghoullier-feat-semigroup
branch
2 times, most recently
from
July 10, 2026 21:23
366cccb to
f4db211
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the Monoid algebraic structure, which extends
Semigroupwith an identity element (empty).A Monoid satisfies two additional laws on top of Semigroup's associativity:
a.concat(empty) === aempty.concat(a) === aWhat's included
Monoid<T>interface extendingSemigroup<T>MonoidConstructor<T>type providing theempty()factorySumMonoid— identity isSum(0)ProductMonoid— identity isProduct(1)StrMonoid— identity isStr('')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.