by diggan · 6y ago · 0 comments
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)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 ... (_ + 1)
Equivalent to this JS function: x => x + 1
Operator Sections: https://github.com/purescript/documentation/blob/master/lang...