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
| case class AccountState(balance: Int, accountHolder: LastOption[String]) | |
| object AccountState { | |
| implicit def accountStateShow[A] = new Show[AccountState] { | |
| def show(a: AccountState): String = { | |
| show"Balance: ${a.balance}\nAccount holder: ${a.accountHolder}" | |
| } | |
| } | |
| implicit val accountMonoid = new Monoid[AccountState] { |
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
| sealed trait BankAccountCommand | |
| case class DepositCmd(time: Instant, amount: Int) extends BankAccountCommand | |
| case class PurchaseCmd(time: Instant, amount: Int) extends BankAccountCommand | |
| case class AssignAccountHolderCmd(time: Instant, accountHolder: String) extends BankAccountCommand | |
| sealed trait BankAccountEvent | |
| case class DepositEvt(time: Instant, amount: Int) extends BankAccountEvent | |
| case class PurchaseEvt(time: Instant, amount: Int) extends BankAccountEvent | |
| case class AssignAccountHolderEvt(time: Instant, accountHolder: String) extends BankAccountEvent |
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 PersistentEntity[IDType, T <: PersistentEntity[IDType, T]] { | |
| type Command | |
| type Event | |
| type State | |
| def id: IDType | |
| def state : State | |
| def processCommand(command: Command) : List[Event] | |
| def processEvent(event: Event) : T |
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
| /*\ | |
| {\o/} | |
| /_\ | |
| \*|*/ | |
| ///|\\\ | |
| ////|\\\\ | |
| /////|\\\\\ | |
| object MerryX | |
| {def main(santa | |
| : Array[String]){ |
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
| .onCommand[AssignAccountHolderCmd, Done]{ | |
| case (AssignAccountHolderCmd(time, accountHolder), ctx, state) => | |
| ctx.thenPersist(AssignAccountHolderEvt(time, accountHolder)){ | |
| _ => | |
| ctx.reply(Done) | |
| } | |
| } |
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
| Foldable[List].fold(List(1,2,3)) | |
| // res1: Int = 6 | |
| Foldable[List].fold(List("1","2","3")) | |
| // res2: String = "123" | |
| // Or you can use the combineAll function | |
| List(1,2,3).combineAll | |
| // res3: Int = 6 |
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
| import cats.Monoid | |
| Monoid[Int].empty |+| 10 | |
| // res1: Int = 10 | |
| 10 |+| Monoid[Int].empty | |
| // res2: Int = 10 |
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 Monoid[A] extends Semigroup[A] { | |
| def empty: A | |
| } |
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
| import cats.Foldable | |
| // Combining a list with a foldLeft | |
| Foldable[List].foldLeft(List(1,2,3), 0){_ |+| _} | |
| // res1: Int = 6 | |
| // Combining a list of string with a foldLeft | |
| Foldable[List].foldLeft(List("1","2","3"), "")(_ |+| _) |
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
| import cats.Semigroup | |
| import cats.implicits._ | |
| // Combining strings | |
| "Semigroups".combine(" ".combine("combine".combine(" ".combine("things")))) | |
| // res1: String = "Semigroups combine things | |
| // Which is hard to read so Cats provides convenient infix syntax |