1

I'm searching for any formal/generic approach to modeling a process distributed in time with functional programming. Here is an example.

Let's implement a very simple notification service. It accepts a "task" that can be unambiguously distinguished to be "done" or "not done". When a "task" is "done", the service notifies the client which requested this "task done" notification.

How it can be implemented?

Some considerations. Let's assume that a "task" is polling some HTTP URL for desired status code. Then, we need to:

  1. request URL
  2. check status code for desired value (where our "done" condition goes)
  3. if it is "done", then notify the client
  4. if not, then wait some time, and go to step 1

This is a very simple example, but even it raises some important topics in functional modeling (of which I'm not aware). Definitely, this process depends on time passing (or time current).

Next, it can be complicated further. For example, requesting our URL can result in a "rate limit error". So, we need to not only "wait" for a predefined amount of time, but also "wait for rate-limit". This functionality must be somehow "plugged in" to our hypothetical "wait" function.

3 Answers 3

2

Functional Reactive Programming is a "formal/generic approach to modeling a process distributed in time with functional programming."

For Haskel there is reactive-banana, for almost any other language, you could use RxExtensions

1
  • I made the answer more answer like. The questioner is looking for a formal/generic approach to modeling a process distributed in time with functional programming and that is precisely what FRP is...
    – Daniel T.
    Oct 12, 2021 at 0:32
0

It's pretty simple, you just make time of event part of the return value of the function rather than time at which the code executes

Once you have done that then you just run through the results in order applying them or waiting for the next accordingly.

1
  • Can you please elaborate more? I don't understand where this "waiting for the next" will happen?
    – Anthony
    Jul 9, 2023 at 10:19
0

My main functional experience is within Erlang. I am making the assumption that there could be multiple client requests in progress at once, and that they are all independent of each other. (i.e. they are not waiting on the same URL to provide a different status code)

A simple approach to handle the async callback is:

  1. The customer to make the request, passing in a callback function and a context object.
  2. The service receives the request, ensures it is a valid request, creates a worker thread to process the request and then response to the customer with an ack that the request has been received.
  3. When the worker thread completes the task, it responds to the customer by calling the callback with the context object and the result.

With functional programming, a good rule to follow is that if you think you need a loop, then in functional programming think recursion. Within the worker thread, to handle the repeated polling of the URL:

  1. Implement one function that hits the URL and obtains the status code.
  2. Implement a second function that calls the first function, and based on the status either returns the completed status, or waits for a poll delay period, then returns the result of recursively calling itself.

There is lots of additional complexity that you could add, but this is the core functional aspects to it.

Note, having HTTP polling is a poor design of the API. It would be better if the remote service provided a response, chunk encoded, returning a status message in a separate chunk every few seconds. That way the server is maintaining a large number of open connections ( a memory issue ) rather than processing a high rate of connection, disconnection and parsing HTTP requests which will consume much more CPU - a much more expensive resource.

1
  • Thanks. Although I'll not accept this as an answer (there is no time formalism in your answer), it is still useful. Please, take a look at this: github.com/tribals/koans/tree/main/…. Although it examines different kind of problem, I believe it is in line with question and answer. Feel free to contribute Erlang/Haskell/ML version of koan if you have time ^_^
    – Anthony
    Jul 9, 2023 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.