Esempio n. 1
0
 public static IArrayLookup <K, U> GroupBy <T, U, K>(this IArray <T> self, Func <T, int, K> keySelector,
                                                     Func <T, int, U> elementSelector)
 {
     return
         (self.Aggregate(new ArrayLookupBuilder <K, U>(),
                         (gb, x, i) => gb.Add(keySelector(x, i), elementSelector(x, i))).ToLookup());
 }
Esempio n. 2
0
        public static void CheckTens(IArray <int> tens)
        {
            Assert.IsTrue(tens.SequenceEquals(ArrayToTen.ToIArray()));
            Assert.AreEqual(0, tens.First());
            Assert.AreEqual(9, tens.Last());
            Assert.AreEqual(45, tens.Aggregate(0, (a, b) => a + b));
            Assert.AreEqual(10, tens.Count);
            Assert.AreEqual(5, tens[5]);
            Assert.AreEqual(5, tens.ElementAt(5));

            var ones  = 1.Repeat(9);
            var diffs = tens.ZipEachWithNext((x, y) => y - x);

            Assert.IsTrue(ones.SequenceEquals(diffs));
            Assert.IsFalse(ones.SequenceEquals(tens));

            var indices = tens.Indices();

            Assert.IsTrue(tens.SequenceEquals(indices));
            Assert.IsTrue(tens.SequenceEquals(tens.SelectByIndex(indices)));
            Assert.IsTrue(tens.Reverse().SequenceEquals(tens.SelectByIndex(indices.Reverse())));

            var sum = 0;

            foreach (var x in tens.ToEnumerable())
            {
                sum += x;
            }
            foreach (var x in tens.ToEnumerable())
            {
                Console.WriteLine(x.ToString());
            }
            Assert.AreEqual(45, sum);
            Assert.AreEqual(0, tens.First());
            Assert.True(tens.All(x => x < 10));
            Assert.True(tens.Any(x => x < 5));
            Assert.AreEqual(5, tens.CountWhere(x => x % 2 == 0));
            Assert.AreEqual(0, tens.Reverse().Last());
            Assert.AreEqual(0, tens.Reverse().Reverse().First());
            var split = tens.Split(LinqArray.Create(3, 6));

            Assert.AreEqual(3, split.Count);
            var counts = split.Select(x => x.Count);

            Assert.True(counts.SequenceEquals(LinqArray.Create(3, 3, 4)));
            var indices2 = counts.Accumulate((x, y) => x + y);

            Assert.True(indices2.SequenceEquals(LinqArray.Create(3, 6, 10)));
            var indices3 = counts.PostAccumulate((x, y) => x + y);

            Assert.True(indices3.SequenceEquals(LinqArray.Create(0, 3, 6, 10)));
            var flattened = split.Flatten();

            Assert.True(flattened.SequenceEquals(tens));
        }
Esempio n. 3
0
 public static IArrayLookup <K, T> GroupBy <T, K>(this IArray <T> self, Func <T, K> keySelector)
 {
     return(self.Aggregate(new ArrayLookupBuilder <K, T>(), (gb, x) => gb.Add(keySelector(x), x)).ToLookup());
 }
Esempio n. 4
0
 public static double Sum(this IArray <double> self)
 {
     return(self.Aggregate(0.0, (a, b) => a + b));
 }
Esempio n. 5
0
 public static float Sum(this IArray <float> self)
 {
     return(self.Aggregate(0.0f, (a, b) => a + b));
 }
Esempio n. 6
0
 public static long Sum(this IArray <long> self)
 {
     return(self.Aggregate(0L, (a, b) => a + b));
 }
Esempio n. 7
0
 public static int Sum(this IArray <int> self)
 {
     return(self.Aggregate(0, (a, b) => a + b));
 }
Esempio n. 8
0
 public static double Sum(this IArray <float> self)
 {
     return(self.Aggregate(0.0, (x, y) => x + y));
 }
Esempio n. 9
0
 public static Vector4 Sum(this IArray <Vector4> self) => self.Aggregate(Vector4.Zero, (x, y) => x + y);
Esempio n. 10
0
 public static double Sum(this IArray <double> self) => self.Aggregate(0.0, (x, y) => x + y);
Esempio n. 11
0
 public static long Sum(this IArray <long> self) => self.Aggregate(0L, (x, y) => x + y);
Esempio n. 12
0
 public static Vector3 Average(IArray <Vector3> xs)
 => xs.Aggregate(Vector3.Zero, (a, b) => a + b) / xs.Count;
Esempio n. 13
0
 public static Vector3 Sum(this IArray <Vector3> self)
 {
     return(self.Aggregate(Vector3.Zero, (x, y) => x + y));
 }
Esempio n. 14
0
 public static string JoinStrings <T>(this IArray <T> self, string sep = ",")
 {
     return(self.Aggregate(new StringBuilder(), (sb, x, i) => (i > 0 ? sb.Append(sep) : sb).Append(x)).ToString());
 }
Esempio n. 15
0
 public static int Count <T>(this IArray <T> self, Func <T, bool> p)
 {
     return(self.Aggregate(0, (a, b) => a + (p(b) ? 1 : 0)));
 }
Esempio n. 16
0
 public static Vector3 Sum(this IArray <Vector3> self)
 {
     return(self.Aggregate(Vector3.Zero, (a, b) => a + b));
 }
Esempio n. 17
0
 public static long Sum(this IArray <long> self)
 {
     return(self.Aggregate(0L, (x, y) => x + y));
 }