Skip to content

Commit 12b03a3

Browse files
committed
fix docs
1 parent 72d715a commit 12b03a3

4 files changed

Lines changed: 53 additions & 39 deletions

File tree

docs/AsyncSeq.fsx

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
(**
2+
---
3+
title: F# Asynchronous Sequences
4+
category: Documentation
5+
categoryindex: 2
6+
index: 1
7+
description: An introduction to F# asynchronous sequences and how to use them.
8+
keywords: F#, asynchronous sequences, AsyncSeq, IAsyncEnumerable, async workflows
9+
---
10+
*)
111
(*** condition: prepare ***)
212
#nowarn "211"
313
#I "../src/FSharp.Control.AsyncSeq/bin/Release/netstandard2.1"
@@ -15,38 +25,36 @@
1525
(**
1626
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/fsprojects/FSharp.Control.AsyncSeq/gh-pages?filepath=AsyncSeq.ipynb)
1727
18-
# F# Async: FSharp.Control.AsyncSeq
28+
# F# Asynchronous Sequences
1929
20-
> NOTE: There is also the option to use [FSharp.Control.TaskSeq](https://github.com/fsprojects/FSharp.Control.TaskSeq) which has a very similar usage model.
30+
An asynchronous sequence is a sequence in which individual elements are _awaited_, so the next element of the sequence is not necessarily available immediately. This allows for efficient composition of asynchronous workflows which involve sequences of data.
2131
22-
An AsyncSeq is a sequence in which individual elements are retrieved using an `Async` computation.
23-
It is similar to `seq<'a>` in that subsequent elements are pulled on-demand.
24-
`AsyncSeq` also bears similarity to `IObservable<'a>` with the former being based on an "asynchronous pull" and the
25-
latter based on a "synchronous push". Analogs for most operations defined for `Seq`, `List` and `IObservable` are also defined for
26-
`AsyncSeq`. The power of `AsyncSeq` lies in that many of these operations also have analogs based on `Async`
27-
allowing composition of complex asynchronous workflows.
32+
The `FSharp.Control.AsyncSeq` library is an implementation of functional asynchronous sequences for F#. The central type of the library is `AsyncSeq<'T>` and is a type alias for `System.Collections.Generic.IAsyncEnumerable<'T>`.
2833
29-
> **v4.0 and later:** `AsyncSeq<'T>` is a type alias for `System.Collections.Generic.IAsyncEnumerable<'T>`.
30-
> Any `IAsyncEnumerable<'T>` value (e.g. from EF Core, ASP.NET Core channels, or `taskSeq { }`) can be used
31-
> directly as an `AsyncSeq<'T>` without conversion, and vice-versa.
34+
This library was also [one of the world's first implementations of asynchronous sequences](http://tomasp.net/blog/async-sequences.aspx) and has been used in production for many years. It is a mature library with a rich set of operations defined on `AsyncSeq` and is widely used in the F# community.
3235
33-
The `AsyncSeq` type is located in the `FSharp.Control.AsyncSeq.dll` assembly which can be loaded in F# Interactive as follows:
36+
To use the library, referrence the NuGet package `FSharp.Control.AsyncSeq` in your project and open the `FSharp.Control` namespace:
3437
*)
3538

36-
#r "../../../bin/FSharp.Control.AsyncSeq.dll"
3739
open FSharp.Control
3840

3941
(**
4042
### Generating asynchronous sequences
4143
42-
An `AsyncSeq<'T>` can be generated using computation expression syntax much like `seq<'T>`:
44+
An asynchronous sequence can be generated using a computation expression, much like `seq<'T>`:
4345
*)
4446

4547
let async12 = asyncSeq {
4648
yield 1
4749
yield 2
4850
}
4951

52+
(**
53+
or more succinctly:
54+
*)
55+
56+
let async12b = asyncSeq { 1; 2 }
57+
5058
(**
5159
Another way to generate an asynchronous sequence is using the `Async.unfoldAsync` function. This
5260
function accepts as an argument a function which can generate individual elements based on a state and
@@ -72,7 +80,7 @@ type Tweet = {
7280
}
7381

7482
let getTweetBatch (offset: int) : Async<(Tweet[] * int) option> =
75-
failwith "TODO: call Twitter API"
83+
async { return failwith "TODO: call Twitter API" }
7684

7785
let tweetBatches : AsyncSeq<Tweet[]> =
7886
AsyncSeq.unfoldAsync getTweetBatch 0
@@ -136,14 +144,16 @@ corresponding `IEnumerator`. An `AsyncSeq` on the other hand can use facilities
136144
more efficient use of system resources.
137145
*)
138146

147+
open System.Threading
148+
139149
let withTime = seq {
140150
Thread.Sleep(1000) // calling thread will block
141151
yield 1
142152
Thread.Sleep(1000) // calling thread will block
143153
yield 1
144154
}
145155

146-
let withTime' = asyncSeq {
156+
let withTime2 = asyncSeq {
147157
do! Async.Sleep 1000 // non-blocking sleep
148158
yield 1
149159
do! Async.Sleep 1000 // non-blocking sleep

docs/AsyncSeqExamples.fsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
(**
2+
---
3+
title: F# AsyncSeq Examples
4+
category: Documentation
5+
categoryindex: 2
6+
index: 2
7+
description: Examples demonstrating the use of F# asynchronous sequences.
8+
keywords: F#, asynchronous sequences, AsyncSeq, examples
9+
---
10+
*)
111
(*** condition: prepare ***)
212
#nowarn "211"
313
#I "../src/FSharp.Control.AsyncSeq/bin/Release/netstandard2.1"

docs/ComparisonWithObservable.fsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
(**
2+
---
3+
title: Comparison with IObservable
4+
category: Documentation
5+
categoryindex: 2
6+
index: 3
7+
description: A comparison of F# asynchronous sequences with IObservable.
8+
keywords: F#, asynchronous sequences, AsyncSeq, IObservable, reactive
9+
---
10+
*)
111
(*** condition: prepare ***)
212
#nowarn "211"
313
#I "../src/FSharp.Control.AsyncSeq/bin/Release/netstandard2.1"

tests/fable/FSharp.Control.AsyncSeq.Tests/AsyncSeq.test.fs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ Jest.describe("AsyncSeq.interleave", fun () ->
404404
})
405405
)
406406

