Skip to content

Commit 12df229

Browse files
xperiandriclaude
andcommitted
docs: write the how-to guides
The two pages of the how-to guides were placeholders: the first one held a text about `IObservable` and Rx, and the second one was empty. They are now "How To build a flat list" and "How To get options and reference tuples", and the pages that link to the guides point to the first of them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 7e3145b commit 12df229

6 files changed

Lines changed: 138 additions & 25 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: How To build a flat list
3+
category: How To Guides
4+
categoryindex: 2
5+
index: 1
6+
---
7+
8+
# How To build a flat list
9+
10+
A `FlatList<'T>` is an `ImmutableArray<'T>`: a function that adds an element returns a new array. To build a flat list
11+
from many pieces, collect the elements first and create the flat list once.
12+
13+
## From elements that you already have
14+
15+
``` F#
16+
open FSharp.Collections.Immutable
17+
18+
let fromArray = FlatList.ofArray [| 1; 2; 3 |]
19+
let fromList = FlatList.ofList [ 1; 2; 3 ]
20+
let fromSeq = FlatList.ofSeq (seq { 1..3 })
21+
let squares = FlatList.init 5 (fun index -> index * index)
22+
```
23+
24+
## With a computation expression
25+
26+
`flatList { }` takes `yield`, `yield!`, loops and conditions, and creates the flat list when the expression ends:
27+
28+
``` F#
29+
let evenSquares = flatList {
30+
for number in 1..10 do
31+
if number % 2 = 0 then
32+
yield number * number
33+
34+
yield! [| 121; 144 |]
35+
}
36+
```
37+
38+
## With a builder
39+
40+
`FlatList.build` gives a builder to a function and returns the flat list of the elements that the function added. The
41+
functions of the `FlatListBuilder` module that change the builder return it, so they form a pipeline:
42+
43+
``` F#
44+
let built =
45+
FlatList.build (fun builder ->
46+
builder
47+
|> FlatListBuilder.add 1
48+
|> FlatListBuilder.addRange [ 2; 3 ]
49+
|> FlatListBuilder.insertAt 0 0
50+
|> ignore
51+
)
52+
```
53+
54+
`FlatList.update` does the same with the elements of an existing flat list, which stays unchanged:
55+
56+
``` F#
57+
let withoutFirst =
58+
built
59+
|> FlatList.update (fun builder -> builder |> FlatListBuilder.removeAt 0 |> ignore)
60+
```
61+
62+
## Without a copy
63+
64+
`FlatList.builderWith` creates a builder of a given capacity. When the builder is full, `FlatList.moveFromBuilder` gives
65+
its array to the flat list without a copy and empties the builder:
66+
67+
``` F#
68+
let builder = FlatList.builderWith 3
69+
70+
for number in 1..3 do
71+
builder.Add number
72+
73+
let moved = FlatList.moveFromBuilder builder
74+
```
75+
76+
`FlatList.moveFromBuilder` throws `InvalidOperationException` when the count of the builder differs from its capacity.
77+
`FlatList.ofBuilder` copies the elements and takes a builder of any count.

‎docsSrc/How_Tos/Doing_A_Thing.md‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎docsSrc/How_Tos/Doing_Another_Thing.md‎

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: How To get options and reference tuples
3+
category: How To Guides
4+
categoryindex: 2
5+
index: 2
6+
---
7+
8+
# How To get options and reference tuples
9+
10+
The functions of `FlatList` return value options and struct tuples, which need no allocation:
11+
12+
``` F#
13+
open FSharp.Collections.Immutable
14+
15+
let numbers = FlatList.ofArray [| 1; 2; 3; 4 |]
16+
17+
// ValueSome 2
18+
let firstEven = numbers |> FlatList.tryFind (fun number -> number % 2 = 0)
19+
20+
let struct (evens, odds) = numbers |> FlatList.partition (fun number -> number % 2 = 0)
21+
```
22+
23+
Code that works with `option` and reference tuples does not have to convert these results.
24+
25+
## Options
26+
27+
Open the `FSharp.Collections.Immutable.Option` namespace after `FSharp.Collections.Immutable`. The functions that take or
28+
return a value option then take or return an option:
29+
30+
``` F#
31+
open FSharp.Collections.Immutable
32+
open FSharp.Collections.Immutable.Option
33+
34+
// Some 1
35+
let firstOdd = numbers |> FlatList.tryFind (fun number -> number % 2 = 1)
36+
37+
let halves =
38+
numbers
39+
|> FlatList.choose (fun number -> if number % 2 = 0 then Some (number / 2) else None)
40+
```
41+
42+
## Reference tuples
43+
44+
Open the `FSharp.Collections.Immutable.Tuple` namespace after `FSharp.Collections.Immutable`. The functions that return
45+
struct tuples then return reference tuples:
46+
47+
``` F#
48+
open FSharp.Collections.Immutable
49+
open FSharp.Collections.Immutable.Tuple
50+
51+
let small, large = numbers |> FlatList.partition (fun number -> number < 3)
52+
53+
// FlatList<int * string>
54+
let named = FlatList.zip numbers (numbers |> FlatList.map string)
55+
```
56+
57+
The two namespaces hold only the functions whose types differ. Every other function of `FlatList` stays the one of
58+
`FSharp.Collections.Immutable`, so the `open` changes these types and nothing else. Both namespaces can be open at the
59+
same time.

‎docsSrc/Tutorials/Getting_Started.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ printfn "Set: %A" (set1 |> Seq.toList)
3636
printfn "Dict: %A" (dict1 |> Seq.toList)
3737
## Next Steps
3838
39-
- Check out the [How-To Guides](../How_Tos/Doing_A_Thing.html) for common scenarios
39+
- Check out the [How-To Guides](../How_Tos/Build_A_Flat_List.html) for common scenarios
4040
- Read the [Background](../Explanations/Background.html) for deeper understanding
4141
- Browse the [API Reference](../reference/index.html) for detailed documentation

‎docsSrc/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ F# bindings for [System.Collections.Immutable](https://learn.microsoft.com/en-us
5252
<p class="card-text">Guides you through the steps involved in addressing key problems and use-cases. </p>
5353
</div>
5454
<div class="card-footer text-right border-top-0">
55-
<a href="{{root}}How_Tos/Doing_A_Thing.html" class="btn btn-primary">Learn Usecases</a>
55+
<a href="{{root}}How_Tos/Build_A_Flat_List.html" class="btn btn-primary">Learn Usecases</a>
5656
</div>
5757
</div>
5858
</div>

0 commit comments

Comments
 (0)