Last active
August 29, 2015 14:26
-
-
Save lobrien/74202cc98e23c44f3c43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
string output = ""; | |
bool reset = false; | |
int input = 15; | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
// Perform any additional setup after loading the view, typically from a nib. | |
/* | |
If reset is true, clear the output and set reset to false | |
*/ | |
var clearRule = GKRule.FromPredicate ((rules) => reset, rules => { | |
output = ""; | |
reset = false; | |
}); | |
clearRule.Salience = 1; | |
var fizzRule = GKRule.FromPredicate (mod (3), rules => { | |
output += "fizz"; | |
}); | |
fizzRule.Salience = 2; | |
var buzzRule = GKRule.FromPredicate (mod (5), rules => { | |
output += "buzz"; | |
}); | |
buzzRule.Salience = 2; | |
/* | |
This *always* evaluates to true, but is higher Salience, so evaluates after lower-salience items | |
(which is counter-intuitive). | |
print the output, and reset (thus triggering "ResetRule" next time) | |
*/ | |
var outputRule = GKRule.FromPredicate (rules => true, rules => { | |
System.Console.WriteLine(output == "" ? input.ToString() : output); | |
reset = true; | |
}); | |
outputRule.Salience = 3; | |
var rs = new GKRuleSystem (); | |
rs.AddRules (new [] { | |
clearRule, | |
fizzRule, | |
buzzRule, | |
outputRule | |
}); | |
for (input = 1; input < 16; input++) { | |
rs.Evaluate (); | |
rs.Reset (); | |
} | |
} | |
protected Func<GKRuleSystem, bool> mod(int m) | |
{ | |
Func<GKRuleSystem,bool> partiallyApplied = (rs) => input % m == 0; | |
return partiallyApplied; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment