Logo Orsak

Effect Module

Functions for running, combining, recovering and repeating effects.

Functions and values

Function or value Description

Effect.asResult e

Full Usage: Effect.asResult e

Parameters:
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, Result<'a, 'e>, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Allows the effect to be bound as a result, rather than failing the surrounding effect when bound. This allows for taking recovery action inside the effect, based on the outcome of a sub-effect.

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, Result<'a, 'e>, 'e>

Effect.bind f e

Full Usage: Effect.bind f e

Parameters:
    f : 'a -> Effect<'r, 'b, 'e> - The continuation function to run in case e succeeds.
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'b, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'b, 'e

Monadic bind of the effect.

f : 'a -> Effect<'r, 'b, 'e>

The continuation function to run in case e succeeds.

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'b, 'e>

Effect.bindNewError e f

Full Usage: Effect.bindNewError e f

Parameters:
    e : Effect<'r, 'a, 'e1> - The effect
    f : 'a -> Effect<'r, 'b, 'e2> - Function which produces an effect that fails with a different error

Returns: Effect<'r, 'b, Choice<'e1, 'e2>>
Type parameters: 'r, 'a, 'e1, 'b, 'e2

Binds an effect with an effect of a different error type

e : Effect<'r, 'a, 'e1>

The effect

f : 'a -> Effect<'r, 'b, 'e2>

Function which produces an effect that fails with a different error

Returns: Effect<'r, 'b, Choice<'e1, 'e2>>

Effect.changeError f e

Full Usage: Effect.changeError f e

Parameters:
    f : 'e1 -> 'e2 - Function which maps the error, in case the effect had failed
    e : Effect<'r, 'a, 'e1> - The effect

Returns: Effect<'r, 'a, 'e2>
Type parameters: 'e1, 'e2, 'r, 'a

Transforms an effect which fails with an error of to an effect which fails with an error of type .

f : 'e1 -> 'e2

Function which maps the error, in case the effect had failed

e : Effect<'r, 'a, 'e1>

The effect

Returns: Effect<'r, 'a, 'e2>

Effect.fanOut size f s

Full Usage: Effect.fanOut size f s

Parameters:
    size : int - The number of workers
    f : IAsyncEnumerable<'a> -> Effect<'r, unit, ^err> - Creates the effect that processes a worker's items
    s : IAsyncEnumerable<'a> - The items to distribute

Returns: Effect<'r, unit, ^err>
Modifiers: inline
Type parameters: 'a, 'r, ^err (requires (static member op_Addition : ^err * ^err -> ^err))

Distributes the items of s over size workers, in turn, and runs the effect created by f for each worker, on the items it is given. The workers run concurrently, and the effect completes when they all have.

When a worker fails, no more items are taken from s, the other workers are stopped where they are, and the effect fails with that worker's error (or exception). A worker that returns before its items end doesn't hold up the others: the items it would have been given are discarded. Each worker is given at most 10 items ahead of processing them.

size : int

The number of workers

f : IAsyncEnumerable<'a> -> Effect<'r, unit, ^err>

Creates the effect that processes a worker's items

s : IAsyncEnumerable<'a>

The items to distribute

Returns: Effect<'r, unit, ^err>
Example

 incomingOrders
 |> Effect.fanOut 4 (fun orders -> eff {
     for order in orders do
         do! processOrder order
 })

Effect.forever e

Full Usage: Effect.forever e

Parameters:
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Type parameters: 'r, 'a, 'e

Runs the effect again every time it fails, until it succeeds. The resulting effect never fails.

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.join e

Full Usage: Effect.join e

Parameters:
Returns: Effect<'a, 'b, 'c>
Modifiers: inline
Type parameters: 'a, 'b, 'c

Monadic join of the effect.

e : Effect<'a, Effect<'a, 'b, 'c>, 'c>

The effect

Returns: Effect<'a, 'b, 'c>

Effect.joinResult e

Full Usage: Effect.joinResult e

Parameters:
Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Merges an inner result with its surrounding effect.

e : Effect<'r, Result<'a, 'e>, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.joinTask e

Full Usage: Effect.joinTask e

Parameters:
Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Merges an inner task with its surrounding effect.

