/// <summary> /// Given a set of points, performs the quickhull algorithm on them to determine a convex set /// </summary> /// <param name="points">An unordered set of points in 2D space</param> /// <returns>The points in order of the convex hull</returns> public static IEnumerable<Vector2> Quickhull(this IEnumerable<Vector2> points) { Vector2 min = points.Aggregate((a, b) => a.X < b.X ? a : b); Vector2 max = points.Aggregate((a, b) => a.X > b.X ? a : b); Vector2 dir = Vector2.Normalize(max - min); List<Vector2> above = new List<Vector2>(); List<Vector2> below = new List<Vector2>(); foreach (var point in points) { if (point == min || point == max) continue; if (IsLeftOfLine(point, min, dir)) above.Add(point); else below.Add(point); } yield return min; foreach (var point in Quickhull(above, min, dir, max)) yield return point; yield return max; foreach (var point in Quickhull(below, min, dir, max)) yield return point; }
public static string Merge(this IEnumerable< string> strings, string separator) { #if NET4 Contract.Requires(strings!= null); #else if (strings == null) throw new ArgumentNullException("strings"); #endif if (separator == null) return strings.Aggregate(new StringBuilder(), (x, y) => x.Append(y)).ToString(); var stringBuilder = strings.Aggregate(new StringBuilder(), (x, y) => x.Append(y).Append(separator)); return (stringBuilder.Length >= separator.Length) ? stringBuilder.ToString(0, stringBuilder.Length - separator.Length) : stringBuilder.ToString(); }
public static float Variance(this IEnumerable<float> source) { float avg = source.Average(); float d = source.Aggregate(0.0f, (total, next) => total + (float) Math.Pow(next - avg, 2)); return d/(source.Count() - 1); }
public static string AggregateAppend(this IEnumerable<string> strings, bool newLine) { return strings.Aggregate(new StringBuilder(), (builder, next) => newLine ? builder.AppendLine(next) : builder.AppendFormat("{0} ", next)) .ToString(); }
public static BigInteger Sum(this IEnumerable<BigInteger> source) { if (source == null) throw new ArgumentNullException(("source")); return source.Aggregate<BigInteger, BigInteger>(0, (a, x) => a + x); }
public static string ToDiscordMessage(this IEnumerable<Command> commands) { var categoryNames = new List<string>(); var categories = new List<Category>(); categoryNames.AddRange(commands.Aggregate(new List<string>(), (accumulated, next) => { accumulated.Add(next.Category); return accumulated; })); categories.AddRange(categoryNames.Distinct().Aggregate(new List<Category>(), (accumulated, next) => { accumulated.Add(new Category { Name = next, Commands = commands.Where(c => c.Category == next).ToList() }); return accumulated; })); var buffer = "Here is a list of available commands you can use to interact with me:\n\n"; foreach (var category in categories) { buffer += string.Format("**{0}**\n\n", category.Name); foreach (var command in category.Commands) { buffer += string.Format("**!{0}** {1} - {2}\n", command.Text, string.Concat(command.Parameters.Select(r => string.Format("**[{0}]** ", r.Name))), command.Description); } buffer += "\n"; } return buffer; }
/// <summary> /// Gets the greatest common denominator of all the integers in the source collection. /// </summary> /// <param name="source">The collection of integers.</param> /// <returns>The greatest common denominator.</returns> public static int GreatestCommonDenominator(this IEnumerable<int> source) { if ((object)source == null) throw new ArgumentNullException("source", "source is null"); return source.Aggregate(GreatestCommonDenominator); }
public static int Hash(this byte[] buffer) { unchecked { return buffer.Aggregate(0, (current, b) => (current * 31) ^ b); } }
public static string Flatten(this IEnumerable<string> list, string separator) { if (list == null) throw new ArgumentNullException("list"); if (separator == null) throw new ArgumentNullException("separator"); string result = list.Aggregate(String.Empty, (acc, next) => acc + next + separator); return result.Left(result.Length - separator.Length); }
public static string AppendStrings(this IEnumerable<string> list, string seperator = ", ") { return list.Aggregate( new StringBuilder(), (sb, s) => (sb.Length == 0 ? sb : sb.Append(seperator)).Append(s), sb => sb.ToString()); }
// Get center of path public static Vector2 Center(this List<Vector2> points) { Vector2 result = points.Aggregate(Vector2.Zero, (current, point) => current + point); result /= points.Count; return result; }
public static string GetHtmlTable(this List<Guest> guests) { var table = guests.Aggregate("<table>", (current, guest) => current + string.Format("<tr><td>{0}</td><td>{1}</td></tr>", guest.UserName, guest.Message)); table += "</table>"; return table; }
public static string BuildQueryString(this IDictionary<string, string> qry) { if (qry == null) return string.Empty; if (qry.Count < 1) return string.Empty; var str = qry.Aggregate("?", (current, pair) => current + pair.Key + "=" + Uri.EscapeDataString(pair.Value) + "&"); return str.Remove(str.Length - 1); }
public static string Implode(this IEnumerable<string> array, string glue) { var result = array.Aggregate(string.Empty, (current, element) => current + (glue + element)); if (result.Length > glue.Length) result = result.Substring(glue.Length); return result; }
public static bool AreNullOrBlank(this IEnumerable<string> values) { if (values.Count() == 0 || values == null) { return false; } return values.Aggregate(true, (current, value) => current & value.IsNullOrBlank()); }
public static string ToVerbatim(this string s) { return s.Aggregate( new StringBuilder("@\"", s.Length * 2), ToVerbatimEscapes) .Append("\"") .ToString(); }
public static string StringifyScopes(this IEnumerable<string> scopes) { if (scopes == null || !scopes.Any()) { return null; } return scopes.Aggregate((s1, s2) => s1 + "," + s2); }
public static string AggregatePaths(this IEnumerable<string> source, string currentDirectory) { if (!source.Any()) return ""; var path = source.Aggregate("", Path.Combine); return CanonicalizePath(path, currentDirectory); }
public static double Mean(this IEnumerable<int> source) { var x = source.Aggregate(Tuple.Create(0, default(int)), (arg1, arg2) => Tuple.Create(arg1.Item1 + 1, arg1.Item2 + arg2)); if (x.Item1 == 0) { return 0; } return x.Item2/(double)x.Item1; }
/// <summary> /// Convert a byte array into a hexadecimal String representation. /// </summary> /// <param name="bytes"></param> /// <returns></returns> /// <remarks></remarks> public static String BytesToHexString(this Byte[] bytes) { //var result = String.Empty; //foreach (byte b in bytes) // result += " " + b.ToString("X").PadLeft(2, '0'); var result = bytes.Aggregate(String.Empty, (aggregate, b) => aggregate + (" " + b.ToString("X").PadLeft(2, '0'))); if (result.Length > 0) result = result.Substring(1); return result; }
public static string ToStringComplete(this IEnumerable<string> enumerable, string delimiter = "") { string longString = string.Empty; if(string.IsNullOrEmpty(delimiter)) { delimiter = System.Environment.NewLine; } return enumerable.Aggregate((i, j) => i + delimiter + j); }
public static Teacher EasiestGrader(this IEnumerable<Teacher> instructors) { return instructors.Aggregate((max, current) => { if (max.AverageStudentGrade() > current.AverageStudentGrade()) return max; else return current; }); }
public static TableSchema Avg( this IMslScript script, DataColumn from, DataColumn into ) { if ( into == null ) { TempTable.RewriteOwnerId( from ).Create(); into = TempTable[ "value" ]; } return script.Aggregate( from, into, values => values.Average() ); }
public static Teacher HardestGrader(this IEnumerable<Teacher> instructors) { return instructors.Aggregate((min, current) => { if (min.AverageStudentGrade() < current.AverageStudentGrade()) return min; else return current; }); }
public static string MaskText(this string text, int position, int count) { const char space = ' '; var min = position; var max = position + count - 1; return text.Aggregate(string.Empty, (g, c) => g + (g.Length >= min && g.Length <= max ? space : c)); }
public static string ToJsArray(this IEnumerable<TableRow> rows) { return string.Format( "{0}]", rows.Aggregate( string.Empty, (s, tr) => string.IsNullOrWhiteSpace(s) ? string.Format("[{0}", tr.ToJsArray()) : string.Format("{0}, {1}", s, tr.ToJsArray()))); }
/// <summary> /// Returns the longest string. /// </summary> public static string Longest(this IEnumerable<string> strings) { if (strings == null) { return string.Empty; } else { return strings.Aggregate(string.Empty, Longer); } }
public static IEnumerable<String> AsSqlServerKeywords( this IEnumerable<String> keywords ) { return keywords.Aggregate( new List<String>(), ( acc, kw ) => { var tmp = kw.AsSqlServerKeyword(); acc.Add( tmp ); return acc; } ) .AsReadOnly(); }
public static string Concat(this string[] input, string addBefore, string addAfter) { string output = string.Empty; if (input != null && input.Count() > 0) output = input.Aggregate<string, StringBuilder>(new StringBuilder(), (x, y) => x.AppendFormat("{0}{1}{2}", addBefore ?? string.Empty, y, addAfter ?? string.Empty)) .ToString(); if (!string.IsNullOrEmpty(addAfter) && output.EndsWith(addAfter)) output = output.Substring(0, (output.Length - addAfter.Length)); return output; }
internal static void ScenarioFailedIfAny(this IEnumerable<LambdaExpression> failed) { failed = failed.ToList(); if(failed.Any()) { throw new ScenarioException("The following assertions failed:" + Environment.NewLine + failed.Aggregate( new StringBuilder(), (builder, assertion) => builder.Append('\t').Append(PAssertFormatter.CreateSimpleFormatFor(assertion)).AppendLine())); } }