Std

Mainly a port of Rust's std.
git clone https://git.philomathiclife.com/repos/Std
Log | Files | Refs | README

Functions.cs (18569B)


      1 using Std.Clone;
      2 using Std.Cmp;
      3 using static Std.Cmp.Ordering;
      4 using Std.Maybe;
      5 using Std.Result;
      6 using Std.Ops;
      7 #region Namespaces
      8 namespace Std.Iter {
      9     #region Types
     10     public static class Functions {
     11 
     12         #region Type-level Constructors
     13         #endregion
     14 
     15         #region Instance Constructors
     16         #endregion
     17 
     18         #region Type-level Fields
     19         #endregion
     20 
     21         #region Instance Fields
     22         #endregion
     23 
     24         #region Type-level Properties
     25         #endregion
     26 
     27         #region Instance Properties
     28         #endregion
     29 
     30         #region Type-level Functions
     31         public static bool All<TSelf, T>(this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: class, IIterator<T> => IIterator<T>.All(ref self, f);
     32         public static bool All<TSelf, T>(ref this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: struct, IIterator<T> => IIterator<T>.All(ref self, f);
     33         public static bool Any<TSelf, T>(this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: class, IIterator<T> => IIterator<T>.Any(ref self, f);
     34         public static bool Any<TSelf, T>(ref this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: struct, IIterator<T> => IIterator<T>.Any(ref self, f);
     35         public static Chain<T, TSelf, TIter> Chain<TSelf, T, TIter>(this TSelf self, TIter other) where T: notnull where TSelf: notnull, IIterator<T> where TIter: notnull, IIterator<T> => new(self, other);
     36         public static Ordering Cmp<TSelf, T, TIter>(this TSelf self, TIter other) where T: notnull, IOrd<T> where TSelf: notnull, IIterator<T> where TIter: notnull, IIterator<T> => self.CmpBy<TSelf, T, TIter, T>(other, (x, y) => x.Cmp(in y));
     37         public static Ordering CmpBy<TSelf, T, TIter, T2>(this TSelf self, TIter other, Fn<T, T2, Ordering> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull where TIter: notnull, IIterator<T2> {
     38 
     39             Maybe<T> val0;
     40             Maybe<T2> val1;
     41             Ordering ord;
     42 
     43             while ((val0 = self.Next()).IsSome) {
     44                 if ((val1 = other.Next()).IsSome) {
     45                     ord = f(val0._some, val1._some);
     46                     if (ord.Var != Ord.Equivalent) { return ord; }
     47                 } else {
     48                     return Greater;
     49                 }
     50             }
     51             return other.Next().IsNone ? Equivalent : Less;
     52         }
     53         public static TFromIter Collect<TSelf, T, TFromIter>(this TSelf self) where T: notnull where TSelf: notnull, IIterator<T> where TFromIter: notnull, IFromIterator<TFromIter, T> => TFromIter.FromIter(self);
     54         public static int CompareToComparable<TSelf, T, TIter>(this TSelf self, TIter other) where T: notnull, System.IComparable<T> where TSelf: notnull, IIterator<T> where TIter: notnull, IIterator<T> {
     55 
     56             Maybe<T> val0;
     57             Maybe<T> val1;
     58             int ord;
     59 
     60             while ((val0 = self.Next()).IsSome) {
     61                 if ((val1 = other.Next()).IsSome) {
     62                     ord = val0._some.CompareTo(val1._some);
     63                     if (ord != 0) { return ord; }
     64                 } else {
     65                     return 1;
     66                 }
     67             }
     68             return other.Next().IsNone ? 0 : -1;
     69         }
     70         public static Cycle<T, TSelf> Cycle<TSelf, T>(this TSelf self) where T: notnull where TSelf: notnull, IIterator<T>, IClone<TSelf> => new(self);
     71         public static Empty<T> Empty<T>() where T: notnull => new();
     72         public static Enumerate<T, TSelf> Enumerate<TSelf, T>(this TSelf self) where T: notnull where TSelf: notnull, IIterator<T> => new(self);
     73         public static bool Eq<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull, IPartialEq<T, T2> where TSelf: notnull, IIterator<T> where T2: notnull, IPartialEq<T2, T> where TIter: notnull, IIterator<T2> => self.EqBy<TSelf, T, TIter, T2>(other, (x, y) => x == y);
     74         public static bool EqBy<TSelf, T, TIter, T2>(this TSelf self, TIter other, Fn<T, T2, bool> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull where TIter: notnull, IIterator<T2> {
     75 
     76             Maybe<T> val0;
     77             Maybe<T2> val1;
     78 
     79             while ((val0 = self.Next()).IsSome) {
     80                 if ((val1 = other.Next()).IsSome) {
     81                     if (!f(val0._some, val1._some)) { return false; }
     82                 } else {
     83                     return false;
     84                 }
     85             }
     86             return other.Next().IsNone;
     87         }
     88         public static bool EqualsEquatable<TSelf, T, TIter>(this TSelf self, TIter other) where T: notnull, System.IEquatable<T> where TSelf: notnull, IIterator<T> where TIter: notnull, IIterator<T> {
     89 
     90             Maybe<T> val0;
     91             Maybe<T> val1;
     92 
     93             while ((val0 = self.Next()).IsSome) {
     94                 if ((val1 = other.Next()).IsSome) {
     95                     if (!val0._some.Equals(val1._some)) { return false; }
     96                 } else {
     97                     return false;
     98                 }
     99             }
    100             return other.Next().IsNone;
    101         }
    102         public static bool EqualsEquality<TSelf, T>(this TSelf self, in TSelf other) where T: notnull, IEquality<T> where TSelf: notnull, IExactSizeIterator<T> {
    103 
    104             if (self.Len() == other.Len()) {
    105                 Maybe<T> val0;
    106                 Maybe<T> val1;
    107 
    108                 while ((val0 = self.Next()).IsSome) {
    109                     if ((val1 = other.Next()).IsSome) {
    110                         if (!val0._some.Equals(in val1._some)) { return false; }
    111                     } else {
    112                         return false;
    113                     }
    114                 }
    115                 return other.Next().IsNone;
    116             } else {
    117                 return false;
    118             }
    119         }
    120         public static Filter<T, TSelf> Filter<TSelf, T>(this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: notnull, IIterator<T> => new(self, f);
    121         public static FilterMap<T, TSelf, T2> FilterMap<TSelf, T, T2>(this TSelf self, Fn<T, Maybe<T2>> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull => new(self, f);
    122         public static Maybe<T> Find<TSelf, T>(this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: class, IIterator<T> => IIterator<T>.Find(ref self, f);
    123         public static Maybe<T> Find<TSelf, T>(ref this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: struct, IIterator<T> => IIterator<T>.Find(ref self, f);
    124         public static Maybe<T2> FindMap<TSelf, T, T2>(this TSelf self, Fn<T, Maybe<T2>> f) where T: notnull where TSelf: class, IIterator<T> where T2: notnull => IIterator<T>.FindMap(ref self, f);
    125         public static Maybe<T2> FindMap<TSelf, T, T2>(ref this TSelf self, Fn<T, Maybe<T2>> f) where T: notnull where TSelf: struct, IIterator<T> where T2: notnull => IIterator<T>.FindMap(ref self, f);
    126         public static FlatMap<T, TSelf, T2, TInnerIter> FlatMap<TSelf, T, TInnerIter, T2>(this TSelf self, Fn<T, TInnerIter> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull where TInnerIter: notnull, IIterator<T2> => new(self, f);
    127         public static Flatten<T, TInnerIter, TSelf> Flatten<TSelf, TInnerIter, T>(this TSelf self) where T: notnull where TInnerIter: notnull, IIterator<T> where TSelf: notnull, IIterator<TInnerIter> => new(self);
    128         public static Maybe<T> FoldFirst<TSelf, T>(this TSelf self, Fn<T, T, T> f) where T: notnull where TSelf: notnull, IIterator<T> {
    129 
    130             var val = self.Next();
    131             return val.IsNone ? val: Maybe<T>.Some(self.Fold(val._some, f));
    132         }
    133         public static Unit ForEach<TSelf, T>(this TSelf self, Fn<T, Unit> f) where T: notnull where TSelf: notnull, IIterator<T> => self.Fold(new Unit(), (_, val) => f(val));
    134         public static FromFn<T> FromFn<T>(Fn<Maybe<T>> f) where T: notnull => new(f);
    135         public static Fuse<T, TSelf> Fuse<TSelf, T>(this TSelf self) where T: notnull where TSelf: notnull, IIterator<T> => new(self);
    136         public static bool Ge<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull, IPartialOrd<T, T2> where TSelf: notnull, IIterator<T> where T2: notnull, IPartialOrd<T2, T> where TIter: notnull, IIterator<T2> {
    137 
    138             var mbe = self.PartialCmp<TSelf, T, TIter, T2>(other);
    139             return mbe.IsSome && mbe._some.Var != Ord.Less;
    140         }
    141         public static bool Gt<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull, IPartialOrd<T, T2> where TSelf: notnull, IIterator<T> where T2: notnull, IPartialOrd<T2, T> where TIter: notnull, IIterator<T2> {
    142 
    143             var mbe = self.PartialCmp<TSelf, T, TIter, T2>(other);
    144             return mbe.IsSome && mbe._some.Var == Ord.Greater;
    145         }
    146         public static Inspect<T, TSelf> Inspect<TSelf, T>(this TSelf self, FnIn<T, Unit> f) where T: notnull where TSelf: notnull, IIterator<T> => new(self, f);
    147         public static bool IsPartitioned<TSelf, T>(this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: notnull, IIterator<T> => IIterator<T>.All(ref self, f) || !IIterator<T>.Any(ref self, f);
    148         public static bool IsSorted<TSelf, T>(this TSelf self) where T: notnull, IPartialOrd<T, T> where TSelf: notnull, IIterator<T> => self.IsSortedBy((in T x, in T y) => x.PartialCmp(in y));
    149         public static bool IsSortedBy<TSelf, T>(this TSelf self, FnIn<T, T, Maybe<Ordering>> f) where T: notnull where TSelf: notnull, IIterator<T> {
    150 
    151             var val = self.Next();
    152 
    153             if (val.IsNone) {
    154                 return true;
    155             } else {
    156                 var prev = val._some;
    157                 T cur;
    158                 Maybe<Ordering> cmp;
    159 
    160                 while ((val = self.Next()).IsSome) {
    161                     cur = val._some;
    162                     cmp = f(in prev, in cur);
    163                     if (cmp.IsNone || cmp._some.Var == Ord.Greater) { return false; }
    164                     prev = cur;
    165                 }
    166                 return true;
    167             }
    168         }
    169         public static bool Le<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull, IPartialOrd<T, T2> where TSelf: notnull, IIterator<T> where T2: notnull, IPartialOrd<T2, T> where TIter: notnull, IIterator<T2> {
    170 
    171             var mbe = self.PartialCmp<TSelf, T, TIter, T2>(other);
    172             return mbe.IsSome && mbe._some.Var != Ord.Greater;
    173         }
    174         public static bool Lt<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull, IPartialOrd<T, T2> where TSelf: notnull, IIterator<T> where T2: notnull, IPartialOrd<T2, T> where TIter: notnull, IIterator<T2> {
    175 
    176             var mbe = self.PartialCmp<TSelf, T, TIter, T2>(other);
    177             return mbe.IsSome && mbe._some.Var == Ord.Less;
    178         }
    179         public static Map<T, TSelf, T2> Map<TSelf, T, T2>(this TSelf self, Fn<T, T2> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull => new(self, f);
    180         public static MapWhile<T, TSelf, T2> MapWhile<TSelf, T, T2>(this TSelf self, Fn<T, Maybe<T2>> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull => new(self, f);
    181         public static Maybe<T> Max<TSelf, T>(this TSelf self) where T: notnull, IOrd<T> where TSelf: notnull, IIterator<T> => self.MaxBy((in T x, in T y) => x.Cmp(in y));
    182         public static Maybe<T> MaxBy<TSelf, T>(this TSelf self, FnIn<T, T, Ordering> f) where T: notnull where TSelf: notnull, IIterator<T> => self.FoldFirst<TSelf, T>((val0, val1) => Std.Cmp.Functions.MaxBy(val0, val1, f));
    183         public static Maybe<T> Min<TSelf, T>(this TSelf self) where T: notnull, IOrd<T> where TSelf: notnull, IIterator<T> => self.MinBy((in T x, in T y) => x.Cmp(in y));
    184         public static Maybe<T> MinBy<TSelf, T>(this TSelf self, FnIn<T, T, Ordering> f) where T: notnull where TSelf: notnull, IIterator<T> => self.FoldFirst<TSelf, T>((val0, val1) => Std.Cmp.Functions.MinBy(val0, val1, f));
    185         public static bool Ne<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull, IPartialEq<T, T2> where TSelf: notnull, IIterator<T> where T2: notnull, IPartialEq<T2, T> where TIter: notnull, IIterator<T2> => !self.Eq<TSelf, T, TIter, T2>(other);
    186         public static Maybe<T> Nth<TSelf, T>(this TSelf self, ulong n) where T: notnull where TSelf: class, IIterator<T> => IIterator<T>.Nth(ref self, n);
    187         public static Maybe<T> Nth<TSelf, T>(ref this TSelf self, ulong n) where T: notnull where TSelf: struct, IIterator<T> => IIterator<T>.Nth(ref self, n);
    188         public static Maybe<T> NthBack<TSelf, T>(this TSelf self, ulong n) where T: notnull where TSelf: class, IDoubleEndedIterator<T> => IDoubleEndedIterator<T>.NthBack(ref self, n);
    189         public static Maybe<T> NthBack<TSelf, T>(ref this TSelf self, ulong n) where T: notnull where TSelf: struct, IDoubleEndedIterator<T> => IDoubleEndedIterator<T>.NthBack(ref self, n);
    190         public static Once<T> Once<T>(T val) where T: notnull => new(new Maybe.IntoIterator<T>(new(val)));
    191         public static OnceWith<T> OnceWith<T>(Fn<T> f) where T: notnull => new(new(f));
    192         public static Maybe<Ordering> PartialCmp<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull, IPartialOrd<T, T2> where TSelf: notnull, IIterator<T> where T2: notnull, IPartialOrd<T2, T> where TIter: notnull, IIterator<T2> => self.PartialCmpBy<TSelf, T, TIter, T2>(other, (x, y) => x.PartialCmp(in y));
    193         public static Maybe<Ordering> PartialCmpBy<TSelf, T, TIter, T2>(this TSelf self, TIter other, Fn<T, T2, Maybe<Ordering>> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull where TIter: notnull, IIterator<T2> {
    194 
    195             Maybe<T> val0;
    196             Maybe<T2> val1;
    197             Maybe<Ordering> ord;
    198 
    199             while ((val0 = self.Next()).IsSome) {
    200                 if ((val1 = other.Next()).IsSome) {
    201                     ord = f(val0._some, val1._some);
    202                     if (ord.IsNone || ord._some.Var != Ord.Equivalent) { return ord; }
    203                 } else {
    204                     return Maybe<Ordering>.Some(Greater);
    205                 }
    206             }
    207             return other.Next().IsNone ? Maybe<Ordering>.Some(Equivalent) : Maybe<Ordering>.Some(Less);
    208         }
    209         public static Prod<TExt, TExt> Partition<TSelf, T, TExt>(this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: notnull, IIterator<T> where TExt: notnull, IExtend<T>, new() {
    210 
    211             var left = new TExt();
    212             var right = new TExt();
    213             _ = self.Fold(new Unit(), (_, val) => f(in val) ? left.ExtendOne(val): right.ExtendOne(val));
    214             return new(left, right);
    215         }
    216         public static Peekable<T, TSelf> Peekable<TSelf, T>(this TSelf self) where T: notnull where TSelf: notnull, IIterator<T> => new(self);
    217         public static Maybe<ulong> Position<TSelf, T>(this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: class, IIterator<T> => IIterator<T>.Position(ref self, f);
    218         public static Maybe<ulong> Position<TSelf, T>(this ref TSelf self, Fn<T, bool> f) where T: notnull where TSelf: struct, IIterator<T> => IIterator<T>.Position(ref self, f);
    219         public static Maybe<T> RFind<TSelf, T>(this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: class, IDoubleEndedIterator<T> => IDoubleEndedIterator<T>.RFind(ref self, f);
    220         public static Maybe<T> RFind<TSelf, T>(ref this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: struct, IDoubleEndedIterator<T> => IDoubleEndedIterator<T>.RFind(ref self, f);
    221         public static Maybe<ulong> RPosition<TSelf, T>(this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: class, IDoubleEndedIterator<T>, IExactSizeIterator<T> => IDoubleEndedIterator<T>.RPosition(ref self, f);
    222         public static Maybe<ulong> RPosition<TSelf, T>(ref this TSelf self, Fn<T, bool> f) where T: notnull where TSelf: struct, IDoubleEndedIterator<T>, IExactSizeIterator<T> => IDoubleEndedIterator<T>.RPosition(ref self, f);
    223         public static Repeat<T> Repeat<T>(T val) where T: notnull, IClone<T> => new(val);
    224         public static RepeatWith<T> RepeatWith<T>(Fn<T> f) where T: notnull => new(f);
    225         public static Rev<T, TSelf> Rev<TSelf, T>(this TSelf self) where T: notnull where TSelf: notnull, IDoubleEndedIterator<T> => new(self);
    226         public static Scan<T, TSelf, TState, T2> Scan<TSelf, T, TState, T2>(this TSelf self, TState st, FnRef<TState, T, Maybe<T2>> f) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull where TState: notnull => new(self, st, f);
    227         public static Skip<T, TSelf> Skip<TSelf, T>(this TSelf self, ulong n) where T: notnull where TSelf: notnull, IIterator<T> => new(self, n);
    228         public static SkipWhile<T, TSelf> SkipWhile<TSelf, T>(this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: notnull, IIterator<T> => new(self, f);
    229         public static Successors<T> Successors<T>(Maybe<T> first, FnIn<T, Maybe<T>> succ) where T: notnull => new(first, succ);
    230         public static Take<T, TSelf> Take<TSelf, T>(this TSelf self, ulong n) where T: notnull where TSelf: notnull, IIterator<T> => new(self, n);
    231         public static TakeWhile<T, TSelf> TakeWhile<TSelf, T>(this TSelf self, FnIn<T, bool> f) where T: notnull where TSelf: notnull, IIterator<T> => new(self, f);
    232         public static Result<Unit, TErr> TryForEach<TSelf, T, TErr>(this TSelf self, Fn<T, Result<Unit, TErr>> f) where T: notnull where TSelf: class, IIterator<T> where TErr: notnull => IIterator<T>.TryForEach(ref self, f);
    233         public static Result<Unit, TErr> TryForEach<TSelf, T, TErr>(ref this TSelf self, Fn<T, Result<Unit, TErr>> f) where T: notnull where TSelf: struct, IIterator<T> where TErr: notnull => IIterator<T>.TryForEach(ref self, f);
    234         public static Prod<TExt, TExt2> Unzip<TSelf, T, T2, TExt, TExt2, TDef, TDef2>(this TSelf self) where T: notnull where T2: notnull where TSelf: notnull, IIterator<Prod<T, T2>> where TExt: notnull, IExtend<T>, new() where TExt2: notnull, IExtend<T2>, new() {
    235 
    236             var left = new TExt();
    237             var right = new TExt2();
    238             _ = self.Fold(new Unit(), (_, val) => { _ = left.ExtendOne(val.Item0); return right.ExtendOne(val.Item1); });
    239             return new(left, right);
    240         }
    241         public static Zip<T, TSelf, T2, TIter> Zip<TSelf, T, TIter, T2>(this TSelf self, TIter other) where T: notnull where TSelf: notnull, IIterator<T> where T2: notnull where TIter: notnull, IIterator<T2> => new(self, other);
    242         #endregion
    243 
    244         #region Instance Functions
    245         #endregion
    246 
    247         #region Operators
    248         #endregion
    249 
    250         #region Types
    251         #endregion
    252     }
    253     #endregion
    254 
    255     #region Namespaces
    256     #endregion
    257 }
    258 #endregion