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
import cats._ | |
import cats.implicits._ | |
import cats.data.Const | |
// List version | |
Traverse[List].traverse(List(1,2,3,4,5,6))(a => if(a%2==0) Const.of[Any](List(a)) else Const.of[Any](List.empty[Int]) | |
).getConst | |
res7: List[Int] = List(2, 4, 6) |
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
import cats._ | |
import cats.implicits._ | |
import cats.data.StateT | |
object Radio { | |
case class Radio(volume: Int) | |
type RadioError = String | |
type RadioEither[A] = Either[RadioError, A] | |
type RadioStateEitherT[A] = StateT[RadioEither, Radio, 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
def functionExample(a: A): B = { | |
// ... do something with A | |
// .. return a value of type B | |
} |
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
object MyThrowable { | |
// This is just here to keep Wartremover happy | |
@SuppressWarnings(Array("org.wartremover.warts.Equals")) | |
implicit final class AnyOps[A](self: A) { | |
def ===(other: A): Boolean = self == other | |
} | |
// Define Try and make it a monad/functor | |
sealed trait Try[+T] |
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
package org.justinhj | |
import zio.blocking.Blocking | |
import zio.clock.Clock | |
import zio.console._ | |
import zio.random.Random | |
import zio.system.System | |
import zio.internal.PlatformLive | |
import zio.DefaultRuntime | |
import zio._ |
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
val finalState2 = finalEvents.map(eventToState).combineAll | |
println(show"Final state 2:\n$finalState2") | |
/* Output: | |
Final state 2: | |
Account holder: Ben Johnson | |
Balance: 80 | |
*/ |
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
val (finalEntity, finalEvents) = | |
commands.foldLeft((sampleAccount, List.empty[BankAccountEvent])) { | |
case ((acc, events), cmd) => | |
val newEvents = acc.processCommand(cmd) | |
val newAcc = newEvents.foldLeft(acc) { | |
case (acc, evt) => | |
acc.processEvent(evt) | |
} | |
(newAcc, events ++ newEvents) | |
} |
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
val t1 = Instant.now | |
val sampleAccount = AccountEntity(1, AccountState(0, None)) | |
val commands = List( | |
DepositCmd(t1.plusSeconds(10), 100), | |
PurchaseCmd(t1.plusSeconds(20), 120), | |
AssignAccountHolderCmd(t1.plusSeconds(40), "Bob Johnson"), | |
DepositCmd(t1.plusSeconds(40), 100), | |
AssignAccountHolderCmd(t1.plusSeconds(50), "Ben Johnson"), |
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
case class AccountEntity(id: Int, state: AccountState) | |
extends PersistentEntity[Int, AccountEntity] { | |
override type Command = BankAccountCommand | |
override type Event = BankAccountEvent | |
override type State = AccountState | |
// Applies the command to the current state, returning a list of events | |
def processCommand(command: Command) : List[Event] = { | |
command match { |
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
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 |