def liftOption[S,A](oa: Option[A]): OptionState[S,A] = oa match { case Some(a) => OptionState {s => (s,a).some } case None => OptionState {_ => none} } def liftState[S,A](sa: State[S,A]): OptionState[S,A] = OptionState {s => sa(s).some}
1 val osprg: OptionState[Int,Int] = for { 2 a <- liftOption(3.some) 3 b <- liftState(put(a)) 4 c <- liftState(get[Int]) 5 d <- liftState(State[Int,Int](s => (s+c, s+a))) 6 } yield c //> osprg : Exercises.rws.OptionState[Int,Int] = OptionState(<function1>)
7 osprg.run(2) //> res3: Option[(Int, Int)] = Some((6,3))
8 val osprg1: OptionState[Int,Int] = for { 9 a <- liftOption(3.some) 10 b <- liftState(put(a)) 11 _ <- liftOption((None: Option[Int])) 12 c <- liftState(get[Int]) 13 d <- liftState(State[Int,Int](s => (s+c, s+a))) 14 } yield c //> osprg1 : Exercises.rws.OptionState[Int,Int] = OptionState(<function1>)
15 osprg1.run(2) //> res4: Option[(Int, Int)] = None
type ReaderWriterStateT[F[_], -R, W, S, A] = IndexedReaderWriterStateT[F, R, W, S, S, A] object ReaderWriterStateT extends ReaderWriterStateTInstances with ReaderWriterStateTFunctions { def apply[F[_], R, W, S, A](f: (R, S) => F[(W, A, S)]): ReaderWriterStateT[F, R, W, S, A] = IndexedReaderWriterStateT[F, R, W, S, S, A] { (r: R, s: S) => f(r, s) } } type IndexedReaderWriterState[-R, W, -S1, S2, A] = IndexedReaderWriterStateT[Id, R, W, S1, S2, A] object IndexedReaderWriterState extends ReaderWriterStateTInstances with ReaderWriterStateTFunctions { def apply[R, W, S1, S2, A](f: (R, S1) => (W, A, S2)): IndexedReaderWriterState[R, W, S1, S2, A] = IndexedReaderWriterStateT[Id, R, W, S1, S2, A] { (r: R, s: S1) => f(r, s) } } type ReaderWriterState[-R, W, S, A] = ReaderWriterStateT[Id, R, W, S, A] object ReaderWriterState extends ReaderWriterStateTInstances with ReaderWriterStateTFunctions { def apply[R, W, S, A](f: (R, S) => (W, A, S)): ReaderWriterState[R, W, S, A] = IndexedReaderWriterStateT[Id, R, W, S, S, A] { (r: R, s: S) => f(r, s) } } type IRWST[F[_], -R, W, -S1, S2, A] = IndexedReaderWriterStateT[F, R, W, S1, S2, A] val IRWST: IndexedReaderWriterStateT.type = IndexedReaderWriterStateT type IRWS[-R, W, -S1, S2, A] = IndexedReaderWriterState[R, W, S1, S2, A] val IRWS: IndexedReaderWriterState.type = IndexedReaderWriterState type RWST[F[_], -R, W, S, A] = ReaderWriterStateT[F, R, W, S, A] val RWST: ReaderWriterStateT.type = ReaderWriterStateT type RWS[-R, W, S, A] = ReaderWriterState[R, W, S, A] val RWS: ReaderWriterState.type = ReaderWriterState