-
Notifications
You must be signed in to change notification settings - Fork 146
/
Caching.fsx
33 lines (26 loc) · 1.06 KB
/
Caching.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// ----------------------------------------------------------------------------
// F# async extensions (Caching.fsx)
// (c) Tomas Petricek, 2011, Available under Apache 2.0 license.
// ----------------------------------------------------------------------------
// This example demonstrates how to use 'Async.Cache' and 'AsyncSeq.cache'
#r @"..\build\FSharpx.Core.dll"
open FSharp.Control
// The Async.Cache combinator makes it possible to create asynchronous
// workflow that caches the result and performs computation only once
// when called multiple times.
let op =
async { // Will be printed just once
printfn "Evaluating..."
return 42 }
|> Async.Cache
Async.RunSynchronously(op)
Async.RunSynchronously(op)
// The AsyncSeq.cache combinator has similar effect - the asynchronous
// sequence can be used multiple times, but it will evaluate just once
let asq =
asyncSeq { for i in 0 .. 10 do
printfn "Generating %d..." i
yield i }
|> AsyncSeq.cache
AsyncSeq.iter (printfn "Consuming %d") asq
|> Async.RunSynchronously