Sanchime.Learn/Monads/Test.fs

30 lines
753 B
Forth
Raw Permalink Normal View History

2022-07-06 21:15:51 +08:00
namespace Sanchime.Monads
module Test =
2022-07-06 21:48:36 +08:00
let reader_example () =
let cache (Reader(f)) = (Reader (fun (v1, v2) ->
match v1 with
| None -> f v2
| Some v -> v))
let read s n = reader {
printfn "读取:%s" s
return n
}
let m = reader {
let! a = cache <| read "A" 1
let! b = cache <| read "B" 2
let! c = read "C" 3
return a + b + c
}
let (Reader f) = m
f ((None, ()), ((None, ()), ((), ()))) |> ignore
f ((Some 1, ()), ((None, ()), ((), ()))) |> ignore
f ((None, ()), ((Some 2, ()), ((), ()))) |> ignore
f ((Some 1, ()), ((Some 2, ()), ((), ()))) |> ignore