Skip to content

Commit

Permalink
Updates for 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
paf31 committed Jun 6, 2015
1 parent cc084e1 commit 36270ad
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
35 changes: 35 additions & 0 deletions src/Control/Monad/Eff/Ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* global exports */
"use strict";

// module Control.Monad.Eff.Ref

exports.newRef = function(val) {
return function () {
return { value: val };
};
};

exports.readRef = function(ref) {
return function() {
return ref.value;
};
};

exports.modifyRef$prime = function(ref) {
return function(f) {
return function() {
var t = f(ref.value);
ref.value = t.state;
return t.value;
};
};
};

exports.writeRef = function(ref) {
return function(val) {
return function() {
ref.value = val;
return {};
};
};
};
45 changes: 6 additions & 39 deletions src/Control/Monad/Eff/Ref.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module Control.Monad.Eff.Ref where

import Prelude

import Control.Monad.Eff (Eff())

-- | The effect associated with the use of global mutable variables.
Expand All @@ -17,54 +19,19 @@ foreign import data REF :: !
foreign import data Ref :: * -> *

-- | Create a new mutable reference containing the specified value.
foreign import newRef
"""
function newRef(val) {
return function () {
return { value: val };
};
}
""" :: forall s r. s -> Eff (ref :: REF | r) (Ref s)
foreign import newRef :: forall s r. s -> Eff (ref :: REF | r) (Ref s)

-- | Read the current value of a mutable reference
foreign import readRef
"""
function readRef(ref) {
return function() {
return ref.value;
};
}
""" :: forall s r. Ref s -> Eff (ref :: REF | r) s
foreign import readRef :: forall s r. Ref s -> Eff (ref :: REF | r) s

-- | Update the value of a mutable reference by applying a function
-- | to the current value.
foreign import modifyRef'
"""
function modifyRef$prime(ref) {
return function(f) {
return function() {
var t = f(ref.value);
ref.value = t.state;
return t.value;
};
};
}
""" :: forall s b r. Ref s -> (s -> { state :: s, value :: b }) -> Eff (ref :: REF | r) b
foreign import modifyRef' :: forall s b r. Ref s -> (s -> { state :: s, value :: b }) -> Eff (ref :: REF | r) b

-- | Update the value of a mutable reference by applying a function
-- | to the current value.
modifyRef :: forall s r. Ref s -> (s -> s) -> Eff (ref :: REF | r) Unit
modifyRef ref f = modifyRef' ref (\s -> { state: f s, value: unit })

-- | Update the value of a mutable reference to the specified value.
foreign import writeRef
"""
function writeRef(ref) {
return function(val) {
return function() {
ref.value = val;
return {};
};
};
}
""" :: forall s r. Ref s -> s -> Eff (ref :: REF | r) Unit
foreign import writeRef :: forall s r. Ref s -> s -> Eff (ref :: REF | r) Unit
12 changes: 6 additions & 6 deletions src/Control/Monad/Eff/Ref/Unsafe.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

module Control.Monad.Eff.Ref.Unsafe where

import Prelude

import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Ref

import Control.Monad.Eff.Unsafe (unsafeInterleaveEff)

-- | This handler function unsafely removes the `Ref` effect from an
-- | effectful action.
-- |
-- | This function might be used when it is impossible to prove to the
-- | typechecker that a particular mutable reference does not escape
-- | its scope.
foreign import unsafeRunRef
"""
function unsafeRunRef(f) {
return f;
}
""" :: forall eff a. Eff (ref :: REF | eff) a -> Eff eff a
unsafeRunRef :: forall eff a. Eff (ref :: REF | eff) a -> Eff eff a
unsafeRunRef = unsafeInterleaveEff

0 comments on commit 36270ad

Please sign in to comment.