Skip to content

Commit

Permalink
Merge pull request #1 from paf31/apply-divide
Browse files Browse the repository at this point in the history
Apply and Divide instances
  • Loading branch information
zudov committed Nov 18, 2015
2 parents 32ed454 + 8026b3e commit 46b4e20
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
32 changes: 19 additions & 13 deletions docs/Control/Monad/Eff/Var.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Typeclass for vars that can be read.

##### Instances
``` purescript
instance gettableVar :: Gettable eff (Var eff) a
instance gettableGettableVar :: Gettable eff (GettableVar eff) a
Gettable eff (Var eff) a
Gettable eff (GettableVar eff) a
```

#### `Settable`
Expand All @@ -52,8 +52,8 @@ Typeclass for vars that can be written.

##### Instances
``` purescript
instance settableVar :: Settable eff (Var eff) a
instance settableSettableVar :: Settable eff (SettableVar eff) a
Settable eff (Var eff) a
Settable eff (SettableVar eff) a
```

#### `($=)`
Expand All @@ -75,7 +75,7 @@ Typeclass for vars that can be updated.

##### Instances
``` purescript
instance updatableVar :: Updatable eff (Var eff) a
Updatable eff (Var eff) a
```

#### `($~)`
Expand All @@ -97,10 +97,10 @@ when read or written.

##### Instances
``` purescript
instance settableVar :: Settable eff (Var eff) a
instance gettableVar :: Gettable eff (Var eff) a
instance updatableVar :: Updatable eff (Var eff) a
instance invariantVar :: Invariant (Var eff)
Settable eff (Var eff) a
Gettable eff (Var eff) a
Updatable eff (Var eff) a
Invariant (Var eff)
```

#### `makeVar`
Expand All @@ -122,8 +122,10 @@ when read.

##### Instances
``` purescript
instance gettableGettableVar :: Gettable eff (GettableVar eff) a
instance functorGettableVar :: Functor (GettableVar eff)
Gettable eff (GettableVar eff) a
Functor (GettableVar eff)
Apply (GettableVar eff)
Applicative (GettableVar eff)
```

#### `makeGettableVar`
Expand All @@ -145,8 +147,12 @@ when written.

##### Instances
``` purescript
instance settableSettableVar :: Settable eff (SettableVar eff) a
instance contravariantSettableVar :: Contravariant (SettableVar eff)
Settable eff (SettableVar eff) a
Contravariant (SettableVar eff)
Divide (SettableVar eff)
Divisible (SettableVar eff)
Decide (SettableVar eff)
Decidable (SettableVar eff)
```

#### `makeSettableVar`
Expand Down
26 changes: 26 additions & 0 deletions src/Control/Monad/Eff/Var.purs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ module Control.Monad.Eff.Var
import Prelude

import Control.Monad.Eff

import Data.Tuple
import Data.Either
import Data.Functor.Contravariant
import Data.Functor.Contravariant.Divisible
import Data.Functor.Invariant

-- | Typeclass for vars that can be read.
Expand Down Expand Up @@ -106,6 +110,12 @@ instance gettableGettableVar :: Gettable eff (GettableVar eff) a where
instance functorGettableVar :: Functor (GettableVar eff) where
map f (GettableVar a) = GettableVar (f <$> a)

instance applyGettableVar :: Apply (GettableVar eff) where
apply (GettableVar f) (GettableVar a) = GettableVar (apply f a)

instance applicativeGettableVar :: Applicative (GettableVar eff) where
pure = GettableVar <<< pure

-- | Write-only var which holds a value of type `a` and produces effects `eff`
-- | when written.
newtype SettableVar eff a = SettableVar (a -> Eff eff Unit)
Expand All @@ -119,3 +129,19 @@ instance settableSettableVar :: Settable eff (SettableVar eff) a where

instance contravariantSettableVar :: Contravariant (SettableVar eff) where
cmap f (SettableVar a) = SettableVar (a <<< f)

instance divideSettableVar :: Divide (SettableVar eff) where
divide f (SettableVar setb) (SettableVar setc) = SettableVar \a ->
case f a of
Tuple b c -> do
setb b
setc c

instance divisibleSettableVar :: Divisible (SettableVar eff) where
conquer = SettableVar \_ -> return unit

instance decideSettableVar :: Decide (SettableVar eff) where
decide f (SettableVar setb) (SettableVar setc) = SettableVar (either setb setc <<< f)

instance decidableSettableVar :: Decidable (SettableVar eff) where
lose = SettableVar

0 comments on commit 46b4e20

Please sign in to comment.