Semigroup
Combine values systematically with the concat operation
What is Semigroup?
A Semigroup is a type that has a way to combine two values into one (called concat). It's associative, meaning the order of operations doesn't matter. Perfect for merging configurations, combining validation results, or aggregating data.
// Instead of manual merging:
const merged = { ...obj1, ...obj2 }
// Use Semigroup:
const merged = Semigroup.concat(obj1, obj2)concat
struct
first/last
combining patterns
➕
Concat Operation
Combine two values into one
🔗
Associative
Order of operations doesn't matter
⚙️
Composable
Build complex combiners from simple ones
🌐
Universal
Works with any type of data
Semigroup in Action
❌ Manual Merging
const merge = (a: Settings, b: Settings): Settings => ({
theme: b.theme || a.theme,
language: b.language || a.language,
notifications: b.notifications || a.notifications
})
// Repetitive, error-prone✅ Semigroup Approach
const SettingsSemigroup = S.struct({
theme: S.first<string>(),
language: S.first<string>(),
notifications: S.first<boolean>()
})
const merge = SettingsSemigroup.concat
// Declarative, reusablePractice Exercises
Why Learn Semigroup?
🔗
Master Semigroup
Learn the fundamental concepts and patterns that make Semigroup powerful
💪
4 Exercises
Practice with hands-on exercises from intermediate level
🚀
Production Ready
Apply Semigroup patterns to build robust, type-safe applications