Last active
August 3, 2017 19:23
-
-
Save noblethrasher/539f71d60b4768ca514aebfe46aa642d to your computer and use it in GitHub Desktop.
Pure OO FizzBuzz
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
using System; | |
using System.Collections.Generic; | |
namespace FizzBuzz | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
FizzBuzz(0); | |
Console.ReadLine(); | |
} | |
static void FizzBuzz(int n) | |
{ | |
n.IsGreaterThanOrEqualTo(0).And(n.IsGreaterThan(100).Not()). | |
IfTrueIfFalse | |
( | |
() => | |
{ | |
var p = (3).Divides(n); | |
var q = (5).Divides(n); | |
p.IfTrue(() => Console.Write("Fizz")); | |
q.IfTrue(() => Console.Write("Buzz")); | |
p.Or(q).IfFalse(() => Console.Write(n.ToString())); | |
Console.WriteLine(); | |
FizzBuzz(n + 1); | |
}, | |
() => | |
{ | |
//done... | |
} | |
); | |
} | |
} | |
static class Utils | |
{ | |
static Dictionary<int, Boolean> memo = new Dictionary<int, Boolean> | |
{ | |
[ 1] = new False(), | |
[ 0] = new True() | |
}; | |
public static Boolean Divides(this int n, int m) | |
{ | |
n = Math.Abs(n); | |
m = Math.Abs(m); | |
Boolean result = new False(); | |
void _Divides(int p) | |
{ | |
p.IsEqualTo(0).IfTrueIfFalse | |
( | |
() => result = new True(), | |
() => | |
{ | |
p.IsGreaterThan(0).IfTrueIfFalse | |
( | |
() => _Divides(p - n), | |
() => { } | |
); | |
} | |
); | |
} | |
_Divides(m); | |
return result; | |
} | |
public static Boolean IsGreaterThanOrEqualTo(this int n, int m) => n.IsGreaterThan(m).Or(m.IsEqualTo(m)); | |
public static Boolean IsGreaterThan(this int n, int m) | |
{ | |
try | |
{ | |
var p = Math.Max(0, n - m); | |
return memo[(p / p)].Not(); | |
} | |
catch (DivideByZeroException ex) | |
{ | |
return new False(); | |
} | |
} | |
public static Boolean IsEqualTo(this int n, int m) | |
{ | |
try | |
{ | |
return memo[(n - m) / (n - m)]; | |
} | |
catch (DivideByZeroException ex) | |
{ | |
return new True(); | |
} | |
} | |
} | |
abstract class Boolean | |
{ | |
public abstract void IfTrue(Action action); | |
public abstract void IfFalse(Action action); | |
public abstract void IfTrueIfFalse(Action if_true, Action if_false); | |
public abstract Boolean Or(Boolean other); | |
public abstract Boolean And(Boolean other); | |
public abstract Boolean Not(); | |
} | |
sealed class True : Boolean | |
{ | |
public override void IfFalse(Action action) { } | |
public override void IfTrue(Action action) => action(); | |
public override void IfTrueIfFalse(Action if_true, Action if_false) => if_true(); | |
public override Boolean And(Boolean other) => other; | |
public override Boolean Or(Boolean other) => this; | |
public override Boolean Not() => new False(); | |
} | |
sealed class False : Boolean | |
{ | |
public override void IfFalse(Action action) => action(); | |
public override void IfTrue(Action action) { } | |
public override void IfTrueIfFalse(Action if_true, Action if_false) => if_false(); | |
public override Boolean And(Boolean other) => this; | |
public override Boolean Or(Boolean other) => other; | |
public override Boolean Not() => new True(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment