Skip to content

Made it work for psc 0.9.1 including test suite #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
],
"keywords": [ "purescript" ],
"dependencies": {
"purescript-eff": "^0.1.2",
"purescript-contravariant": "^0.2.3",
"purescript-invariant": "^0.3.0"
"purescript-eff": "^1.0.0",
"purescript-contravariant": "^1.0.0",
"purescript-invariant": "^1.0.0"
},
"devDependencies": {
"purescript-console": "^0.1.1"
"purescript-console": "^1.0.0"
}
}
38 changes: 18 additions & 20 deletions src/Control/Monad/Eff/Var.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
-- | ```

module Control.Monad.Eff.Var
( Gettable
( class Gettable
, get
, Settable
, class Settable
, set
, ($=)
, Updatable
, class Updatable
, update
, ($~)
, Var()
Expand All @@ -44,13 +44,16 @@ module Control.Monad.Eff.Var

import Prelude

import Control.Monad.Eff
import Control.Monad.Eff (Eff)
import Data.Decidable (class Decidable)
import Data.Decide (class Decide)
import Data.Divide (class Divide)
import Data.Divisible (class Divisible)
import Data.Tuple (Tuple(..))
import Data.Either (either)
import Data.Functor.Contravariant (class Contravariant, (>$<))
import Data.Functor.Invariant (class Invariant)

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.
class Gettable (eff :: # !) (var :: * -> *) (a :: *) where
Expand All @@ -61,20 +64,14 @@ class Settable (eff :: # !) (var :: * -> *) (a :: *) where
set :: var a -> a -> Eff eff Unit

-- | Alias for `set`.
infixr 2 $=
($=) :: forall eff var a. (Settable eff var a)
=> var a -> a -> Eff eff Unit
($=) = set
infixr 2 set as $=

-- | Typeclass for vars that can be updated.
class Updatable (eff :: # !) (var :: * -> *) (a :: *) where
update :: var a -> (a -> a) -> Eff eff Unit

-- | Alias for `get`
infixr 2 $~
($~) :: forall eff var a. (Updatable eff var a)
=> var a -> (a -> a) -> Eff eff Unit
($~) = update
infixr 2 update as $~

-- | Read/Write var which holds a value of type `a` and produces effects `eff`
-- | when read or written.
Expand Down Expand Up @@ -138,10 +135,11 @@ instance divideSettableVar :: Divide (SettableVar eff) where
setc c

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

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

instance decidableSettableVar :: Decidable (SettableVar eff) where
lose = SettableVar
-- lose :: forall a. (a -> Void) -> f a
lose f = SettableVar (absurd <<< f)
6 changes: 4 additions & 2 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Test.Main where

import Prelude

import Control.Monad.Eff
import Control.Monad.Eff.Var
import Control.Monad.Eff.Console
Expand All @@ -18,5 +17,8 @@ main = do
get counter >>= print -- => 0
counter $= 2 -- set counter to 2
get counter >>= print -- => 2
counter $~ (* 5) -- multiply counter by 5
counter $~ (_ * 5) -- multiply counter by 5
get counter >>= print -- => 10

print :: forall eff a. Show a => a -> Eff (console :: CONSOLE | eff) Unit
print = log <<< show