Std

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

SkipWhile.cs (3286B)


      1 using Std.Convert;
      2 using Std.Maybe;
      3 using Std.Ops;
      4 using Std.Result;
      5 using System;
      6 using System.Runtime.InteropServices;
      7 #region Namespaces
      8 namespace Std.Iter {
      9     #region Types
     10     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 0)]
     11     public struct SkipWhile<T, TIter>: IInto<SkipWhile<T, TIter>>, IIterator<T> where T: notnull where TIter: notnull, IIterator<T> {
     12 
     13         #region Type-level Constructors
     14         #endregion
     15 
     16         #region Instance Constructors
     17         public SkipWhile() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     18         internal SkipWhile(TIter iter, FnIn<T, bool> f) => (_iter, _f, _flag) = (iter, f, false);
     19         #endregion
     20 
     21         #region Type-level Fields
     22         #endregion
     23 
     24         #region Instance Fields
     25         readonly FnIn<T, bool> _f;
     26         TIter _iter;
     27         bool _flag;
     28         #endregion
     29 
     30         #region Type-level Properties
     31         #endregion
     32 
     33         #region Instance Properties
     34         #endregion
     35 
     36         #region Type-level Functions
     37         #endregion
     38 
     39         #region Instance Functions
     40         public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<T>.AdvanceByDefault(ref this, n);
     41         public ulong Count() => IIterator<T>.CountDefault(ref this);
     42         public override readonly bool Equals(object? _) => false;
     43         public TInit Fold<TInit>(TInit init, Fn<TInit, T, TInit> f) where TInit: notnull {
     44 
     45             if (!_flag) {
     46                 var val = Next();
     47 
     48                 if (val.IsSome) {
     49                     init = f(init, val._some);
     50                 } else {
     51                     return init;
     52                 }
     53             }
     54             return _iter.Fold(init, f);
     55         }
     56         public override readonly int GetHashCode() => 0;
     57         public readonly SkipWhile<T, TIter> Into() => this;
     58         public Maybe<T> Last() => IIterator<T>.LastDefault(ref this);
     59         public Maybe<T> Next() {
     60 
     61             var flag = _flag;
     62             var pred = _f;
     63             var val = IIterator<T>.Find(ref _iter, (in T x) => { if (flag || !pred(in x)) { flag = true; return true; } else { return false; } });
     64             _flag = flag;
     65             return val;
     66         }
     67         public Prod<ulong, Maybe<ulong>> SizeHint() => new(ulong.MinValue, _iter.SizeHint().Item1);
     68         public override readonly string ToString() => string.Empty;
     69         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull {
     70 
     71             if (!_flag) {
     72                 var val = Next();
     73 
     74                 if (val.IsSome) {
     75                     var res = f(init, val._some);
     76 
     77                     if (res.IsErr) {
     78                         return res;
     79                     } else {
     80                         init = res._ok;
     81                     }
     82                 } else {
     83                     return new(init);
     84                 }
     85             }
     86             return _iter.TryFold(init, f);
     87         }
     88         readonly Result<SkipWhile<T, TIter>, Bottom> ITryInto<SkipWhile<T, TIter>, Bottom>.TryInto() => new(this);
     89         #endregion
     90 
     91         #region Operators
     92         #endregion
     93 
     94         #region Types
     95         #endregion
     96     }
     97     #endregion
     98 
     99     #region Namespaces
    100     #endregion
    101 }
    102 #endregion