Skip to content

add some examples and update comments and README #19

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
Sep 2, 2018
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ install:
- bower install
script:
- npm run -s build
- npm test
after_success:
- >-
test $TRAVIS_TAG &&
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,44 @@
[![Latest release](http://img.shields.io/github/release/purescript/purescript-refs.svg)](https://github.com/purescript/purescript-refs/releases)
[![Build status](https://travis-ci.org/purescript/purescript-refs.svg?branch=master)](https://travis-ci.org/purescript/purescript-refs)

Mutable value references.
This module defines functions for working with mutable value references.

_Note_: [`Control.Monad.ST`](https://pursuit.purescript.org/packages/purescript-st/4.0.0/docs/Control.Monad.ST) provides a _safe_ alternative to `Ref` when mutation is restricted to a local scope.

## Installation

```
bower install purescript-refs
```

## Example

```purs
main = do
-- initialize a new Ref with the value 0
ref <- Ref.new 0

-- read from it and check it
curr1 <- Ref.read ref
assertEqual { actual: curr1, expected: 0 }

-- write over the ref with 1
Ref.write 1 ref

-- now it is 1 when we read out the value
curr2 <- Ref.read ref
assertEqual { actual: curr2, expected: 1 }

-- modify it by adding 1 to the current state
Ref.modify_ (\s -> s + 1) ref

-- now it is 2 when we read out the value
curr3 <- Ref.read ref
assertEqual { actual: curr3, expected: 2 }
```

See [tests](test/Main.purs) to see usages.

## Documentation

Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-refs).
3 changes: 3 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"dependencies": {
"purescript-effect": "^2.0.0",
"purescript-prelude": "^4.0.0"
},
"devDependencies": {
"purescript-assert": "^4.0.0"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"test": "pulp test",
"build": "eslint src && pulp build -- --censor-lib --strict"
},
"devDependencies": {
Expand Down
8 changes: 3 additions & 5 deletions src/Effect/Ref.purs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
-- | This module defines an effect and actions for working with
-- | global mutable variables.
-- | This module defines actions for working with mutable value references.
-- |
-- | _Note_: `Control.Monad.ST` provides a _safe_ alternative
-- | to global mutable variables when mutation is restricted to a
-- | local scope.
-- | _Note_: `Control.Monad.ST` provides a _safe_ alternative to `Ref` when
-- | mutation is restricted to a local scope.
module Effect.Ref where

import Prelude
Expand Down
30 changes: 30 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Test.Main where

import Prelude

import Effect (Effect)
import Effect.Ref as Ref
import Test.Assert (assertEqual)

main :: Effect Unit
main = do
-- initialize a new Ref with the value 0
ref <- Ref.new 0

-- read from it and check it
curr1 <- Ref.read ref
assertEqual { actual: curr1, expected: 0 }

-- write over the ref with 1
Ref.write 1 ref

-- now it is 1 when we read out the value
curr2 <- Ref.read ref
assertEqual { actual: curr2, expected: 1 }

-- modify it by adding 1 to the current state
Ref.modify_ (\s -> s + 1) ref

-- now it is 2 when we read out the value
curr3 <- Ref.read ref
assertEqual { actual: curr3, expected: 2 }