Std

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

Inspect.cs (2963B)


      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 Inspect<T, TIter>: IInto<Inspect<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 Inspect() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     18         internal Inspect(TIter iter, FnIn<T, Unit> f) => (_iter, _f) = (iter, f);
     19         #endregion
     20 
     21         #region Type-level Fields
     22         #endregion
     23 
     24         #region Instance Fields
     25         readonly FnIn<T, Unit> _f;
     26         [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Can't be readonly since we call mutable methods on it.")]
     27         TIter _iter;
     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             static Fn<TInit, T, TInit> fn(FnIn<T, Unit> g, Fn<TInit, T, TInit> h) => (acc, item) => { _ = g(in item); return h(acc, item); };
     45             return _iter.Fold(init, fn(_f, f));
     46         }
     47         public override readonly int GetHashCode() => 0;
     48         public readonly Inspect<T, TIter> Into() => this;
     49         public Maybe<T> Last() => IIterator<T>.LastDefault(ref this);
     50         public Maybe<T> Next() {
     51 
     52             var mbe = _iter.Next();
     53 
     54             if (mbe.IsSome) {
     55                 _ = _f(in mbe._some);
     56             }
     57             return mbe;
     58         }
     59         public Prod<ulong, Maybe<ulong>> SizeHint() => _iter.SizeHint();
     60         public override readonly string ToString() => string.Empty;
     61         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull {
     62             static Fn<TInit, T, Result<TInit, TErr>> fn(FnIn<T, Unit> g, Fn<TInit, T, Result<TInit, TErr>> h) => (acc, item) => { _ = g(in item); return h(acc, item); };
     63             return _iter.TryFold(init, fn(_f, f));
     64         }
     65         readonly Result<Inspect<T, TIter>, Bottom> ITryInto<Inspect<T, TIter>, Bottom>.TryInto() => new(this);
     66         #endregion
     67 
     68         #region Operators
     69         #endregion
     70 
     71         #region Types
     72         #endregion
     73     }
     74     #endregion
     75 
     76     #region Namespaces
     77     #endregion
     78 }
     79 #endregion