|
1 | 1 | module Control.Monad.Eff.Ref where
|
2 | 2 |
|
3 | 3 | import Control.Monad.Eff
|
| 4 | +import Data.Tuple |
4 | 5 |
|
5 | 6 | foreign import data Ref :: !
|
6 | 7 |
|
7 | 8 | foreign import data RefVal :: * -> *
|
8 | 9 |
|
9 |
| -foreign import newRef |
10 |
| - "function newRef(val) {\ |
11 |
| - \ return function () {\ |
12 |
| - \ return { value: val };\ |
13 |
| - \ };\ |
14 |
| - \}" :: forall s r. s -> Eff (ref :: Ref | r) (RefVal s) |
15 |
| - |
16 |
| -foreign import readRef |
17 |
| - "function readRef(ref) {\ |
18 |
| - \ return function() {\ |
19 |
| - \ return ref.value;\ |
20 |
| - \ };\ |
21 |
| - \}" :: forall s r. RefVal s -> Eff (ref :: Ref | r) s |
22 |
| - |
23 |
| - |
24 |
| -foreign import modifyRef |
25 |
| - "function modifyRef(ref) {\ |
26 |
| - \ return function(f) {\ |
27 |
| - \ return function() {\ |
28 |
| - \ ref.value = f(ref.value);\ |
29 |
| - \ return {};\ |
30 |
| - \ };\ |
31 |
| - \ };\ |
32 |
| - \}" :: forall s r. RefVal s -> (s -> s) -> Eff (ref :: Ref | r) Unit |
33 |
| - |
34 |
| -foreign import writeRef |
35 |
| - "function writeRef(ref) {\ |
36 |
| - \ return function(val) {\ |
37 |
| - \ return function() {\ |
38 |
| - \ ref.value = val;\ |
39 |
| - \ return {};\ |
40 |
| - \ };\ |
41 |
| - \ };\ |
42 |
| - \}" :: forall s r. RefVal s -> s -> Eff (ref :: Ref | r) Unit |
| 10 | +foreign import newRef """ |
| 11 | + function newRef(val) { |
| 12 | + return function () { |
| 13 | + return { value: val }; |
| 14 | + }; |
| 15 | + } |
| 16 | +""" :: forall s r. s -> Eff (ref :: Ref | r) (RefVal s) |
| 17 | + |
| 18 | +foreign import readRef """ |
| 19 | + function readRef(ref) { |
| 20 | + return function() { |
| 21 | + return ref.value; |
| 22 | + }; |
| 23 | + } |
| 24 | +""" :: forall s r. RefVal s -> Eff (ref :: Ref | r) s |
| 25 | + |
| 26 | + |
| 27 | +foreign import modifyRef' """ |
| 28 | + function modifyRef$prime(ref) { |
| 29 | + return function(f) { |
| 30 | + return function() { |
| 31 | + var t = f(ref.value); |
| 32 | + ref.value = t.value0; |
| 33 | + return t.value1; |
| 34 | + }; |
| 35 | + }; |
| 36 | + } |
| 37 | +""" :: forall s b r. RefVal s -> (s -> Tuple s b) -> Eff (ref :: Ref | r) b |
| 38 | + |
| 39 | +modifyRef :: forall s r. RefVal s -> (s -> s) -> Eff (ref :: Ref | r) Unit |
| 40 | +modifyRef ref f = modifyRef' ref (\s -> Tuple (f s) unit) |
| 41 | + |
| 42 | +foreign import writeRef """ |
| 43 | + function writeRef(ref) { |
| 44 | + return function(val) { |
| 45 | + return function() { |
| 46 | + ref.value = val; |
| 47 | + return {}; |
| 48 | + }; |
| 49 | + }; |
| 50 | + } |
| 51 | +""" :: forall s r. RefVal s -> s -> Eff (ref :: Ref | r) Unit |
0 commit comments