-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 2: Create type class Combinator
- Loading branch information
rafaparadela
committed
Nov 25, 2018
1 parent
9dc4a61
commit b81c78d
Showing
4 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
package training | ||
|
||
import training.datatypes._ | ||
import training.instances._ | ||
import training.typeclasses._ | ||
|
||
object implicits { | ||
|
||
implicit def intCombinator: Combinator[Int] = new IntCombinator | ||
|
||
implicit def maybeCombinator[A](implicit CA: Combinator[A]): Combinator[Maybe[A]] = new MaybeCombinator[A] | ||
|
||
} |
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 |
---|---|---|
@@ -1,6 +1,24 @@ | ||
package training | ||
|
||
import training.datatypes._ | ||
import training.typeclasses._ | ||
|
||
object instances { | ||
|
||
class IntCombinator extends Combinator[Int] { | ||
override def combine(x: Int, y: Int): Int = x + y | ||
} | ||
|
||
class MaybeCombinator[A](implicit CA: Combinator[A]) extends Combinator[Maybe[A]] { | ||
|
||
override def combine(x: Maybe[A], y: Maybe[A]): Maybe[A] = (x, y) match { | ||
case (No, No) => No | ||
case (No, Yes(yy)) => y | ||
case (Yes(xx), No) => x | ||
case (Yes(xx), Yes(yy)) => Yes(CA.combine(xx, yy)) | ||
} | ||
|
||
} | ||
|
||
|
||
} |
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 |
---|---|---|
|
@@ -2,5 +2,9 @@ package training | |
|
||
object typeclasses { | ||
|
||
trait Combinator[A] { | ||
def combine(x: A, y: A): A | ||
} | ||
|
||
|
||
} |