Giter VIP home page Giter VIP logo

purescript-refs's Introduction

purescript-refs

Latest release Build status Pursuit

This module defines functions for working with mutable value references.

Note: Control.Monad.ST provides a safe alternative to Ref when mutation is restricted to a local scope.

Installation

spago install refs

Example

import Effect.Ref as Ref

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 to see usages.

Documentation

Module documentation is published on Pursuit.

purescript-refs's People

Contributors

garyb avatar hdgarrood avatar jordanmartinez avatar justinwoo avatar kl0tl avatar kritzcreek avatar michaelxavier avatar paf31 avatar telser avatar thomashoneyman avatar triallax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

purescript-refs's Issues

Expose EffectFn variants

I think it would be good to export uncurried variants of the operations for cases where we want to forgo the overhead of currying.

Return value from writeRef and modifyRef

Preempted by purescript/purescript-st#4:

modifySTRef :: forall a h r. STRef h a -> (a -> a) -> Eff (st :: ST h | r) a
modifyRef   :: forall s   r.   Ref   s -> (s -> s) -> Eff (ref :: REF | r) Unit

In particular the current behavior of modifyRef' strangely returns an arbitrary value not what's stored.

Operators for writeRef and modifyRef

Mimicing Lens:

(.=) = writeRef
(%=) = modifyRef
(%%=) = modifyRef`

In practice looks like:

nextEvent counter payload = do
  count <- getRef counter
  counter %= (_ + 1)
  pure (Event (payload {id = count}))

Just wondered if it might be generally useful?

Is `newRef` actually effectful?

I've bumped into a few cases where it would be really handy to be able to construct a Ref outside of Eff - it seems to me that reading and writing a ref are definitely effectful, but creating one isn't necessarily so. Thoughts?

Do you see value in a Ref-Monad like State?

I am using purescript for a canvas-based web app and don't need any fancy UI library. I end up with a lean, global application state and deal with events quite bare-footed.

I find myself writing

app <- read refAppState
flip write refAppState $
    app { foo = newFoo
        , bar = app.bar + 1
        }

quite a lot.

If you don't object, I'll come up with a pull request soon to allow to write the above via a monad for effectfull, global state:

foo .= newFoo
bar += 1

I miss the lenses :)

Also, maybe you have a suggestion for the module name.

How about Effect.Ref.Monad? Alternatively Control.Monad.State.Global.

Eq instance?

I think it wouldn't be hard to do an Eq instance for Ref right? Would you be open to a pull request?

Expand newWithSelf documentation

The function passed to newWithSelf should not read or write to the reference it's passed, though it may create closures that will eventually do so. This restriction is not documented.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.