Skip to content

Update docs #31

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
Oct 13, 2020
Merged
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
24 changes: 21 additions & 3 deletions src/Effect/Ref.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
-- | This module defines actions for working with mutable value references.
-- | This module defines the `Ref` type for mutable value references, as well
-- | as actions for working with them.
-- |
-- | _Note_: `Control.Monad.ST` provides a _safe_ alternative to `Ref` when
-- | You'll notice that all of the functions that operate on a `Ref` (e.g.
-- | `new`, `read`, `write`) return their result wrapped in an `Effect`.
-- | Working with mutable references is considered effectful in PureScript
-- | because of the principle of purity: functions should not have side
-- | effects, and should return the same result when called with the same
-- | arguments. If a `Ref` could be written to without using `Effect`, that
-- | would cause a side effect (the effect of changing the result of subsequent
-- | reads for that `Ref`). If there were a function for reading the current
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the effect of changing the result of subsequent reads for that Ref

Good simple and clear explanation. Well done!

-- | value of a `Ref` without the result being wrapped in `Effect`, the result
-- | of calling that function would change each time a new value was written to
-- | the `Ref`. Even creating a new `Ref` is effectful: if there were a
-- | function for creating a new `Ref` with the type `forall s. s -> Ref s`,
-- | then calling that function twice with the same argument would not give the
-- | same result in each case, since you'd end up with two distinct references
-- | which could be updated independently of each other.
-- |
-- | _Note_: `Control.Monad.ST` provides a pure alternative to `Ref` when
-- | mutation is restricted to a local scope.
module Effect.Ref
( Ref
Expand All @@ -25,7 +42,7 @@ type role Ref representational
-- | Create a new mutable reference containing the specified value.
foreign import new :: forall s. s -> Effect (Ref s)

-- | Read the current value of a mutable reference
-- | Read the current value of a mutable reference.
foreign import read :: forall s. Ref s -> Effect s

-- | Update the value of a mutable reference by applying a function
Expand All @@ -40,6 +57,7 @@ foreign import modifyImpl :: forall s b. (s -> { state :: s, value :: b }) -> Re
modify :: forall s. (s -> s) -> Ref s -> Effect s
modify f = modify' \s -> let s' = f s in { state: s', value: s' }

-- | A version of `modify` which does not return the updated value.
modify_ :: forall s. (s -> s) -> Ref s -> Effect Unit
modify_ f s = void $ modify f s

Expand Down