Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
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
trait Semigroup[A] {
def combine(x: A, y: A): A
}
"handle deposit and successful withdrawal" in withTestDriver
{ driver =>
val t1 = Instant.now
val t2 = t1.plusSeconds(100)
val outcome = driver.run(DepositCmd(t1, "Opening deposit", 100))
outcome.replies should contain only 100
val outcome2 = driver.run(WithdrawCmd(t2, "Transfer to savings", 20))
outcome2.replies should contain only SuccessfulWithdrawalResponse(80)