e : Effect<'r, Task<'a>, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.map f e

Full Usage: Effect.map f e

Parameters:
    f : 'a -> 'b - The continuation function to run in case e succeeds.
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'b, 'e>
Modifiers: inline
Type parameters: 'a, 'b, 'r, 'e

map of the effect.

f : 'a -> 'b

The continuation function to run in case e succeeds.

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'b, 'e>

Effect.map2 f s1 s2

Full Usage: Effect.map2 f s1 s2

Parameters:
    f : 'a -> 'b -> 'c
    s1 : Effect<'d, 'a, 'e>
    s2 : Effect<'d, 'b, 'e>

Returns: Effect<'d, 'c, 'e>
Type parameters: 'a, 'b, 'c, 'd, 'e

map2 of the effect.

f : 'a -> 'b -> 'c
s1 : Effect<'d, 'a, 'e>
s2 : Effect<'d, 'b, 'e>
Returns: Effect<'d, 'c, 'e>

Effect.mapPar s f

Full Usage: Effect.mapPar s f

Parameters:
Returns: Effect<'a0, 'b list, 'c>
Modifiers: inline
Type parameters: 'r, 'a, 'e, 'a, 'b, 'c
s : Effect<'r, 'a, 'e> seq
f : Effect<'r, 'a, 'e> -> Effect<'a0, 'b, 'c>
Returns: Effect<'a0, 'b list, 'c>

Effect.mapParLimited limit s f

Full Usage: Effect.mapParLimited limit s f

Parameters:
    limit : int - The most effects to run at a time, at least 1
    s : Effect<'r, 'a, 'e> seq - The effects
    f : Effect<'r, 'a, 'e> -> Effect<'r, 'b, 'e> - The function to apply to each effect

Returns: Effect<'r, 'b list, 'e>
Type parameters: 'r, 'a, 'e, 'b

Applies f to each effect, and executes the results in parallel, at most limit at a time, like Effect.parLimited.

limit : int

The most effects to run at a time, at least 1

s : Effect<'r, 'a, 'e> seq

The effects

f : Effect<'r, 'a, 'e> -> Effect<'r, 'b, 'e>

The function to apply to each effect

Returns: Effect<'r, 'b list, 'e>

Effect.onError f e

Full Usage: Effect.onError f e

Parameters:
    f : 'e -> Effect<'r, 'a, 'e> - The function to run in case of failure
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'e, 'r, 'a

Potentially recovers a failed effect by producing a new effect in case of failure. If the effect was already successful, f is never run.

f : 'e -> Effect<'r, 'a, 'e>

The function to run in case of failure

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.par s

Full Usage: Effect.par s

Parameters:
    s : Effect<'r, 'a, 'e> seq - The effects to run in parallel

Returns: Effect<'r, 'a list, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Executes effects in parallel if possible, and collects their results in a list. Like Effect.whenAll, which returns an array and allocates less.

s : Effect<'r, 'a, 'e> seq

The effects to run in parallel

Returns: Effect<'r, 'a list, 'e>

Effect.parLimited limit s

Full Usage: Effect.parLimited limit s

Parameters:
    limit : int - The most effects to run at a time, at least 1
    s : Effect<'r, 'a, 'e> seq - The effects to run in parallel

Returns: Effect<'r, 'a list, 'e>
Type parameters: 'r, 'a, 'e

Executes effects in parallel, at most limit at a time, and collects their results in a list, in the order the effects were given. The effects are started in that order, the next one as soon as fewer than limit are running, without waiting for the others. Once one fails or throws, no more are started, and once those already running have completed, the result is the first error in that order, or the exception.

limit : int

The most effects to run at a time, at least 1

s : Effect<'r, 'a, 'e> seq

The effects to run in parallel

Returns: Effect<'r, 'a list, 'e>
Example

 // at most 4 requests at a time
 customerIds |> Seq.map fetchCustomer |> Effect.parLimited 4
module Seq from Microsoft.FSharp.Collections
val map: mapping: ('T -> 'U) -> source: 'T seq -> 'U seq

Effect.par_ s

Full Usage: Effect.par_ s

Parameters:
    s : Effect<'r, unit, 'e> seq - The effects to run in parallel

