Module

Control.Monad.Eff.Var

Package
purescript-var
Repository
zudov/purescript-var

Vars allow to provide a uniform read/write access to the references in the Eff monad. This is mostly useful when making low-level FFI bindings. For example we might have some global counter with the following API:

foreign import data COUNT :: !
getCounter :: forall eff. Eff (count :: COUNT | eff) Int
setCounter :: forall eff. Int -> Eff (count :: COUNT | eff) Unit

getCounter and setCounter can be kept together in a Var:

counter :: forall eff. Var (count :: COUNT | eff) Int
counter = makeVar getCounter setCounter

counter can be used in this way:

main = do
  counter $= 0          -- set counter to 0
  get counter >>= print -- => 0
  counter $= 2          -- set counter to 2
  get counter >>= print -- => 2
  counter $~ (* 5)      -- multiply counter by 5
  get counter >>= print -- => 10

#Gettable Source

class Gettable (eff :: Row Effect) (var :: Type -> Type) (a :: Type) | var -> a, var -> eff where

Typeclass for vars that can be read.

Members

Instances

#Settable Source

class Settable (eff :: Row Effect) (var :: Type -> Type) (a :: Type) | var -> a, var -> eff where

Typeclass for vars that can be written.

Members

Instances

#($=) Source

Operator alias for Control.Monad.Eff.Var.set (right-associative / precedence 2)

Alias for set.

#Updatable Source

class Updatable (eff :: Row Effect) (var :: Type -> Type) (a :: Type) | var -> a, var -> eff where

Typeclass for vars that can be updated.

Members

Instances

#($~) Source

Operator alias for Control.Monad.Eff.Var.update (right-associative / precedence 2)

Alias for get

#Var Source

newtype Var (eff :: Row Effect) a

Read/Write var which holds a value of type a and produces effects eff when read or written.

Instances

#makeVar Source

makeVar :: forall a eff. Eff eff a -> (a -> Eff eff Unit) -> Var eff a

Create a Var from getter and setter.

#GettableVar Source

newtype GettableVar eff a

Read-only var which holds a value of type a and produces effects eff when read.

Instances

#makeGettableVar Source

makeGettableVar :: forall a eff. Eff eff a -> GettableVar eff a

Create a GettableVar from getter.

#SettableVar Source

newtype SettableVar eff a

Write-only var which holds a value of type a and produces effects eff when written.

Instances

#makeSettableVar Source

makeSettableVar :: forall a eff. (a -> Eff eff Unit) -> SettableVar eff a

Create a SettableVar from setter.