Files
spark/src/client/spark_core/utils/TupleExtensions.cs
T

58 lines
2.0 KiB
C#

namespace bts.utils {
using System;
using System.Collections.Generic;
using System.Numerics;
public static class TupleExtensions {
public static (T1, T2, T3, T4) with<T1, T2, T3, T4>(this (T1, T2) tuple, T3 value3, T4 value4) =>
(tuple.Item1, tuple.Item2, value3, value4);
public static IEnumerable<T> enumerate<T>(this (T, T, T) tuple) {
yield return tuple.Item1;
yield return tuple.Item2;
yield return tuple.Item3;
}
public static IEnumerable<T> flatten<T>(this IEnumerable<(T, T, T)> tuples) {
foreach (var (v1, v2, v3) in tuples) {
yield return v1;
yield return v2;
yield return v3;
}
}
public static bool any(this (bool, bool) tuple) =>
tuple.Item1 || tuple.Item2;
public static bool any(this (bool, bool, bool) tuple) =>
tuple.Item1 || tuple.Item2 || tuple.Item3;
public static bool any(this (bool, bool, bool, bool) tuple) =>
tuple.Item1 || tuple.Item2 || tuple.Item3 || tuple.Item4;
public static bool none(this (bool, bool) tuple) =>
!tuple.any();
public static bool none(this (bool, bool, bool) tuple) =>
!tuple.any();
public static bool none(this (bool, bool, bool, bool) tuple) =>
!tuple.any();
public static T sum<T>(this (T, T) vals) where T : IAdditionOperators<T, T, T> =>
vals.Item1 + vals.Item2;
public static (T, T) ordered<T>(this (T, T) vals, IComparer<T> comparer = null) where T : IComparable<T> {
comparer ??= Comparer<T>.Default;
return comparer.Compare(vals.Item1, vals.Item2) < 0 ?
(vals.Item2, vals.Item1) : vals;
}
public static (T, T) ordered<T>(this (T, T) vals, out bool negated, IComparer<T> comparer = null) where T : IComparable<T> {
comparer ??= Comparer<T>.Default;
return (negated = comparer.Compare(vals.Item1, vals.Item2) > 0) ?
(vals.Item2, vals.Item1) : vals;
}
}
}