The answer to life, the universe, and everything is well-known as 42. There are a myriad of questions that lead to this answer, and my favorite is perhaps the percent of leet pi. I wanted to be able to write this in C# as close to English as possible. Since I had no way to write my own keywords, the closest I could devise was the following bit of code.
double percent = 0.01;
var answer = percent.Of().Leet().Pi();
The tricky part here is deciding what to do about the Of() method. It represents an operation, and it needs the values on the left and the right. My first thought was to create an operation struct, which I would represent in C# by wrapping the value and the operation so the next method could utilize it.
public struct Operation<T>
{
private readonly T value;
private readonly Operator op;
public T Value
{
get { return this.value; }
}
public Operator Operator
{
get { return this.op; }
}
public Operation(T value, Operator op)
{
this.value = value;
this.op = op;
}
public TResult Calculate<T2, TResult>(T2 operand)
{
...
}
}
public enum Operator
{
Add, Subtract, Multiply, Divide
}
This way creates a dependency on an enum, which is bad news. The addition of a new operation would cause the class to be changed. Another way to do this is to pass in a Func instead of an operator. Although it would work, it appeared that I would be forced into hardcoding the generic definitions in code.
Instead, it would be better to utilize extension methods, standard functions, and currying.
public static class Extensions
{
private static Func<double, double, double> multiply = (x, y) => x * y;
public static Func<T2, TResult> Partial<T, T2, TResult>(this Func<T, T2, TResult> func,
T value)
{
return y => func(value, y);
}
public static Func<double, double> Of(this double num)
{
return multiply.Partial(num);
}
public static double Leet(this Func<double, double> func)
{
return func(1337);
}
public static double Pi(this double num)
{
return num * Math.PI;
}
}
The Of extension method uses partial application on the multiply function so that a lower arity function is returned (that is, instead of a function that multiplies two arguments, there is now a function that multiplies one argument by a constant). The numeric extension methods are there so the original statement can be written as designed. The Leet method extends the unary Func<double, double> so that it is called with 1337. Pi extends any double and multiplies it by Math.PI, since in English no operator implies multiplication.
Although I built this to see if I could represent a geeky message in C#, it would be possible to take the currying and DSL elements I’ve shown and build an interesting library. I would leave it up to the user of the library to provide the numeric extensions, aside from standard math constants.
Unfortunately, the answer variable value isn’t rounded to its significant digits making it slightly greater than 42. Perhaps it would be good to create a class to carry necessary information concerning the calculations after all?