Skip to content

Commit a0a08ed

Browse files
CopilotBillWagner
andcommitted
Add applicative computation expressions documentation to and! section
Co-authored-by: BillWagner <[email protected]>
1 parent 93949d8 commit a0a08ed

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

docs/fsharp/language-reference/computation-expressions.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ If you bind the call to a computation expression with `let`, you will not get th
8080

8181
### `and!`
8282

83-
The `and!` keyword allows you to bind the results of multiple computation expression calls in a performant manner.
83+
The `and!` keyword allows you to bind the results of multiple computation expression calls in a performant manner. This keyword enables **applicative computation expressions**, which provide a different computational model from the standard monadic approach.
8484

8585
```fsharp
8686
let doThingsAsync url =
@@ -92,12 +92,20 @@ let doThingsAsync url =
9292
}
9393
```
9494

95-
Using a series of `let! ... let! ...` forces re-execution of expensive binds, so using `let! ... and! ...` should be used when binding the results of numerous computation expressions.
95+
Using a series of `let! ... let! ...` forces sequential execution where each bind depends on the previous one. In contrast, `let! ... and! ...` indicates that the computations are independent of each other. This independence allows computation expression authors to:
96+
97+
- Execute computations more efficiently
98+
- Potentially run computations in parallel
99+
- Accumulate results without forcing unnecessary sequential dependencies
100+
101+
The restriction is that computations combined with `and!` cannot depend on the results of previously bound values within the same `let!`/`and!` chain. This trade-off enables the performance benefits.
96102

97103
`and!` is defined primarily by the `MergeSources(x1, x2)` member on the builder type.
98104

99105
Optionally, `MergeSourcesN(x1, x2 ..., xN)` can be defined to reduce the number of tupling nodes, and `BindN(x1, x2 ..., xN, f)`, or `BindNReturn(x1, x2, ..., xN, f)` can be defined to bind computation expression results efficiently without tupling nodes.
100106

107+
For more information on applicative computation expressions, see [Applicative Computation Expressions in F# 5](../whats-new/fsharp-50.md#applicative-computation-expressions) and [F# RFC FS-1063](https://github.com/fsharp/fslang-design/blob/main/FSharp-5.0/FS-1063-support-letbang-andbang-for-applicative-functors.md).
108+
101109
### `do!`
102110

103111
The `do!` keyword is for calling a computation expression that returns a `unit`-like type (defined by the `Zero` member on the builder):

0 commit comments

Comments
 (0)