Can the concat
combinator defined in the demo 01_retry.ts
be used to define a Semigroup
instance for the RetryPolicy
type?
Yes, it can. Let's define the Semigroup like this:
import { Semigroup } from 'fp-ts/Semigroup'
const SemigroupRetryPolicy: Semigroup<RetryPolicy> = {
concat: (first, second) => concat(first)(second)
}
It obeys all the Semigroup rules:
-
first
,second
and the result ofconcat
are all of the same typeRetryPolicy
-
concat
is associative:given 3
RetryPolicy
first
,second
andthird
and astatus
:- if any of the
RetryPolicy
returnsundefined
, then the result of bothconcat(concat(first, second), third)(status)
andconcat(first, concat(second, third))(status)
will beundefined
. - if all the
RetryPolicy
return a number, thenconcat(concat(first, second), third)(status)
will beMath.max(Math.max(delay1, delay2), delay3)
andconcat(first, concat(second, third))(status)
will beMath.max(delay1, Math.max(delay2, delay3))
. AsMath.max
is associative, the result will be the max ofdelay1
,delay2
anddelay3
.
- if any of the