How to request a URL for each timecode using functional reactive programming?

I am wondering, how could you write a program using Functional Reactive Programming that, each x timesteps, extracts a JSON object from a given URL?

I look in the Elm framework, but I am open to more general solutions. I have a method

send : Signal (Request a) -> Signal (Response String) 

i.e. it receives an HTTP request wrapped in a signal and returns a response string wrapped in a signal.

Now I have a โ€œnext stateโ€ function that receives input signals and creates a new game state. They are wrapped using foldp. One input is a response from an HTTP request. However, when I run it, the request is run only once, and not every timecode. How can i fix this?

EDIT: Here, how would I solve this using non-FRP (imperative style):

 while True: myJson = send postRequest url --do stuff with myJSON sleep(timestep) 

i.e. just request url so often, endlessly looping.

+4
source share
1 answer

From the 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)

The above functions can be used to perform the necessary actions:

send $ lift (\_ -> get myURL) $ every (10 * seconds) , which will be of type Signal (Response String)

I have not tested the code, but hope this gives you this idea.

+5
source

Source: https://habr.com/ru/post/1501131/


All Articles