407-
Jest.describe("AsyncSeq.bufferBy", fun () ->
408-
Jest.test("AsyncSeq.bufferByCount", async {
407+
Jest.describe("AsyncSeq.chunkBy", fun () ->
408+
Jest.test("AsyncSeq.chunkBySize", async {
409409
let! actual =
410410
asyncSeq {
411411
yield 1
@@ -414,31 +414,31 @@ Jest.describe("AsyncSeq.bufferBy", fun () ->
414414
yield 4
415415
yield 5
416416
}
417-
|> AsyncSeq.bufferByCount 2 |> AsyncSeq.toArrayAsync
417+
|> AsyncSeq.chunkBySize 2 |> AsyncSeq.toArrayAsync
418418
let expected = [|[|1;2|];[|3;4|];[|5|]|]
419419

420420
Jest.expect(actual).toEqual(expected)
421421
Jest.expect(actual).toHaveLength(expected.Length)
422422
})
423423

424-
Jest.test("AsyncSeq.bufferByCount various sizes", async {
424+
Jest.test("AsyncSeq.chunkBySize various sizes", async {
425425
for sz in 0 .. 10 do
426426
let! actual =
427427
asyncSeq {
428428
for i in 1 .. sz do
429429
yield i
430430
}
431-
|> AsyncSeq.bufferByCount 1 |> AsyncSeq.toArrayAsync
431+
|> AsyncSeq.chunkBySize 1 |> AsyncSeq.toArrayAsync
432432
let expected = [|for i in 1 .. sz -> [|i|]|]
433433

434434
Jest.expect(actual).toEqual(expected)
435435
Jest.expect(actual).toHaveLength(expected.Length)
436436
})
437437

438-
Jest.test("AsyncSeq.bufferByCount empty", async {
438+
Jest.test("AsyncSeq.chunkBySize empty", async {
439439
let! actual =
440440
AsyncSeq.empty<int>
441-
|> AsyncSeq.bufferByCount 2
441+
|> AsyncSeq.chunkBySize 2
442442
|> AsyncSeq.toArrayAsync
443443
let expected = [||]
444444

@@ -862,22 +862,6 @@ Jest.test("AsyncSeq.while should allow do at end", async {
862862
Jest.expect(x.Value).toBe(3)
863863
})
864864

865-
Jest.test("AsyncSeq.getIterator should work", async {
866-
let s1 = [1..2] |> AsyncSeq.ofSeq
867-
use i = s1.GetEnumerator()
868-
869-
match! i.MoveNext() with
870-
| None as v -> Jest.expect(v).toBeDefined()
871-
| Some v ->
872-
Jest.expect(v).toBe(1)
873-
874-
match! i.MoveNext() with
875-
| None as v -> Jest.expect(v).toBeDefined()
876-
| Some v ->
877-
Jest.expect(v).toBe(2)
878-
do! Jest.expect(i.MoveNext()).toBeUndefined()
879-
})
880-
881865
Jest.test("asyncSeq.For should delay", async {
882866
let (s: seq<int>) =
883867
{ new System.Collections.Generic.IEnumerable<int> with

0 commit comments

Comments
 (0)