A program is a valueLesson 3 of 23

What can you change without touching it?

Retry, repeat and time out a program from outside it.

flaky describes a call to a busy server. Each attempt takes 200 milliseconds, and the first two fail. Somebody else wrote it, and for this lesson you aren't allowed to edit it.

Predict

program is flaky, as it stands. Run it in your head. What is the last thing the output shows?

Check your answer

One line, attempt 1, and then a failure: server busy.

Effect.fail("server busy") ends the program on the spot, with that value as the reason. Press Run to see it. Press it again: still attempt 1, because each run starts the file over.

Try it

Change only the last line, never flaky.

  1. Make it flaky.pipe(Effect.retry({ times: 2 })). Before you run: how many attempt lines will there be, and how does it end?

  2. Set times to 1. What happens now, and why? Then put it back to 2.

  3. Give the whole thing half a second: add Effect.timeout("500 millis") after the retry. Three attempts take 600 milliseconds. Before you run: which wins?

  4. Move the timeout line above the retry line. The same two lines, in the other order. What does the half second apply to now?

  5. Add Effect.repeat({ times: 1 }) at the end. How many attempts in total?

About steps 3 and 4

In step 3 the limit wraps everything, retries included, so the clock runs out during the third attempt and the program fails with a timeout. In step 4 the limit wraps one attempt, and the retry wraps that. Each attempt gets its own half second, and the third one succeeds.

Each line takes a description and gives back a new one, so the order you stack them in is the order they nest.

What you found

You changed how often flaky runs, how long it may take and what happens when it fails. The text of flaky is the same as when you started.

That is only possible because of lesson 1. flaky is a description, so retry can run it again, timeout can stop waiting for it, and repeat can run it once more after it works. If flaky were a call that had already gone out, you would be holding one result of one attempt, and there would be nothing left to retry.

So decisions like these don't have to be written into the work. They belong to whoever holds it, and they compose the way any values do.

Every step here leaned on flaky failing. But read flaky again, or just its name in the last line. What told you it could fail at all?

03
Principle 3

Behaviour is added from outside

Because a program is a value, how often it runs, how long it may take and what happens when it fails are decided by whoever holds it, not written into it.

OutputNot run yet

Not run yet

Read the code and say what it will do. Then press Run.