Skip to content

Instantly share code, notes, and snippets.

@lobrien
Last active August 29, 2015 14:26
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 lobrien/74202cc98e23c44f3c43 to your computer and use it in GitHub Desktop.
Save lobrien/74202cc98e23c44f3c43 to your computer and use it in GitHub Desktop.
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