InMemoryMechanismRepository

core.repositories.InMemoryMechanismRepository
class InMemoryMechanismRepository[F[_]](state: Ref[F, Map[MechanismId, Mechanism]])(implicit evidence$1: Sync[F]) extends MechanismRepository[F]

InMemoryMechanismRepository is analogous to a Haskell stateful data structure that holds a Map within a monadic context. This class abstracts over an effect type F, which can be seen as a Haskell monad that supports side effects and state management.

Type parameters

F

The abstract effect type, which could be likened to an effectful monad in Haskell (e.g., IO, StateT). type MechanismRepository m = StateT (Map MechanismId Mechanism) m

Value parameters

state

Ref[F, Map[MechanismId, Mechanism]]

  • Ref in Scala is similar to IORef or MVar in Haskell, representing mutable state within a monad.
  • Map[MechanismId, Mechanism] represents an immutable key-value data structure, comparable to Data.Map in Haskell.
  • F[_]: Sync constraint in Scala corresponds to a Haskell MonadIO constraint, enabling us to manage effects in a functional way.

Attributes

Source
InMemoryMechanismRepository.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def create(mechanism: Mechanism): F[Either[MechanismError, Mechanism]]

Creates a new Mechanism entry, assigning it a unique identifier, and updates the state.

Creates a new Mechanism entry, assigning it a unique identifier, and updates the state.

Haskell equivalent signature:

create :: MonadIO m => Mechanism -> MechanismRepository (Map MechanismId Mechanism) m Mechanism

  • This function modifies the state, analogous to Haskell’s StateT monad transformer with modify.
  • state.modify here acts like modify in Haskell’s State monad, updating the map with the new Mechanism.
  • The copy method in Scala can be thought of as record syntax in Haskell, creating a new Mechanism with an updated id.

Attributes

Source
InMemoryMechanismRepository.scala
def delete(id: MechanismId): F[Boolean]

Deletes a Mechanism from the state by its identifier.

Deletes a Mechanism from the state by its identifier.

Equivalent Haskell signature:

delete :: MonadIO m => MechanismId -> MechanismRepository (Map MechanismId Mechanism) m Bool

  • The modify function again resembles Haskell’s StateT modify, allowing safe state updates within an effectful context.

Attributes

Source
InMemoryMechanismRepository.scala
def get(id: MechanismId): F[Option[Mechanism]]

Retrieves a Mechanism by its identifier.

Retrieves a Mechanism by its identifier.

This function’s signature in Haskell might look like:

get :: MonadIO m => Mechanism -> MechanismRepository m (Either MechanismError Mechanism)

  • The Option[Mechanism] is analogous to Maybe Mechanism in Haskell.
  • The monadic context F represents the effect type (like StateT or IO), enabling access to the mutable state Ref.

Attributes

Source
InMemoryMechanismRepository.scala