Skip to content

Commit da0bd72

Browse files
committed
Merge pull request #4 from garyb/generalise
Generalise from Aff to MonadAff
2 parents 63b015e + fe949ff commit da0bd72

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
.*
12
output
2-
.psci*
33
bower_components

docs/Control/Coroutine/Aff.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ produce :: forall a r eff. ((Either a r -> Eff (avar :: AVAR | eff) Unit) -> Eff
1414

1515
Create a `Producer` using an asynchronous callback.
1616

17-
The callback should provide zero or more values of type `a`, which will be
17+
The callback should provide zero or more values of type `a`, which will be
1818
emitted by the `Producer`, terminated by an optional value of type `r`. No values
1919
should be provided after a value of type `r` has been provided.
2020

@@ -28,4 +28,13 @@ produce \emit -> do
2828
emit (Right "finished")
2929
```
3030

31+
#### `produce'`
32+
33+
``` purescript
34+
produce' :: forall a r m eff. (Monad m, MonadAff (avar :: AVAR | eff) m) => ((Either a r -> Eff (avar :: AVAR | eff) Unit) -> Eff (avar :: AVAR | eff) Unit) -> Producer a m r
35+
```
36+
37+
A version of `produce` that creates a `Producer` with an underlying
38+
`MonadAff`, rather than `Aff` specifically.
39+
3140

src/Control/Coroutine/Aff.purs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import Prelude
1111
import Data.Either (Either())
1212

1313
import Control.Coroutine (Producer(), producer)
14-
import Control.Monad.Eff (Eff())
15-
import Control.Monad.Eff.Class (liftEff)
1614
import Control.Monad.Aff (Aff(), runAff)
1715
import Control.Monad.Aff.AVar (AVAR(), makeVar, takeVar, putVar)
16+
import Control.Monad.Aff.Class (MonadAff, liftAff)
17+
import Control.Monad.Eff (Eff())
18+
import Control.Monad.Eff.Class (liftEff)
19+
import Control.Monad.Free.Trans (hoistFreeT)
1820
import Control.Monad.Trans (lift)
1921

2022
-- | Create a `Producer` using an asynchronous callback.
@@ -32,8 +34,20 @@ import Control.Monad.Trans (lift)
3234
-- | log "Done!"
3335
-- | emit (Right "finished")
3436
-- | ```
35-
produce :: forall a r eff. ((Either a r -> Eff (avar :: AVAR | eff) Unit) -> Eff (avar :: AVAR | eff) Unit) -> Producer a (Aff (avar :: AVAR | eff)) r
36-
produce recv = do
37+
produce
38+
:: forall a r eff
39+
. ((Either a r -> Eff (avar :: AVAR | eff) Unit) -> Eff (avar :: AVAR | eff) Unit)
40+
-> Producer a (Aff (avar :: AVAR | eff)) r
41+
produce recv = hoistFreeT liftAff do
3742
v <- lift makeVar
3843
lift $ liftEff $ recv $ runAff (const (return unit)) return <<< putVar v
3944
producer (takeVar v)
45+
46+
-- | A version of `produce` that creates a `Producer` with an underlying
47+
-- | `MonadAff`, rather than `Aff` specifically.
48+
produce'
49+
:: forall a r m eff
50+
. (Monad m, MonadAff (avar :: AVAR | eff) m)
51+
=> ((Either a r -> Eff (avar :: AVAR | eff) Unit) -> Eff (avar :: AVAR | eff) Unit)
52+
-> Producer a m r
53+
produce' = hoistFreeT liftAff <<< produce

0 commit comments

Comments
 (0)