Std

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

Map.cs (2788B)


      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 Map<T, TIter, T2>: IInto<Map<T, TIter, T2>>, IIterator<T2> where T: notnull where TIter: notnull, IIterator<T> where T2: notnull {
     12 
     13         #region Type-level Constructors
     14         #endregion
     15 
     16         #region Instance Constructors
     17         public Map() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     18         internal Map(TIter iter, Fn<T, T2> f) => (_iter, _f) = (iter, f);
     19         #endregion
     20 
     21         #region Type-level Fields
     22         #endregion
     23 
     24         #region Instance Fields
     25         readonly Fn<T, T2> _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<T2>.AdvanceByDefault(ref this, n);
     41         public ulong Count() => IIterator<T2>.CountDefault(ref this);
     42         public override readonly bool Equals(object? _) => false;
     43         public TInit Fold<TInit>(TInit init, Fn<TInit, T2, TInit> f) where TInit: notnull {
     44 
     45             static Fn<TInit, T, TInit> fn(Fn<T, T2> g, Fn<TInit, T2, TInit> h) => (acc, item) => h(acc, g(item));
     46             return _iter.Fold(init, fn(_f, f));
     47         }
     48         public override readonly int GetHashCode() => 0;
     49         public readonly Map<T, TIter, T2> Into() => this;
     50         public Maybe<T2> Last() => IIterator<T2>.LastDefault(ref this);
     51         public Maybe<T2> Next() => _iter.Next().Map(_f);
     52         public Prod<ulong, Maybe<ulong>> SizeHint() => _iter.SizeHint();
     53         public override readonly string ToString() => string.Empty;
     54         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T2, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull {
     55 
     56             static Fn<TInit, T, Result<TInit, TErr>> fn(Fn<T, T2> g, Fn<TInit, T2, Result<TInit, TErr>> h) => (acc, item) => h(acc, g(item));
     57             return _iter.TryFold(init, fn(_f, f));
     58         }
     59         readonly Result<Map<T, TIter, T2>, Bottom> ITryInto<Map<T, TIter, T2>, Bottom>.TryInto() => new(this);
     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