Skip to content
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

Update Var.purs #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: CI

jobs:
build:
runs-on: ubuntu-latest

steps:
# SETUP
- uses: actions/checkout@v2

- uses: thomashoneyman/setup-purescript@main
with:
purescript: "0.14.5"
spago: "0.20.3"
zephyr: "0.3.2"
purs-tidy: "0.6.4"

- name: Cache PureScript dependencies
uses: actions/cache@v2
with:
key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }}
path: |
.spago
output
- uses: actions/setup-node@v2
with:
node-version: "16.x"
cache: "npm"

# - name: Install NPM dependencies
# run: npm ci

- name: Build source
run: spago build

- name: Run tests
run: spago test --no-install

- name: Check formatting
run: purs-tidy check src test

# - name: Run bundle
# run: npm run bundle

# - name: Deploy to GitHub Pages
# uses: peaceiris/actions-gh-pages@v3
# with:
# github_token: ${{ secrets.GITHUB_TOKEN }}
# publish_dir: ./dist
# if: github.event_name == 'push' && github.ref == 'refs/heads/main'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bower_components/
output/
.spago/
.purs-repl
26 changes: 0 additions & 26 deletions .travis.yml

This file was deleted.

33 changes: 0 additions & 33 deletions bower.json

This file was deleted.

104 changes: 104 additions & 0 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{-
Welcome to your new Dhall package-set!

Below are instructions for how to edit this file for most use
cases, so that you don't need to know Dhall to use it.

## Use Cases

Most will want to do one or both of these options:
1. Override/Patch a package's dependency
2. Add a package not already in the default package set

This file will continue to work whether you use one or both options.
Instructions for each option are explained below.

### Overriding/Patching a package

Purpose:
- Change a package's dependency to a newer/older release than the
default package set's release
- Use your own modified version of some dependency that may
include new API, changed API, removed API by
using your custom git repo of the library rather than
the package set's repo

Syntax:
where `entityName` is one of the following:
- dependencies
- repo
- version
-------------------------------
let upstream = --
in upstream
with packageName.entityName = "new value"
-------------------------------

Example:
-------------------------------
let upstream = --
in upstream
with halogen.version = "master"
with halogen.repo = "https://example.com/path/to/git/repo.git"

with halogen-vdom.version = "v4.0.0"
with halogen-vdom.dependencies = [ "extra-dependency" ] # halogen-vdom.dependencies
-------------------------------

### Additions

Purpose:
- Add packages that aren't already included in the default package set

Syntax:
where `<version>` is:
- a tag (i.e. "v4.0.0")
- a branch (i.e. "master")
- commit hash (i.e. "701f3e44aafb1a6459281714858fadf2c4c2a977")
-------------------------------
let upstream = --
in upstream
with new-package-name =
{ dependencies =
[ "dependency1"
, "dependency2"
]
, repo =
"https://example.com/path/to/git/repo.git"
, version =
"<version>"
}
-------------------------------

Example:
-------------------------------
let upstream = --
in upstream
with benchotron =
{ dependencies =
[ "arrays"
, "exists"
, "profunctor"
, "strings"
, "quickcheck"
, "lcg"
, "transformers"
, "foldable-traversable"
, "exceptions"
, "node-fs"
, "node-buffer"
, "node-readline"
, "datetime"
, "now"
]
, repo =
"https://github.com/hdgarrood/purescript-benchotron.git"
, version =
"v7.0.0"
}
-------------------------------
-}
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.14.5-20220110/packages.dhall sha256:8dbf71bfc6c7a11043619eebe90ff85f7d884541048aa8cc48eef1ee781cbc0e

in upstream
14 changes: 14 additions & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ name = "var"
, dependencies = [
"console",
"effect",
"prelude",
"psci-support",
"contravariant",
"invariant",
"either",
"tuples"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
38 changes: 19 additions & 19 deletions src/Control/Monad/Eff/Var.purs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
-- | `Var`s allow to provide a uniform read/write access to the references in
-- | the `Eff` monad. This is mostly useful when making low-level FFI bindings.
-- | the `Effect` monad. This is mostly useful when making low-level FFI bindings.

-- | For example we might have some global counter with the following API:
-- | ```purescript
-- | foreign import data COUNT :: !
-- | getCounter :: forall eff. Eff (count :: COUNT | eff) Int
-- | setCounter :: forall eff. Int -> Eff (count :: COUNT | eff) Unit
-- | getCounter :: Effect Int
-- | setCounter :: Int -> Effect Unit
-- | ```
-- |
-- | `getCounter` and `setCounter` can be kept together in a `Var`:
-- | ```purescript
-- | counter :: forall eff. Var (count :: COUNT | eff) Int
-- | counter :: Var Int
-- | counter = makeVar getCounter setCounter
-- | ```
-- |
Expand Down Expand Up @@ -42,7 +41,7 @@ module Effect.Var
, makeSettableVar
) where

import Prelude
import Prelude

import Effect (Effect)
import Data.Decidable (class Decidable)
Expand Down Expand Up @@ -74,31 +73,32 @@ infixr 2 update as $~

-- | Read/Write var which holds a value of type `a` and produces effects `eff`
-- | when read or written.
newtype Var a
= Var { gettable :: GettableVar a
, settable :: SettableVar a
}
newtype Var a = Var
{ gettable :: GettableVar a
, settable :: SettableVar a
}

-- | Create a `Var` from getter and setter.
makeVar :: forall a. Effect a -> (a -> Effect Unit) -> Var a
makeVar g s = Var { gettable, settable }
where
gettable = makeGettableVar g
settable = makeSettableVar s
gettable = makeGettableVar g
settable = makeSettableVar s

instance settableVar :: Settable Var a where
set (Var { settable } ) = set settable
set (Var { settable }) = set settable

instance gettableVar :: Gettable Var a where
get (Var { gettable }) = get gettable

instance updatableVar :: Updatable Var a where
update v f = get v >>= f >>> set v

instance invariantVar :: Invariant Var where
imap ab ba (Var v) = Var { gettable: ab <$> v.gettable
, settable: ba >$< v.settable
}
instance invariantVar :: Invariant Var where
imap ab ba (Var v) = Var
{ gettable: ab <$> v.gettable
, settable: ba >$< v.settable
}

-- | Read-only var which holds a value of type `a` and produces effects `eff`
-- | when read.
Expand Down Expand Up @@ -128,7 +128,7 @@ newtype SettableVar a = SettableVar (a -> Effect Unit)
makeSettableVar :: forall a. (a -> Effect Unit) -> SettableVar a
makeSettableVar = SettableVar

instance settableSettableVar :: Settable SettableVar a where
instance settableSettableVar :: Settable SettableVar a where
set (SettableVar action) = action

instance contravariantSettableVar :: Contravariant SettableVar where
Expand All @@ -148,5 +148,5 @@ instance decideSettableVar :: Decide SettableVar where
choose f (SettableVar setb) (SettableVar setc) = SettableVar (either setb setc <<< f)

instance decidableSettableVar :: Decidable SettableVar where
-- lose :: forall a. (a -> Void) -> f a
-- lose :: forall a. (a -> Void) -> f a
lose f = SettableVar (absurd <<< f)
6 changes: 3 additions & 3 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ counter = makeVar getCounter setCounter

main :: Effect Unit
main = do
counter $= 0 -- set counter to 0
counter $= 0 -- set counter to 0
get counter >>= print -- => 0
counter $= 2 -- set counter to 2
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 a. Show a => a -> Effect Unit
Expand Down