using System; using System.Collections.Generic; using System.Collections.Concurrent; using System.Linq; using System.Text; namespace csmic.Extensions { /// CS-MIC extension methods. public static class CSMICExtension { #region String /// A string extension method that interprets as input the string that calls it. /// The input to act on. /// The output from the interpretation of the string. public static string Interpret(this string input) { InputInterpreter interpreter = new InputInterpreter(); interpreter.Interpret(input); return interpreter.Output; } /// A string extension method that executes as macro operation. /// The script to act on. /// The final output of the script. public static string RunAsMacro(this string script) { MacroBuilder macro = new MacroBuilder(script, new InputInterpreter()); return macro.FinalOutput; } #endregion #region IEnumerable /// /// A string extension method that interprets as input the string that calls it. /// /// The collection to act on. /// The output from the interpretation of the string. public static IEnumerable Interpret(this IEnumerable collection) { List computed = new List(); InputInterpreter interpreter = new InputInterpreter(); foreach (string input in collection) { interpreter.Interpret(input); computed.Add(interpreter.Output); } return computed; } /// /// A string extension method that interprets as input the string that calls it. /// /// The collection to act on. /// The action. /// The output from the interpretation of the string. public static IEnumerable Interpret(this IEnumerable collection, Action action) { List computed = new List(); InputInterpreter interpreter = new InputInterpreter(); foreach (string input in collection) { interpreter.Interpret(input); computed.Add(interpreter.Output); action(interpreter); } return computed; } /// Enumerates input in this collection, returning a selection on the interpreter. /// Generic type parameter. /// The collection to act on. /// The selection. /// /// An enumerator that allows foreach to be used to process interpret< t> in this /// collection. /// public static IEnumerable Interpret(this IEnumerable collection, Func selection) { List computed = new List(); InputInterpreter interpreter = new InputInterpreter(); foreach (string input in collection) { interpreter.Interpret(input); computed.Add(selection(interpreter)); } return computed; } /// A string extension method that executes as macro operation. /// The collection to act on. /// The final output of the script. public static MacroBuilder RunAsMacro(this IEnumerable collection) { return new MacroBuilder(string.Join(Environment.NewLine, collection.ToArray()), new InputInterpreter()); } #endregion #region IEnumerable In Parallel /// Enumerates input in parallel in this collection. /// The collection to act on. /// /// An enumerator that allows foreach to be used to process interpret in parallel in this /// collection. /// public static IEnumerable InterpretInParallel(this IEnumerable collection) { ConcurrentBag bag = new ConcurrentBag(); collection.AsParallel().ForAll(input => { InputInterpreter interpreter = new InputInterpreter(); interpreter.Interpret(input); bag.Add(interpreter); }); return bag; } /// Enumerates input in parallel this collection, returning a selection on the interpreter. /// Generic type parameter. /// The collection to act on. /// The selection. /// /// An enumerator that allows foreach to be used to process interpret in parallel< t> in /// this collection. /// public static IEnumerable InterpretInParallel(this IEnumerable collection, Func selection) { ConcurrentBag bag = new ConcurrentBag(); collection.AsParallel().ForAll(input => { InputInterpreter interpreter = new InputInterpreter(); interpreter.Interpret(input); bag.Add(selection(interpreter)); }); return bag; } #endregion #region ParallelQuery /// /// A string extension method that interprets as input the strings that calls it in parallel. /// /// The collection to act on. /// The output from the interpretation of the string. public static IEnumerable Interpret(this ParallelQuery collection) { ConcurrentBag bag = new ConcurrentBag(); collection.ForAll(input => { InputInterpreter interpreter = new InputInterpreter(); interpreter.Interpret(input); bag.Add(interpreter); }); return bag; } #endregion } }