Returns: Effect<'r, unit, 'e>
Modifiers: inline
Type parameters: 'r, 'e

Executes effects in parallel if possible.

s : Effect<'r, unit, 'e> seq

The effects to run in parallel

Returns: Effect<'r, unit, 'e>

Effect.race eff1 eff2

Full Usage: Effect.race eff1 eff2

Parameters:
    eff1 : Effect<'a, 'b, 'c> - The first effect
    eff2 : Effect<'a, 'b, 'c> - The second effect

Returns: Effect<'a, 'b, 'c>
Modifiers: inline
Type parameters: 'a, 'b, 'c

Races two effects against each other, returning the result of the winner.

eff1 : Effect<'a, 'b, 'c>

The first effect

eff2 : Effect<'a, 'b, 'c>

The second effect

Returns: Effect<'a, 'b, 'c>

Effect.recover f e

Full Usage: Effect.recover f e

Parameters:
    f : 'e -> 'a - The function to run in case of failure
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'e, 'a, 'r

Recovers a failed effect by producing an in case of failure. If the effect was already successful, f is never run.

f : 'e -> 'a

The function to run in case of failure

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.repeatForever e

Full Usage: Effect.repeatForever e

Parameters:
    e : Effect<'a, unit, 'b> - The effect

Returns: Effect<'a, unit, 'b>
Type parameters: 'a, 'b

Runs the effect repeatedly, one run after another, until it fails.

e : Effect<'a, unit, 'b>

The effect

Returns: Effect<'a, unit, 'b>

Effect.repeatTimes time e

Full Usage: Effect.repeatTimes time e

Parameters:
    time : int - How many times to run the effect
    e : Effect<'a, unit, 'b> - The effect

Returns: Effect<'a, unit, 'b>
Modifiers: inline
Type parameters: 'a, 'b

Runs the effect time times, one after another. It stops at the first failure.

time : int

How many times to run the effect

e : Effect<'a, unit, 'b>

The effect

Returns: Effect<'a, unit, 'b>

Effect.repeatUntil fn e

Full Usage: Effect.repeatUntil fn e

Parameters:
    fn : unit -> bool - Whether to stop
    e : Effect<'a, unit, 'b> - The effect

Returns: Effect<'a, unit, 'b>
Modifiers: inline
Type parameters: 'a, 'b

Runs the effect repeatedly until fn returns true, which is checked before every run. It stops at the first failure.

fn : unit -> bool

Whether to stop

e : Effect<'a, unit, 'b>

The effect

Returns: Effect<'a, unit, 'b>

Effect.repeatUntilCancellation token e

Full Usage: Effect.repeatUntilCancellation token e

Parameters:
Returns: Effect<'a, unit, 'b>
Modifiers: inline
Type parameters: 'a, 'b

Runs the effect repeatedly until token is cancelled, which is checked before every run. It stops at the first failure.

token : CancellationToken

The token that stops the repetition

e : Effect<'a, unit, 'b>

The effect

Returns: Effect<'a, unit, 'b>
Example

 processNextMessage ()
 |> Effect.repeatUntilCancellation stoppingToken

Effect.repeatWhileTrue e

Full Usage: Effect.repeatWhileTrue e

Parameters:
    e : Effect<'r, bool, 'e> - The effect, returning whether to run it again

Returns: Effect<'r, unit, 'e>
Type parameters: 'r, 'e

Runs the effect repeatedly for as long as it returns true.

e : Effect<'r, bool, 'e>

The effect, returning whether to run it again

Returns: Effect<'r, unit, 'e>

Effect.resolveEnvironment env effect

Full Usage: Effect.resolveEnvironment env effect

Parameters:
    env : 'env - Provider needed to run the effect
    effect : Effect<'env, 'a, 'err> - The effect to resolve

Returns: Effect<'r, 'a, 'err>
Modifiers: inline
Type parameters: 'env, 'a, 'err, 'r

Runs an effect with the provided environment, resolving the dependencies of the providers in that environment

env : 'env

Provider needed to run the effect

effect : Effect<'env, 'a, 'err>

The effect to resolve

Returns: Effect<'r, 'a, 'err>

Effect.resultJoin r

Full Usage: Effect.resultJoin r

