Std

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

OnceWith.cs (2991B)


      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 OnceWith<T>: IDoubleEndedIterator<T>, IExactSizeIterator<T>, IFusedIterator<T>, IInto<OnceWith<T>> where T: notnull {
     12 
     13         #region Type-level Constructors
     14         #endregion
     15 
     16         #region Instance Constructors
     17         public OnceWith() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     18         internal OnceWith(Maybe<Fn<T>> gen) => _gen = gen;
     19         #endregion
     20 
     21         #region Type-level Fields
     22         #endregion
     23 
     24         #region Instance Fields
     25         Maybe<Fn<T>> _gen;
     26         #endregion
     27 
     28         #region Type-level Properties
     29         #endregion
     30 
     31         #region Instance Properties
     32         #endregion
     33 
     34         #region Type-level Functions
     35         #endregion
     36 
     37         #region Instance Functions
     38         public Result<Unit, ulong> AdvanceBackBy(ulong n) => IDoubleEndedIterator<T>.AdvanceBackByDefault(ref this, n);
     39         public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<T>.AdvanceByDefault(ref this, n);
     40         public ulong Count() => IIterator<T>.CountDefault(ref this);
     41         public override readonly bool Equals(object? _) => false;
     42         public TInit Fold<TInit>(TInit init, Fn<TInit, T, TInit> f) where TInit: notnull => IIterator<T>.FoldDefault(ref this, init, f);
     43         public override readonly int GetHashCode() => 0;
     44         public readonly OnceWith<T> Into() => this;
     45         public Maybe<T> Last() => IIterator<T>.LastDefault(ref this);
     46         public readonly ulong Len() => _gen.IntoIter().Len();
     47         public Maybe<T> Next() {
     48 
     49             var val = _gen;
     50             _gen = Maybe<Fn<T>>.None();
     51             return val.IsSome ? new(val._some()) :  Maybe<T>.None();
     52         }
     53         public Maybe<T> NextBack() => Next();
     54         public TInit RFold<TInit>(TInit init, Fn<TInit, T, TInit> f) where TInit: notnull => IDoubleEndedIterator<T>.RFoldDefault(ref this, init, f);
     55         public readonly Prod<ulong, Maybe<ulong>> SizeHint() => _gen.IntoIter().SizeHint();
     56         public override readonly string ToString() => string.Empty;
     57         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull => IIterator<T>.TryFoldDefault(ref this, init, f);
     58         readonly Result<OnceWith<T>, Bottom> ITryInto<OnceWith<T>, Bottom>.TryInto() => new(this);
     59         public Result<TInit, TErr> TryRFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull => IDoubleEndedIterator<T>.TryRFoldDefault(ref this, init, f);
     60         #endregion
     61 
     62         #region Operators
     63         #endregion
     64 
     65         #region Types
     66         #endregion
     67     }
     68     #endregion
     69 
     70     #region Namespaces
     71     #endregion
     72 }
     73 #endregion