Std

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

FilterMap.cs (2834B)


      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 FilterMap<T, TIter, T2>: IInto<FilterMap<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 FilterMap() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     18         internal FilterMap(TIter iter, Fn<T, Maybe<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, Maybe<T2>> _f;
     26         TIter _iter;
     27         #endregion
     28 
     29         #region Type-level Properties
     30         #endregion
     31 
     32         #region Instance Properties
     33         #endregion
     34 
     35         #region Type-level Functions
     36         #endregion
     37 
     38         #region Instance Functions
     39         public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<T2>.AdvanceByDefault(ref this, n);
     40         public ulong Count() => IIterator<T2>.CountDefault(ref this);
     41         public override readonly bool Equals(object? _) => false;
     42         public TInit Fold<TInit>(TInit init, Fn<TInit, T2, TInit> f) where TInit: notnull {
     43 
     44             static Fn<TInit, T, TInit> fn(Fn<T, Maybe<T2>> g, Fn<TInit, T2, TInit> h) => (acc, item) => { var val = g(item); return val.IsSome ? h(acc, val._some) : acc; };
     45             return _iter.Fold(init, fn(_f, f));
     46         }
     47         public override readonly int GetHashCode() => 0;
     48         public readonly FilterMap<T, TIter, T2> Into() => this;
     49         public Maybe<T2> Last() => IIterator<T2>.LastDefault(ref this);
     50         public Maybe<T2> Next() => IIterator<T>.FindMap(ref _iter, _f);
     51         public Prod<ulong, Maybe<ulong>> SizeHint() => new(ulong.MinValue, _iter.SizeHint().Item1);
     52         public override readonly string ToString() => string.Empty;
     53         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T2, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull {
     54             static Fn<TInit, T, Result<TInit, TErr>> fn(Fn<T, Maybe<T2>> g, Fn<TInit, T2, Result<TInit, TErr>> h) => (acc, item) => { var val = g(item); return val.IsSome ? h(acc, val._some) : new(acc); };
     55             return _iter.TryFold(init, fn(_f, f));
     56         }
     57         readonly Result<FilterMap<T, TIter, T2>, Bottom> ITryInto<FilterMap<T, TIter, T2>, Bottom>.TryInto() => new(this);
     58         #endregion
     59 
     60         #region Operators
     61         #endregion
     62 
     63         #region Types
     64         #endregion
     65     }
     66     #endregion
     67 
     68     #region Namespaces
     69     #endregion
     70 }
     71 #endregion