using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace csmic.ComputableEngine { /// Computable class. public sealed class Computable { #region Members /// The expression to be built. private string expression; /// The interpreter to act as a base. private InputInterpreter interpreter; #region Constants /// The add symbol. private const string ADD = "+"; /// The substract symbol. private const string SUBSTRACT = "-"; /// The divide symbol. private const string DIVIDE = "/"; /// The multiply symbol. private const string MULTIPLY = "*"; /// The modifier symbol. private const string MOD = "%"; /// The power symbol. private const string POWER = "^"; #endregion #endregion #region Properties /// Gets the expression. /// The expression. internal string Expression { get { return this.expression; } } #endregion #region Constructor /// Creates a Computable instance. /// The expression. /// The interpreter. internal Computable(string expression, InputInterpreter interpreter) { this.expression = expression; this.interpreter = interpreter; } #endregion #region Methods /// Resolves the computable as an input interpreter having calculated the input. /// The computable as an input interpreter. public InputInterpreter Resolve() { this.interpreter.Interpret(this.Expression); return this.interpreter; } /// Resolve the computer to a type . /// Generic type parameter. /// The selector function. /// . public T ResolveTo(Func selector) { return selector(this.Resolve()); } /// Form the operation given the operation constant and an argument. /// The operation constant. /// The argument. /// A string with the given operation appended. private string FormOperation(string operation, object argument) { return string.Format("({0} {1} {2})", this.Expression, operation, argument); } /// Form the function with the current expression as the first argument, followed by . /// The name of the function. /// The arguments. /// . private string FormFunction(string name, object[] arguments) { return string.Format("{0}({1})", name, string.Join(",", this.Expression, arguments)); } /// Adds addend. /// The decimal to add. /// A computable class after the addition. public Computable Add(decimal addend) { this.expression = FormOperation(ADD, addend); return this; } /// Subtracts the subtrahend. /// The subtrahend. /// A computable class after the subtraction. public Computable Subtract(decimal subtrahend) { this.expression = FormOperation(SUBSTRACT, subtrahend); return this; } /// Multiplies the multiplicand. /// The multiplicand. /// A computable class after the mulitplication. public Computable Multiply(decimal multiplicand) { this.expression = FormOperation(MULTIPLY, multiplicand); return this; } /// Divides the divisor. /// The divisor. /// A computable class after the divison. public Computable Divide(decimal divisor) { this.expression = FormOperation(DIVIDE, divisor); return this; } /// Mods using a given divisor. /// The divisor. /// A computable class after the mod. public Computable Mod(decimal divisor) { this.expression = FormOperation(MOD, divisor); return this; } /// Raises to power of the given integer value. /// The power. /// A computable class after the power operation. public Computable RaiseToPower(int power) { this.expression = FormOperation(POWER, power); return this; } /// Applies the function with the current expression as the first argument, followed by . /// The name of the function. /// The arguments. /// A computable class after the function is applied. public Computable ApplyFunction(string name, object[] arguments) { this.expression = FormFunction(name, arguments); return this; } /// Executes a command for all items in a collection. /// Generic type parameter. /// The items of type . /// The action belonging to type . /// . public Computable ForAll(ICollection items, Func> action) { foreach (T item in items) { action(this)(item); } return this; } #endregion } }