Skip to content

Instantly share code, notes, and snippets.

@chiller
Last active October 3, 2018 08:03
Show Gist options
  • Save chiller/61142ae9c20894df3e90513565b97b16 to your computer and use it in GitHub Desktop.
Save chiller/61142ae9c20894df3e90513565b97b16 to your computer and use it in GitHub Desktop.
CQRS
sealed trait Command
case class CreatePotato(sort: String) extends Command
case class CookPotato() extends Command
case class EatPotato() extends Command
object Command {
import cats.data.State
def dispatch(c: Command): State[List[String], Unit] = State( s => {
c match {
case EatPotato() => (s.tail, ())
case CreatePotato(sort) => (sort :: s, ())
case CookPotato() => (s, ())
}
})
}
import cats.data.State
object Main extends App {
// parse command from json
val command = CreatePotato("samsø")
val program = for {
_ <- Command.dispatch(command)
_ <- Command.dispatch(command)
_ <- Command.dispatch(command)
_ <- Command.dispatch(command)
_ <- Command.dispatch(command)
_ <- Command.dispatch(EatPotato())
potatoes <- Query.list()
} yield potatoes
println(program.run(List()).value)
}
import cats.data.State
object Query {
def list(): State[List[String], List[String]] = State(s => (s,s))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment