3 ms·
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 (Re
by noelwelsh 6y ago
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 <- useState 0
pure
$ R.button
{ onClick: handler_ do
setCounter (_ + 1)
, children:
[ R.text $ "Increment: " <> 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 https://github.com/spicydonuts/purescript-react-basic-hooks
- diggan 6y agoNot sure I misunderstand the code that you're posting, but seems to not have the whole increment vs decrements parts in it, which would be interesting to see.
- TexInWien 6y agoThe following line defines a function (the syntax is known as an 'operator section'): (_ + 1) Equivalent to this JS function: x => x + 1 Operator Sections: https://github.com/purescript/documentation/blob/master/language/Differences-from-Haskell.md#operator-sections https://github.com/purescript/documentation/blob/master/lang...
- rkangel 6y agoI think the parent's point is that it only includes increment and not decrement.
- noelwelsh 6y agoYou are correct, which is why I said "[I]t's not 100% identical". Although I can read Purescript reasonably well I'm not so proficient writing it (I do most of my programming in a different FP language, so I understand the concepts but not the specifics.) I didn't attempt to modify the code I pulled from the example because it would take more time than I have to install Purescript and check my modifications. It would be simple to add this functionality. This equivalent of the `counterType` would be an algebraic data type, defined looking something like data counterType = Increment | Decrement and usage something like case counterType of Increment -> ... make increment here ... Decrement -> ... make decrement here ...
- lalaithion 6y agoThis is a closer comparison. mkCounter :: String -> (Int -> Int) -> Effect (ReactComponent {}) mkCounter label op = do component "Counter" \props -> React.do counter /\ setCounter <- useState 0 pure $ R.button { onClick: handler_ do setCounter op , children: [ R.text $ label <> ": " <> show counter ] } now you can call mkCounter "Increment" (_ + 1)