Last active
August 29, 2015 14:10
-
-
Save pirrmann/b4dd7b54cf35c4fbc037 to your computer and use it in GitHub Desktop.
Scoped rules builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Nov 27, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment