Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save deanwampler/58066a74670639ccecd67bbe7ec78efd to your computer and use it in GitHub Desktop.
Save deanwampler/58066a74670639ccecd67bbe7ec78efd to your computer and use it in GitHub Desktop.
import scala.annotation.targetName
trait Semigroup[T]:
extension (t: T)
infix def combine(other: T): T
@targetName("plus") def <+>(other: T): T = t.combine(other)
trait Monoid[T] extends Semigroup[T]:
def unit: T
given NumericMonoid[T](using num: Numeric[T]): Monoid[T] with
def unit: T = num.zero
extension (t: T) infix def combine(other: T): T = num.plus(t, other)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment