dev.atedeg.mdm.utils.monads

Type members

Types

type Action[Error, Event, Result] = ActionWithState[Error, Event, Result, Unit]

The same as an action with state but does not read an immutable state.

The same as an action with state but does not read an immutable state.

type ActionWithState[Error, Event, Result, State] = EitherT[EmittingT[Reading[State], Event], Error, Result]

An action that can read an immutable state of type State, emits one or more events of type Event and can either fail with an error of type Error or produces a value of type Result.

An action that can read an immutable state of type State, emits one or more events of type Event and can either fail with an error of type Error or produces a value of type Result.

type CanEmit[Emitted] = [M[_]] =>> Tell[M, List[Emitted]]

Signals that a method could emit elements of a given type which are accumulated in a list.

Signals that a method could emit elements of a given type which are accumulated in a list.

type CanRaise[Raised] = [M[_]] =>> Raise[M, Raised]

Signals that a method could fail with a given error type.

Signals that a method could fail with a given error type.

type CanRead[C] = [F[_]] =>> Ask[F, C]

Signals that a method can read a state C.

Signals that a method can read a state C.

type Emits[Emitted] = [M[_]] =>> Tell[M, List[Emitted]]

Signals that a method will always emit one or more elements of a given type which are accumulated in a list.

Signals that a method will always emit one or more elements of a given type which are accumulated in a list.

Note:

this check is not imposed neither at compile-time nor at run-time. This is just used to better document the behaviour of methods but does not guarantee any invariant.

type SafeAction[Event, Result] = Writer[List[Event], Result]

The same as an action but does not fail.

The same as an action but does not fail.

type SafeActionTwoEvents[Event1, Event2, Result] = WriterT[[A] =>> Writer[List[Event2], A], List[Event1], Result]

The same as a safe action but with two events.

The same as a safe action but with two events.

type ServerAction[Config, Error, Result] = EitherT[[A] =>> Kleisli[IO, Config, A], Error, Result]

An action which performs IO, reads an immutable state C and can either fail with an error E or produce a result R.

An action which performs IO, reads an immutable state C and can either fail with an error E or produce a result R.

Value members

Concrete methods

def emit[M[_], E](e: E)(using T: Tell[M, List[E]]): M[Unit]

Emits an element of type E in a context M[_] that accumulates emitted elements in a list.

Emits an element of type E in a context M[_] that accumulates emitted elements in a list.

def raise[M[_], E, A](e: E)(using R: Raise[M, E]): M[A]

Raises an error of type E in a context M[_] where the computation can be aborted.

Raises an error of type E in a context M[_] where the computation can be aborted.

def readState[C, M[_] : CanRead[C]](implicit evidence$1: Monad[M], evidence$2: CanRead[C][M], A: Ask[M, C]): M[C]

Reads the current global state in a context M[_] with a global state C.

Reads the current global state in a context M[_] with a global state C.

def readStateView[C, M[_] : CanRead[C], C1](f: C => C1): M[C1]

Gets a view of the current state applying a function f to it.

Gets a view of the current state applying a function f to it.

def unless[M[_], A](cond: => Boolean)(action: => M[A])(using M: Monad[M]): M[Unit]

unless(cond)(a) performs the monadic action a if the condition cond is false.

unless(cond)(a) performs the monadic action a if the condition cond is false.

def when[M[_], A](cond: => Boolean)(action: => M[A])(using M: Monad[M]): M[Unit]

when(cond)(a) performs the monadic action a if the condition cond holds true.

when(cond)(a) performs the monadic action a if the condition cond holds true.

Extensions

Extensions

extension [A](a: Option[A])
def ifMissingRaise[M[_], E](error: => E)(using R: Raise[M, E], M: Monad[M]): M[A]

opt.ifMissingRaise(err) raises the error err if opt is None, otherwise returns its value inside the context M[_].

opt.ifMissingRaise(err) raises the error err if opt is None, otherwise returns its value inside the context M[_].

extension [A](a: IO[A])
def liftIO[F[_]](using L: LiftIO[F]): F[A]
extension [A](a: => A)
def performSyncIO[F[_]](using L: LiftIO[F]): F[A]
extension [Event1, Event2, Result](action: SafeActionTwoEvents[Event1, Event2, Result])
def execute: (List[Event1], List[Event2], Result)
extension [Event, Result](action: SafeAction[Event, Result])
def execute: (List[Event], Result)
extension [Error, Event, Result, State](action: ActionWithState[Error, Event, Result, State])
def execute(state: State): (List[Event], Either[Error, Result])

a.execute(s) runs the action a with a state s returning all the emitted events and its return value.

a.execute(s) runs the action a with a state s returning all the emitted events and its return value.

extension [Error, Event, Result](action: Action[Error, Event, Result])
def execute: (List[Event], Either[Error, Result])

a.execute(s) runs the action a returning all the emitted events and its return value.

a.execute(s) runs the action a returning all the emitted events and its return value.

extension [T[_], A](as: T[A])
def forEachDo[M[_] : Monad, B](f: A => M[B]): M[Unit]

as.forEachDo(ma) performs the monadic action ma for each of the elements of as and discards the return values returning Unit in the context M[_].

as.forEachDo(ma) performs the monadic action ma for each of the elements of as and discards the return values returning Unit in the context M[_].

extension (condition: Boolean)
def otherwiseRaise[M[_], E](error: => E)(using R: Raise[M, E], M: Monad[M]): M[Boolean]

cond.otherwiseRaise(err) raises the error err if the condition cond is false.

cond.otherwiseRaise(err) raises the error err if the condition cond is false.

extension [A, B](e: Either[A, B])
def getOrRaise[M[_] : CanRaise[A]]: M[B]
extension [M[_], A](ma: M[A])
def andThen[B](mb: M[B]): M[B]

ma.andThen(mb) performs the monadic action ma, ignores its return value and then performs the monadic action mb.

ma.andThen(mb) performs the monadic action ma, ignores its return value and then performs the monadic action mb.

def ignore(implicit evidence$9: Monad[M]): M[Unit]

ma.void performs the monadic action ma then discards its return value returning Unit in the context M[_].

ma.void performs the monadic action ma then discards its return value returning Unit in the context M[_].

def thenReturn[B](b: B): M[B]

ma.thenReturn(b) performs the monadic action ma, ignores its return value and then returns the value b in the context M[_].

ma.thenReturn(b) performs the monadic action ma, ignores its return value and then returns the value b in the context M[_].

extension [Config, Error, Result](sa: ServerAction[Config, Error, Result])
def unsafeExecute(config: Config): Either[Error, Result]

sa.unsafeExecute(config) runs the server action with a configuration.

sa.unsafeExecute(config) runs the server action with a configuration.

Note:

Don't use it in production!