Created
January 4, 2025 13:19
-
-
Save p-pavel/a6456d01422a8d8114d38f3a7073acea to your computer and use it in GitHub Desktop.
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
trait Semigroup[T]: | |
type C = T | |
extension (a: C) def |+|(b: C): C | |
trait Monoid[T] extends Semigroup[T]: | |
def empty: T | |
def mkMonoid[T: Semigroup as s](e: T) = | |
new: | |
def empty = e | |
export s.* | |
given additive: Semigroup[Int] with | |
extension (a: C) def |+|(b: C) = a + b | |
given multiplicative: Semigroup[Int] with | |
extension (a: C) def |+|(b: C) = a * b | |
extension [T: Semigroup](t: T) def double = t |+| t | |
@main | |
def tst = | |
given Semigroup[Int] = additive | |
val a = 10.double | |
val b = 10.double(using multiplicative) | |
println(s"a = $a, b = $b") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment