Skip to content

Instantly share code, notes, and snippets.

@pirrmann
Last active August 29, 2015 14:10
Show Gist options
  • Save pirrmann/b4dd7b54cf35c4fbc037 to your computer and use it in GitHub Desktop.
Save pirrmann/b4dd7b54cf35c4fbc037 to your computer and use it in GitHub Desktop.
Scoped rules builder
type ScopeAndRules = | ScopeAndRules of string * int list
type RulesBuilder() =
member x.Yield(v) = []
[<CustomOperation("scope", MaintainsVariableSpace=true)>]
member x.Scope(source, scope) = ScopeAndRules(scope, []) :: source
[<CustomOperation("rule", MaintainsVariableSpace=true)>]
member x.Rule(source, rule) =
match source with
| ScopeAndRules(scope, rules) :: tail -> ScopeAndRules(scope, rule :: rules) :: tail
| _ -> failwith "You need to defian a scope before adding a rule"
member x.Run(source) =
source
|> List.rev
|> List.map (fun (ScopeAndRules(s, r)) -> s, (List.rev r))
let rules = new RulesBuilder()
let x = rules {
scope "test"
rule 1
rule 2
scope "test2"
rule 3
rule 4
}
@vbfox
Copy link

vbfox commented Nov 27, 2014

module myFirm
    open xxx;

    [<FirmRules("FirmId")>]
    let rules = rules {
        scope "test"
        rule 1
        rule 2

        scope "test2"
        rule 3
        rule 4
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment