Giter Site home page Giter Site logo

Comments (6)

JordanMartinez avatar JordanMartinez commented on June 30, 2024

from purescript-httpure.

easafe avatar easafe commented on June 30, 2024

I thought about that @JordanMartinez. But it seemed like such a common use case, I wondered if I was missing a more "pure" way to do it.

from purescript-httpure.

JordanMartinez avatar JordanMartinez commented on June 30, 2024

Since the state monad is just a boilerplate-free way to thread immutable pure state through a computation...

this :: MonadState Int m => m String
this = do
  original <- get
  put (original + 1)
  pure "done"

sameThing :: Monad m => Int -> m (Tuple Int String)
sameThing initialValue = do
  let original = initialValue
  let new = original + 1
  pure (Tuple new "done")

So, if you wrote a handler that uses the state monad, the problem is with the initial state value. Each time a request is made, the same initial state would be used in the resulting state monad computation. Using a counter as an example, the initial count would always start at 0 and be 1 at the end of the response. The next time a request comes in, it's back to 0, not 1.
If you wanted to track state across requests, you need to use mutable state. Your options are either ST (local) or Ref (global). Since ST doesn't work in this situation, you're only real option is Ref.

from purescript-httpure.

cprussin avatar cprussin commented on June 30, 2024

Currently, I don't think there would be any way to use something like a StateT monad to thread mutable server state into your router. But as @JordanMartinez mentions, you can certainly use a Ref.

I'm not opposed to thinking about ways to make one of those other monad transformerss usable for this kind of situation (which would probably match the solution for #134). That said, I typically structure my apps following something very close to https://www.fpcomplete.com/blog/2017/06/readert-design-pattern. That article makes very compelling arguments why Ref is actually more appropriate for global app state. Presumably your app is your server, so server state is a synonym for global app state.

from purescript-httpure.

easafe avatar easafe commented on June 30, 2024

@cprussin would you say the remarks about exceptions apply to PureScript as well?

from purescript-httpure.

cprussin avatar cprussin commented on June 30, 2024

wow this is a very late reply but somehow I missed your message @easafe . Yes I think the same logic for exceptions applies to purescript since the Effect monad in purescript has the same exception contract as IO (that is, anything running in it has an implicit assumption that it could throw an exception).

from purescript-httpure.

Related Issues (20)

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.