Repository F# setup
open System
open System.IO
open System.Threading
open System.Threading.Tasks
open Axial
open Axial.Console
open Axial.FileSystem
open Axial.Hosting
open Axial.Hosting.Browser
open Axial.Hosting.Node
open Axial.State
open Axial.Telemetry
open Axial.Telemetry.JavaScriptCombining Flows
Flow.map changes the successful value, Flow.mapError changes the expected error, and Flow.bind runs dependent
work (it is the function form of let!). Flow.zip runs two descriptions one after the other and keeps both values:
> let loadUser (id: int) : Flow<string, string> = if id = 1 then Flow.ok "ada" else Flow.fail $"no user {id}";;
> loadUser 1 |> Flow.map (fun name -> name.ToUpperInvariant()) |> Flow.run ();;
> loadUser 2 |> Flow.mapError (fun message -> message.Length) |> Flow.run ();;
> loadUser 1 |> Flow.bind (fun name -> Flow.ok $"Hello, {name}") |> Flow.run ();;
> Flow.zip (loadUser 1) (Flow.ok 42) |> Flow.run ();;val it: Exit<string,string> = Success "ADA"
val it: Exit<string,int> = Failure (Fail 9)
val it: Exit<string,string> = Success "Hello, ada"
val it: Exit<Tuple<string,int>,string> = Success ("ada", 42)loadUser: int -> Flow<string,string>id: intintAn abbreviation for the CLI type . Basic Types
FlowA flow that requires no environment and can fail with a typed error.
stringAn abbreviation for the CLI type . Basic Types
(=): 'T -> 'T -> boolStructural equality The first parameter. The second parameter. The result of the comparison. 5 = 5 // Evaluates to true 5 = 6 // Evaluates to false [1; 2] = [1; 2] // Evaluates to true (1, 5) = (1, 6) // Evaluates to false
Axial.Flowok: 'value -> Flow<'env,'error,'value>Creates a successful synchronous flow. The value to wrap in a successful flow. A flow that always succeeds with the provided value.
fail: 'error -> Flow<'env,'error,'value>Same as error. The error value to wrap in a failing flow. A flow that always fails with the provided error. let result = Flow.fail "error" |> Flow.run () // result = Failure (Cause.Fail "error")
(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
map: ('value -> 'next) -> Flow<'env,'error,'value> -> Flow<'env,'error,'next>Transforms the successful value of a flow. If the source fails, the is not executed. The original failure cause is preserved, including typed failures, interruption, and defects. Use map for pure value transformations after an effect has succeeded. A function of type 'value -> 'next to transform the successful value. The source flow of type to transform. A new with the transformed success value of type 'next. let flow = Flow.succeed 1 |> Flow.map (fun x -> x + 1)
name: stringToUpperInvariant: unit -> stringReturns a copy of this object converted to uppercase using the casing rules of the invariant culture. The uppercase equivalent of the current string.
run: 'env -> Flow<'env,'error,'value> -> Exit<'value,'error>Runs the workflow and blocks until the final exit is available. The environment used by the workflow. The workflow to run. The final workflow exit. let exit = workflow |> Flow.run environment
mapError: ('error -> 'nextError) -> Flow<'env,'error,'value> -> Flow<'env,'nextError,'value>Maps the error value of a synchronous flow. Transforms the error type of the flow while leaving successful values untouched. Useful for mapping internal errors into public-facing domain errors. The function to transform the error value. The source flow. A with the transformed error type. let flow = Flow.fail "error" |> Flow.mapError (fun err -> err + "!")
message: stringLength: intGets the number of characters in the current object. The number of characters in the current string.
bind: ('value -> Flow<'env,'error,'next>) -> Flow<'env,'error,'value> -> Flow<'env,'error,'next>Sequences a dependent flow after a successful value. This is the flatmap operation for . The continuation only runs when the source flow succeeds, and it receives the successful value. Use bind when the next effect depends on the previous result; use map when the next step is pure. On .NET, a recursive return! through bind does not keep a task per call alive. Work still pending in the source, such as a use or try/finally, lives until the source completes. A function that takes the successful value and returns a new flow. The source flow to sequence. A representing the combined workflow. let flow = Flow.succeed 1 |> Flow.bind (fun x -> Flow.succeed (x + 1))
zip: Flow<'env,'error,'left> -> Flow<'env,'error,'right> -> Flow<'env,'error,('left * 'right)>Runs two flows sequentially and combines their successful values into a tuple. The first flow to run. The second flow to run. A flow that returns a tuple of both successful values. Flow.zip (Flow.succeed 1) (Flow.succeed 2) |> Flow.run ()
Flow.map2 and Flow.map3 combine the successful values directly. Concurrent composition is a separate choice;
use Flow.zipPar only when both branches are safe to run at the same time.
Prefer flow {} for a longer dependent sequence and pipelines for a short transformation. Both create the same Flow.
Choosing where parallel branches start
Flow.zipPar and Flow.sequencePar start each branch inline, as Task.WhenAll does when given calls to async
methods. A branch runs on the current thread until it reaches an incomplete await. This keeps I/O and
already-completed flows cheap, but a synchronous CPU-bound branch can finish before the next branch starts.
Wrap only the CPU-bound branches that should start on the thread pool:
> let cpuBranch value : Flow<string, int> = Flow.delay (fun () -> Flow.ok (value * value)) |> Flow.runOn RunOn.ThreadPool;;
> [ 1; 2; 3 ] |> List.map cpuBranch |> Flow.sequencePar |> Flow.run ();;val it: Exit<int list,string> = Success [1; 4; 9]cpuBranch: int -> Flow<string,int>value: intFlowA flow that requires no environment and can fail with a typed error.
stringAn abbreviation for the CLI type . Basic Types
intAn abbreviation for the CLI type . Basic Types
Axial.Flowdelay: (unit -> Flow<'env,'error,'value>) -> Flow<'env,'error,'value>Defers flow construction until execution time. A function that returns the flow to execute. Recursive calls deferred here are consumed by the executor without growing a chain of parent tasks. Work that must run after the recursive call still needs storage until that call finishes. A flow that lazily evaluates the factory when executed. let flow = Flow.delay (fun () -> Flow.succeed 42)
ok: 'value -> Flow<'env,'error,'value>Creates a successful synchronous flow. The value to wrap in a successful flow. A flow that always succeeds with the provided value.
(*): ^T1 -> ^T2 -> ^T3Overloaded multiplication operator The first parameter. The second parameter. The result of the operation. 8 * 6 // Evaluates to 48
(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
runOn: RunOn -> Flow<'env,'error,'value> -> Flow<'env,'error,'value>Starts a flow at the requested execution location. This changes only where the wrapped flow starts. It keeps the same environment, cancellation token, runtime, and scope. Cancellation remains cooperative: the wrapped flow observes interruption at its normal check points, such as Flow.ensureNotCanceled. If the token is cancelled before scheduling or while waiting in the scheduler's queue, the flow returns interruption without starting the wrapped flow. On .NET, ThreadPool uses . DedicatedThread gives the synchronous part before the first incomplete await a dedicated thread; later continuations normally resume on the thread pool. Scheduler captures the supplied scheduler across awaits inside the wrapped flow; the caller's continuation resumes on its original scheduler. On JavaScript every location runs inline because JavaScript has one thread. Where the wrapped flow starts. The flow to start there. The wrapped flow, or unchanged for Inline. let parallel = jobs |> List.map (fun job -> compute job |> Flow.runOn RunOn.ThreadPool) |> Flow.sequencePar Fable compatible; JavaScript runs every case inline. Custom schedulers are .NET only.
Axial.RunOnSpecifies where a flow starts running. JavaScript has one thread, so every placement runs inline there.
ThreadPoolStarts work on the .NET thread pool.
Microsoft.FSharp.Collections.ListModuleContains operations for working with values of type . Operations for collections such as lists, arrays, sets, maps and sequences. See also F# Collection Types in the F# Language Guide.
map: ('T -> 'U) -> 'T list -> 'U listBuilds a new collection whose elements are the results of applying the given function to each of the elements of the collection. The function to transform elements from the input list. The input list. The list of transformed elements. let inputs = [ "a"; "bbb"; "cc" ] inputs |> List.map (fun x -> x.Length) Evaluates to [ 1; 3; 2 ]
sequencePar: Flow<'env,'error,'value> list -> Flow<'env,'error,'value list>Runs all flows concurrently and returns their values in input order. An empty input succeeds immediately. Branches start inline, like async methods passed to Task.WhenAll; use Flow.runOn RunOn.ThreadPool for synchronous CPU-heavy branches. If any flow fails, remaining flows are interrupted through the same structured parallel composition as zipPar. Scoped resources follow zipPar too: when every flow succeeds they move to the caller's scope; when any fails, all of them are released before sequencePar returns.
run: 'env -> Flow<'env,'error,'value> -> Exit<'value,'error>Runs the workflow and blocks until the final exit is available. The environment used by the workflow. The workflow to run. The final workflow exit. let exit = workflow |> Flow.run environment
Flow.runOn RunOn.ThreadPool source moves a CPU-heavy flow to the thread pool. RunOn.Inline leaves the flow unchanged. See Where work runs for parallel policies and adaptive placement.
On .NET, RunOn.Scheduler scheduler starts the flow on a particular TaskScheduler and keeps the wrapped flow's
continuations after awaits on that scheduler. The caller after runOn resumes on its original scheduler. Nested
placement on the same exclusive scheduler yields rather than blocking it. RunOn.DedicatedThread gives a long
blocking synchronous section a dedicated thread, but
only until its first incomplete await; use Flow.fromBlocking instead when a blocking foreign call must run to
completion after interruption.
runOn does not turn interruption into preemption. The branch receives the same cancellation token and observes it
at its normal checkpoints, such as Flow.ensureNotCanceled; parallel combinators still wait for branch cleanup.
If the token is already cancelled, the branch is not scheduled. If it is cancelled while queued on a scheduler, the
wrapped flow does not start. JavaScript has one thread, so every RunOn case runs inline when compiled with Fable.
Related guides
- Composition reference lists mapping, binding, recovery, traversal, and sequential combination functions.
- Fibers introduces explicit child workflows.
- Schedules adds retry and repetition policies without changing the underlying workflow.

