I am trying to use a Computation Expression to build a list of actions. I need to bind to a value that gets returned from the getFood
action so that I can register a later step to consume it.
type Food =
| Chicken
| Rice
type Step =
| GetFood of Food
| Eat of Food
| Sleep of duration:int
type Plan = Plan of Step list
type PlanBuilder () =
member this.Bind (plan:Plan, f) =
f plan
member this.Yield _ = Plan []
member this.Run (Plan x) = Plan (List.rev x)
[<CustomOperation("eat")>]
member this.Eat (Plan p, food) =
printfn "Eat"
Plan ((Eat food)::p)
[<CustomOperation("sleep")>]
member this.Sleep (Plan p, duration) =
printfn "Sleep"
Plan ((Sleep duration)::p)
let plan = PlanBuilder()
let rng = System.Random(123)
let getFood (Plan p) =
printfn "GetFood"
let randomFood =
if rng.NextDouble() > 0.5 then
Food.Chicken
else
Food.Rice
(Plan ((GetFood randomFood)::p)), randomFood
let testPlan =
plan {
let! food = getFood // <-- This is what I am trying to get to work
sleep 10
eat food
}
I believe the problem is with the Bind
but I can't figure out what it is.
(*
Example result
testPlan =
(GetFood Chicken,(
(Sleep 10,(
EatFood Chicken
))
))
*)