Parameters:
    r : Result<Effect<'r, 'a, 'e>, 'e> - The result containing the effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Merges an outer result with its inner effect.

r : Result<Effect<'r, 'a, 'e>, 'e>

The result containing the effect

Returns: Effect<'r, 'a, 'e>

Effect.ret a

Full Usage: Effect.ret a

Parameters:
    a : 'a - The value to wrap in the effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Creates a new effect from a.

a : 'a

The value to wrap in the effect

Returns: Effect<'r, 'a, 'e>

Effect.retry e

Full Usage: Effect.retry e

Parameters:
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Runs the effect again, once, if it fails.

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.retryIf cond e

Full Usage: Effect.retryIf cond e

Parameters:
    cond : 'e -> bool - Whether to retry after a given error
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'e, 'r, 'a

Runs the effect again, once, if it fails with an error for which cond is true.

cond : 'e -> bool

Whether to retry after a given error

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.retryTimes times e

Full Usage: Effect.retryTimes times e

Parameters:
    times : int - How many times to retry
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Runs the effect again when it fails, up to times times after the first attempt. It fails with the last error if every attempt fails.

times : int

How many times to retry

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>
Example

 sendEmail message |> Effect.retryTimes 3

Effect.retryWhile cond e

Full Usage: Effect.retryWhile cond e

Parameters:
    cond : 'e -> bool - Whether to retry after a given error
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Type parameters: 'e, 'r, 'a

Runs the effect again every time it fails with an error for which cond is true. It fails with the first error for which cond is false.

cond : 'e -> bool

Whether to retry after a given error

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>
Example

 fetchPrices ()
 |> Effect.retryWhile (fun err -> err = Timeout)

Effect.run env e

Full Usage: Effect.run env e

Parameters:
    env : 'r - The environment needed to start the effect
    e : Effect<'r, 'a, 'e> - The effect to run

Returns: AsyncResult<'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Starts the effect.

env : 'r

The environment needed to start the effect

e : Effect<'r, 'a, 'e>

The effect to run

Returns: AsyncResult<'a, 'e>
Example

 task {
     match! placeOrder order |> Effect.run env with
     | Ok id -> printfn $"Placed {id}"
     | Error err -> printfn $"Failed: {err}"
 }
val task: TaskBuilder
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
val id: obj
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val err: obj

Effect.runOrFail env e

Full Usage: Effect.runOrFail env e

Parameters:
    env : 'r - The environment needed to start the effect
    e : Effect<'r, 'a, 'e> - The effect to run

Returns: Task<'a>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Starts the effect, and raises an exception if it fails, with the error's string representation as its message. Prefer Effect.run, which returns the error as a value; this is meant for places where an exception is the only option, such as tests or scripts.

env : 'r

The environment needed to start the effect

e : Effect<'r, 'a, 'e>

The effect to run

Returns: Task<'a>

Effect.sequence eff

Full Usage: Effect.sequence eff

Parameters:
    eff : Effect<'r, 'a, 'e> array

Returns: Effect<'r, 'a array, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Sequences an array of effects, turning it in to an effect of an array. For a more generic implementation, consider FSharpPlus

eff : Effect<'r, 'a, 'e> array
Returns: Effect<'r, 'a array, 'e>

Effect.taskJoin t

Full Usage: Effect.taskJoin t

Parameters:
    t : Task<Effect<'r, 'a, 'e>> - The task containing the effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Merges an outer task with its inner effect.

t : Task<Effect<'r, 'a, 'e>>

The task containing the effect

Returns: Effect<'r, 'a, 'e>

Effect.timeout ts onTimeout eff

Full Usage: Effect.timeout ts onTimeout eff

Parameters:
    ts : TimeSpan - How long to wait for the effect
    onTimeout : 'a - The error to fail with when the effect doesn't finish in time
    eff : Effect<'b, 'c, 'a> - The effect

Returns: Effect<'b, 'c, 'a>
Modifiers: inline
Type parameters: 'a, 'b, 'c

Configures an effect to fail after a given amount of time, unless it has already succeeded. The original effect still executes.

ts : TimeSpan

How long to wait for the effect

onTimeout : 'a

