Sanchime.Learn/Monads/Test.fs

30 lines
753 B
Forth

namespace Sanchime.Monads
module Test =
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