I'm quite new to Purescript and I am trying to process some user input (received from stdin) but my best approach is this one:
processInput :: String -> Effect Unit
processInput input = do
let
arr = split (Pattern " ") input
player1 = unsafeIndex arr 0
player2 = unsafeIndex arr 1
result = checkGame player1 player2
log $ "You typed: " <> player1 <> " " <> player2 <> ":" <> show (result)
lineHandler :: Interface -> String -> Effect Unit
lineHandler interface s = do
if s == "quit"
then do
close interface
else do
processInput s
prompt interface
main :: Effect Unit
main = do
interface <- createConsoleInterface noCompletion
setPrompt "> " interface
setLineHandler (lineHandler interface) interface
prompt interface
In which I use the Node.ReadLine package to create an interface, ask for input and process it in another method. This is meant for a rock-scissors-paper game and I managed to obtain the result of a single move and log it to the user. But I'm stuck at adding that result to a total outside the lineHandler function.
Is there any other way of obtaining string input from user at main scope (without a lineHandler)? Or is there a way I can define a parameter points which accumulates over the lineHandler executions?
I thought about State but I still don't understand it very well. Thanks in advance
If you want to structure your program as an
InterfacefromNode.ReadLine, you'll have to keep your game state (whatever it may be) in a mutable memory cell, which is represented by theReftype. You can create such cell with thenewfunction, and then read from and write to it with thereadandwritefunctions respectively.You'll have to create the memory cell in
mainand then tunnel it throughlineHandlertoprocessInput.