The error to fail with when the effect doesn't finish in time

eff : Effect<'b, 'c, 'a>

The effect

Returns: Effect<'b, 'c, 'a>
Example

 lookupAddress postcode
 |> Effect.timeout (TimeSpan.FromSeconds 2.0) AddressLookupTimedOut

Effect.traverse f effects

Full Usage: Effect.traverse f effects

Parameters:
    f : 'a -> 'b
    effects : Effect<'r, 'a, 'e> array

Returns: Effect<'r, 'b array, 'e>
Modifiers: inline
Type parameters: 'a, 'b, 'r, 'e

Traverses an array of effects, turning it in to an effect of an array. For a more generic implementation, consider FSharpPlus

f : 'a -> 'b
effects : Effect<'r, 'a, 'e> array
Returns: Effect<'r, 'b array, 'e>

Effect.tryRecover f e

Full Usage: Effect.tryRecover f e

Parameters:
    f : 'e -> Result<'a, 'e> - The function to run in case of failure
    e : Effect<'r, 'a, 'e> - The effect

Returns: Effect<'r, 'a, 'e>
Modifiers: inline
Type parameters: 'e, 'a, 'r

Potentially recovers a failed effect by producing an Result{'a, 'e} in case of failure. If the effect was already successful, f is never run.

f : 'e -> Result<'a, 'e>

The function to run in case of failure

e : Effect<'r, 'a, 'e>

The effect

Returns: Effect<'r, 'a, 'e>

Effect.whenAll s

Full Usage: Effect.whenAll s

Parameters:
    s : Effect<'r, 'a, 'e> seq - The effects to run in parallel

Returns: Effect<'r, 'a array, 'e>
Modifiers: inline
Type parameters: 'r, 'a, 'e

Executes effects in parallel if possible.

s : Effect<'r, 'a, 'e> seq

The effects to run in parallel

Returns: Effect<'r, 'a array, 'e>

Effect.withCancellation ct eff

Full Usage: Effect.withCancellation ct eff

Parameters:
Returns: Effect<'a, unit, 'b>
Type parameters: 'a, 'b

Completes successfully when ct is cancelled, without waiting for the effect to finish. The effect is not cancelled: it keeps running, and its result is ignored.

ct : CancellationToken

The token that ends the wait

eff : Effect<'a, unit, 'b>

The effect

Returns: Effect<'a, unit, 'b>

Effect.withEnvironment f e

Full Usage: Effect.withEnvironment f e

Parameters:
    f : 'r2 -> 'r1 - Derives the environment of e
    e : Effect<'r1, 'a, 'e> - The effect

Returns: Effect<'r2, 'a, 'e>
Type parameters: 'r2, 'r1, 'a, 'e

Runs the effect with an environment derived by f from the one the resulting effect is run with. Such as an effect needing a particular environment type, run in the environment of a scope, which inherits that type.

f : 'r2 -> 'r1

Derives the environment of e

e : Effect<'r1, 'a, 'e>

The effect

Returns: Effect<'r2, 'a, 'e>
Example

 // report: Effect<AppEnvironment, Report, AppError>, run in a transaction, whose environment inherits AppEnvironment
 let! report = report |> Effect.withEnvironment (fun env -> upcast env)
val report: obj

Effect.zip e1 e2 f

Full Usage: Effect.zip e1 e2 f

Parameters:
    e1 : Effect<'r, 'a, ^e> - The first effect.
    e2 : Effect<'r, 'b, ^e> - The second effect.
    f : 'a * 'b -> 'g - Function to combine the result of the effects in case both succeeds.

Returns: Effect<'r, 'g, ^e>
Modifiers: inline
Type parameters: 'r, 'a, ^e, 'b, 'g (requires (static member op_Addition : ^e * ^e -> ^e))

Zips two effects together, the resulting effect will fail if either of the effects failed. In case both failed, the error is combined. The effects will execute in parallel if possible.

e1 : Effect<'r, 'a, ^e>

The first effect.

e2 : Effect<'r, 'b, ^e>

The second effect.

f : 'a * 'b -> 'g

Function to combine the result of the effects in case both succeeds.

Returns: Effect<'r, 'g, ^e>

Type something to start searching.