I'm wondering, how would you write a program using Functional Reactive Programming which, every x timesteps, retrieves a JSON object from a given URL?
I'm looking in the Elm framework, but I'm open to more general solutions. I have a method
send : Signal (Request a) -> Signal (Response String)
i.e. it takes a HTTP Request wrapped in a signal, and returns a response string, wrapped in a signal.
Right now, I have a "next state" function which takes input signals and creates a new game state. These are wrapped up using foldp. One of the input signals is the Response from the HTTP request. However, when I run it, the query runs only once, not every timestep. How can I fix this?
EDIT: Here's how I would solve this using non-FRP (imperative style):
while True:
myJson = send postRequest url
--do stuff with myJSON
sleep(timestep)
i.e. just query the url every so often, infinitely looping.
From Elm docs you will find:
every : Time -> Signal Time
lift : (a -> b) -> Signal a -> Signal b
get : String -> Request String
send : Signal (Request a) -> Signal (Response String)
Above functions can be used to do what you need:
send $ lift (\_ -> get myURL) $ every (10 * seconds)
which will be of type Signal (Response String)
I haven't tested the code but I hope this gives you the idea.