-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a626db
commit 7b9c604
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
core/src/test/scala/com/github/dmytromitin/SyntaxMonoidSemigroupTest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.github.dmytromitin | ||
|
||
import org.scalatest._ | ||
|
||
class SyntaxMonoidSemigroupTest extends FlatSpec with Matchers { | ||
@syntax | ||
trait Semigroup[A] { | ||
def combine(a: A, a1: A): A | ||
} | ||
|
||
object Semigroup { | ||
// object syntax { | ||
// implicit class Ops[A](a: A) { | ||
// def combine(a1: A)(implicit inst: Semigroup[A]): A = inst.combine(a, a1) | ||
// } | ||
// } | ||
} | ||
|
||
@syntax | ||
trait Monoid[A] extends Semigroup[A] { | ||
def empty: A | ||
def combine(a: A, a1: A): A | ||
} | ||
|
||
object Monoid { | ||
def instance[A](f: => A, f1: (A, A) => A): Monoid[A] = new Monoid[A] { | ||
override def empty: A = f | ||
override def combine(a: A, a1: A): A = f1(a, a1) | ||
} | ||
|
||
// object syntax { | ||
// implicit class Ops[A](a: A) { | ||
// def combine(a1: A)(implicit inst: Monoid[A]): A = inst.combine(a, a1) | ||
// } | ||
// } | ||
|
||
implicit val int: Monoid[Int] = instance(0, _ + _) | ||
implicit val str: Monoid[String] = instance("", _ + _) | ||
} | ||
|
||
import Semigroup.syntax._ | ||
|
||
"2 + 3" should "be 5" in { | ||
2.combine(3)(Monoid.int) should be (5) | ||
} | ||
|
||
} |