Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
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] {
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
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
/*\
{\o/}
/_\
\*|*/
///|\\\
////|\\\\
/////|\\\\\
object MerryX
{def main(santa
: Array[String]){
.onCommand[AssignAccountHolderCmd, Done]{
case (AssignAccountHolderCmd(time, accountHolder), ctx, state) =>
ctx.thenPersist(AssignAccountHolderEvt(time, accountHolder)){
_ =>
ctx.reply(Done)
}
}
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
import cats.Monoid
Monoid[Int].empty |+| 10
// res1: Int = 10
10 |+| Monoid[Int].empty
// res2: Int = 10
trait Monoid[A] extends Semigroup[A] {
def empty: A
}
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"), "")(_ |+| _)
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