Consider an algorithm with a while
loop (or recursion) with the stopping condition checked after each iteration. The stopping parameters must be changeable from the UI during the algorithm's run.
Here’s a toy example in abridged Scala:
val cInit = CellSink(2.0)
//Initial value: textfield
val cCond = CellSink(100.0)
//Stopping parameter: textfield
val sRun = StreamSink[Unit]
//Starts the algorithm: button
//squaring function ("the algorithm"):
val f = (a: Dbl, b: Dbl) => {
def go(x: Dbl)= if (x>b) x
else go(x*x)
go(a)}
val cRes = sRun.snapshot(cInit, cCond, f).hold(NaN)
//label with the result
As it is, both cInit
and cCond
are fixed per run of the algorithm. How could the cCond
value be changed during a run for a later iteration of the same run? For example, a run initially with cInit = 0.99
and cCond = 100
, causing an infinite recursion, would be stopped by changing cCond
to 0.0.
In my actual app, each iteration is slow, whereas the stopping check is quick. Also, intermediate results per iteration must be output during the run.
In Javafx 8, the GUI is on the Application Thread. To run a long-running algorithm, I'd presumably start a ScheduledService
in a background thread. Then, there'll be a new Task
per iteration with the stopping check sampling cCond
. This seems a bit complicated, as I've little experience with concurrency.
What could be a clean solution via FRP? In particular, how to connect the background Tasks
with the Application Thread via FRP entities?
Any Scala or Java or general advice would be appreciated.