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
| def program[F[_]](implicit M: Monad[F]): IndexedReaderWriterStateT[F, Env, Vector[SystemState], SystemState, SystemState, SystemState] = { | |
| for { | |
| // retrieve environment input | |
| env <- IndexedReaderWriterStateT.ask[F, Env, Vector[SystemState], SystemState] | |
| // get server status as F[Status] and lift into transformer context | |
| statusF = getServerStatus[F]().apply(env) | |
| status <- IndexedReaderWriterStateT.liftF[F, Env, Vector[SystemState], SystemState, Status](statusF) | |
| // get current state |
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
| program[IO].run(Env("127.0.0.1:8080"), SystemState(Map.empty[Env, Status])) // IO[(Vector[SystemState], SystemState, SystemState)] |
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 MonadWriter[F[_], L] { | |
| val monad: Monad[F] | |
| def tell(l: L): F[Unit] | |
| def logs: F[L] | |
| } | |
| trait MonadState[F[_], S] { | |
| val monad: Monad[F] |
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
| def createMonadWriter[L](implicit M: Monoid[L]): IO[MonadWriter[IO, L]] = { | |
| for { | |
| writer <- Ref.of[IO, L](M.empty) | |
| } yield new MonadWriter[IO, L] { | |
| val monad: Monad[IO] = Monad[IO] | |
| def tell(l: L): IO[Unit] = writer.update(_.combine(l)) | |
| def logs: IO[L] = writer.get | |
| } | |
| } |
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
| def program[F[_]](R: MonadReader[F, Env], | |
| W: MonadWriter[F, Vector[SystemState]], | |
| S: MonadState[F, SystemState]): F[(Vector[SystemState], SystemState)] = { | |
| implicit val M: Monad[F] = R.monad | |
| for { | |
| status <- R.reader(getServerStatus[F]()) | |
| state <- S.get |
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
| def main(): IO[(Vector[SystemState], SystemState)] = { | |
| for { | |
| monadReader <- createMonadReader(Env("127.0.0.1:8080")) | |
| monadWriter <- createMonadWriter[Vector[SystemState]] | |
| monadState <- createMonadState(SystemState(Map.empty[Env, Status])) | |
| result <- program(monadReader, monadWriter, monadState) | |
| } yield result | |
| } |
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 Restaurant { | |
| def menu: String | |
| } | |
| case class Mexican() extends Restaurant { | |
| def menu: String = "tacos" | |
| } | |
| case class Italian() extends Restaurant { | |
| def menu: String = "spaghetti" |
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
| def order(restaurant: Restaurant): String = { | |
| restaurant.menu | |
| } |
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 Set { | |
| def get(index: Long): ??? | |
| def add(value: ???): Set | |
| } |
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 Set { | |
| def get(index: Long): Any | |
| def add(value: Any): Set | |
| } |