Skip to content

Instantly share code, notes, and snippets.

@ninjarobot
Created April 16, 2022 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninjarobot/883ffad7f103e846e0583e4c05e8fa94 to your computer and use it in GitHub Desktop.
Save ninjarobot/883ffad7f103e846e0583e4c05e8fa94 to your computer and use it in GitHub Desktop.
Execution plan Computation Expressions
type Step =
| Action of string
| Branch of Step list list
type Plan =
{
Name : string
Steps : Step list
}
// The step and branch DSL members can be simple functions
let step name = Action name
let branch steps = Branch steps
type PlanBuilder () =
member _.Yield _ =
{
Name = ""
Steps = []
}
member _.Run state = state
[<CustomOperation "name">]
member _.Name (state, name) =
{ state with Name = name }
[<CustomOperation "steps">]
member _.Steps (state, steps) =
{ state with Steps = steps }
let plan = PlanBuilder ()
let x =
plan {
name "Chicken"
steps [
// The compiler reports errors for all the
// `step` and `branch` calls
step "1"
step "2"
branch [
[
step "3a"
step "4a"
]
[
step "3b"
step "4b"
]
]
step "5"
]
}
type Step =
| Action of string
| Branch of Step list list
type Plan =
{
Name : string
Steps : Step list
}
// The step and branch DSL members can be builders of their own
type ActionBuilder () =
member _.Yield _ =
Action ""
[<CustomOperation "name">]
member _.Name (_:Step, name) =
Action name
let action = ActionBuilder ()
type BranchBuilder () =
member _.Yield _ =
Branch []
[<CustomOperation "steps">]
member _.Steps (_:Step, steps:Step list list ) =
Branch steps
let branch = BranchBuilder ()
type PlanBuilder () =
member _.Yield _ =
{
Name = ""
Steps = []
}
[<CustomOperation "name">]
member _.Name (state, name) : Plan =
{ state with Name = name }
[<CustomOperation "steps">]
member _.Steps (state, steps) : Plan =
{ state with Steps = steps }
let plan = PlanBuilder ()
let x =
plan {
name "Chicken"
steps [
action { name "1" }
action { name "2" }
branch {
steps [
[
action { name "3a" }
action { name "4a" }
]
[
action { name "3b" }
action { name "4b" }
]
]
}
action { name "5" }
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment