Live data from Hacker News

How to write PureScript react components to replace JavaScript

thomashoneyman.com

21–30 of 74 posts

Re: How to write PureScript react components to replace JavaScript

#21
post #13

Is PureScript nice to use again? Tried it a year or two ago and it relied heavily on Bower and I had a hard time getting anything to work on Windows - lots of Bower errors and such. I tried it a few years ago and got it to work, but in that case getting Halogen to compile the most basic app just stuck the compiler into a freeze for minutes, so I gave up. But I am not shitting on PureScript, it looks like a fantastic…

I'm also wondering the same thing, after the whole Elm meltdown I've been looking for something similar to learn on the side.

I'm not in the market myself at the moment, but I would consider Fable (F#) for that job.

Re: How to write PureScript react components to replace JavaScript

#22
The article says that PureScript helped them reduce bugs but fails to give any examples of how PureScript prevented bugs.

Looking at the examples, the code seems to function the same so it's not obvious what problem is being solved that warrants teaching a whole dev team a new language.

In my experience, the vast majority of React app bugs are 1) state management, or 2) asynchronousisty. How does PureScript help to prevent those two classes of bugs?

Re: How to write PureScript react components to replace JavaScript

#24
post #3

This is the corresponding code written in React using hooks: export const Counter = ({label, counterType, onClick}) => { const [counter, setCounter] = useState(0); return ( { counterType == "Increment" && setCounter(counter + 1); counterType == "Decrement" && setCounter(counter - 1); onClick && onClick(); }}> {label}: {counter} ); } I think the case for PureScript React would be more convincing if the code you ended…

FWIW here's a Purescript example from the `purescript-react-basic-hooks` homepage[1]. It's not 100% identical but it is fairly close.

  mkCounter :: Effect (ReactComponent {})
  mkCounter = do
    component "Counter" \props -> React.do
      counter /\ setCounter  show counter ]
          }
Your aesthetic / background may differ from mine, but I find the Purescript version easier to read.

[1]: https://github.com/spicydonuts/purescript-react-basic-hooks

Re: How to write PureScript react components to replace JavaScript

#25
post #3

This is the corresponding code written in React using hooks: export const Counter = ({label, counterType, onClick}) => { const [counter, setCounter] = useState(0); return ( { counterType == "Increment" && setCounter(counter + 1); counterType == "Decrement" && setCounter(counter - 1); onClick && onClick(); }}> {label}: {counter} ); } I think the case for PureScript React would be more convincing if the code you ended…

Did someone say concise? Here is a ClojureScript + Reagent example for whoever is interested in that:

  (defn Counter [{:keys [label counterType onClick]
                  :or {onClick (fn [])}}]
    (let [count (r/atom 0)]
      [:button
        {:onClick #(do (swap! count (condp = counterType
                                      :increment inc
                                      :decrement dec))
                       (onClick))}
        (str label ": " @count)])))

Re: How to write PureScript react components to replace JavaScript

#26
post #3

This is the corresponding code written in React using hooks: export const Counter = ({label, counterType, onClick}) => { const [counter, setCounter] = useState(0); return ( { counterType == "Increment" && setCounter(counter + 1); counterType == "Decrement" && setCounter(counter - 1); onClick && onClick(); }}> {label}: {counter} ); } I think the case for PureScript React would be more convincing if the code you ended…

Posting code on the internet - dangerous business!

I think the counter updates should use functional updates, since they depend on the current state. My understanding is that this can lead to a potential race condition, although I'm not 100% on the details.

  setCounter(currentCounter => currentCounter + 1)
https://reactjs.org/docs/hooks-reference.html#functional-upd...

Interestingly your code is similar to some the first code in the basic 'into to hooks' section of the docs. Perhaps it's not a huge issue? Maybe somebody else who knows a bit more about React internals can chime in.

Re: How to write PureScript react components to replace JavaScript

#27
post #13

Earlier quoted context omitted.

I'm also wondering the same thing, after the whole Elm meltdown I've been looking for something similar to learn on the side.

Elm meltdown?

I hate to be that guy but I expected this from the begining. When you make a language that can only does one thing (web programming) it's destined to go into obscurity. All maintream languages are multiple-purposes. Even javascript is, thesedays.

Re: How to write PureScript react components to replace JavaScript

#29
post #13

Is PureScript nice to use again? Tried it a year or two ago and it relied heavily on Bower and I had a hard time getting anything to work on Windows - lots of Bower errors and such. I tried it a few years ago and got it to work, but in that case getting Halogen to compile the most basic app just stuck the compiler into a freeze for minutes, so I gave up. But I am not shitting on PureScript, it looks like a fantastic…

I'm also wondering the same thing, after the whole Elm meltdown I've been looking for something similar to learn on the side.

I've been recently looking into some of the new WebAssembly based web frameworks, specifically yew for Rust[0], and I'm having a lot of fun with it. You get a lot of the same compiler guarantee's as Elm, with a familiar syntax if you're already used to writing Rust. It's still rough around the edges though.

[0]https://github.com/yewstack/yew

Re: How to write PureScript react components to replace JavaScript

#30

Is PureScript nice to use again? Tried it a year or two ago and it relied heavily on Bower and I had a hard time getting anything to work on Windows - lots of Bower errors and such. I tried it a few years ago and got it to work, but in that case getting Halogen to compile the most basic app just stuck the compiler into a freeze for minutes, so I gave up. But I am not shitting on PureScript, it looks like a fantastic…

Yes! The build tool to use is Spago (https://github.com/purescript/spago). It uses package sets which ensures all the packages you use are compatible with eachother. It is incredibly simple to use.

    spago init
    spago install halogen
    spago build
Halogen is now at v5 which has removed a lot of the complexity and type wierdness that was around in v4. Highly recommend giving it another spin!

Can't speak for how well it works on Windows though. Perhaps someone elso out there knows?

Post reply on HN