1+ -- | This module defines an effect and actions for working with
2+ -- | global mutable variables.
3+ -- |
4+ -- | _Note_: The `Control.Monad.ST` provides a _safe_ alternative
5+ -- | to global mutable variables when mutation is restricted to a
6+ -- | local scope.
7+
18module Control.Monad.Eff.Ref where
29
310import Control.Monad.Eff
411
12+ -- | The effect associated with the use of global mutable variables.
513foreign import data Ref :: !
614
15+ -- | A value of type `RefVal a` represents a mutable reference
16+ -- | which holds a value of type `a`.
717foreign import data RefVal :: * -> *
818
19+ -- | Create a new mutable reference containing the specified value.
920foreign import newRef " " "
1021 function newRef(val) {
1122 return function () {
@@ -14,6 +25,7 @@ foreign import newRef """
1425 }
1526" " " :: forall s r . s -> Eff (ref :: Ref | r ) (RefVal s )
1627
28+ -- | Read the current value of a mutable reference
1729foreign import readRef " " "
1830 function readRef(ref) {
1931 return function() {
@@ -22,7 +34,8 @@ foreign import readRef """
2234 }
2335" " " :: forall s r . RefVal s -> Eff (ref :: Ref | r ) s
2436
25-
37+ -- | Update the value of a mutable reference by applying a function
38+ -- | to the current value.
2639foreign import modifyRef' " " "
2740 function modifyRef$prime(ref) {
2841 return function(f) {
@@ -35,9 +48,12 @@ foreign import modifyRef' """
3548 }
3649" " " :: forall s b r . RefVal s -> (s -> { newState :: s , retVal :: b } ) -> Eff (ref :: Ref | r ) b
3750
51+ -- | Update the value of a mutable reference by applying a function
52+ -- | to the current value.
3853modifyRef :: forall s r . RefVal s -> (s -> s ) -> Eff (ref :: Ref | r ) Unit
3954modifyRef ref f = modifyRef' ref (\s -> {newState: f s, retVal: unit})
4055
56+ -- | Update the value of a mutable reference to the specified value.
4157foreign import writeRef " " "
4258 function writeRef(ref) {
4359 return function(val) {
0 commit comments