PolyLink
A library to allow manipulation of geometry from within Mathematica
 All Classes Namespaces Files Functions Variables Properties
GeneralExtensions.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using MoreLinq;
7 
8 namespace PolyLink
9 {
13  public static class GeneralExtensions
14  {
21  public static string Fmt(this String s, params object[] args)
22  {
23  return String.Format(s, args);
24  }
25 
31  public static IEnumerable<T1> Print<T1>(this IEnumerable<T1> enumerable, Func<T1, string> selector)
32  {
33  foreach (var i in enumerable)
34  {
35  Console.WriteLine(selector(i));
36  yield return i;
37  }
38  }
39 
40 
44  public static IEnumerable<Tuple<T,T>> CirclularPairs<T>(this IEnumerable<T> list)
45  {
46  var prevItem = list.First();
47 
48  foreach (var item in list.Skip(1))
49  {
50  yield return new Tuple<T, T>(prevItem, item);
51  prevItem = item;
52  }
53  yield return new Tuple<T, T>(list.Last(), list.First());
54  }
55 
61  public static IEnumerable<T[]> CircularTuples<T>(this IEnumerable<T> list, int size)
62  {
63  var bakedList = list.ToList();
64 
65  foreach (var i in Enumerable.Range(0, bakedList.Count))
66  {
67  var array = new T[size];
68  foreach (var j in Enumerable.Range(0, size))
69  {
70  int index = i + j;
71  if (index >= bakedList.Count)
72  {
73  index -= bakedList.Count;
74  }
75  array[j] = bakedList[index];
76  }
77  yield return array;
78  }
79  }
80 
84  public static bool EqualsAny<T>(this T source, params T[] list)
85  {
86  if (null == source) throw new ArgumentNullException("source");
87  return list.Contains(source);
88  }
89 
93  public static IEnumerable<TResult> NetZip<TFirst, TSecond, TResult>
94  (this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
95  {
96  return Enumerable.Zip(first, second, resultSelector);
97  }
98 
102  public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
103  {
104  return new HashSet<T>(source);
105  }
106 
112  public static IEnumerable<T> PrintCount<T>(this IEnumerable<T> source, string enumerableName)
113  {
114  var l = source.ToList();
115  Console.WriteLine(enumerableName + " has " + l.Count + " members");
116  return l;
117  }
118  }
119 }