Home:ALL Converter>Shared state in functional application

Shared state in functional application

Ask Time:2018-02-17T02:42:46         Author:Wes

Json Formatter

Okay I'm trying to understand how to do programming without variables.

I've managed a simple tic-tac-toe game but it depended on a tail recursive state and as far as I can tell would only work in a single thread. Example can be seen at https://codereview.stackexchange.com/questions/187339/scala-tic-tac-toe-no-mutable-state

Now if I was to convert the same code to be either a webapp or even a gui program I can't see how the state could be shared across threads without having a mutable reference at the top level.

Even ScalaFX uses a variable for its PrimaryStage which I suspect is to provide such a top level reference.

How would one go about using shared state between two or more consumers without using variables? For instance tic tac toe as above or a web application that accepted posts of text and had an endpoint stating how many times an individual word was posted.

related: Java: state sharing between threads in functional programming

Functional way to implement a thread safe shared counter

However the solutions to this all seem to require some mutable variable an atomic integer in one and STM in the other.

Is there a standard pattern for this type of work?

After reading How can you do anything useful without mutable state? and How Functional Programming addresses concurrent increment/decrement operations invoked by different users I am pretty sure that this is not possible. There does need to be a reference to mutable state.

If this is the case how can the mutable state be minimised?

What patterns do people use for scala in web applications with internal state?

Author:Wes,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48833026/shared-state-in-functional-application
yy