| | | 1 | | namespace Towel; |
| | | 2 | | |
| | | 3 | | /// <summary>Status of iteration.</summary> |
| | | 4 | | public enum StepStatus |
| | | 5 | | { |
| | | 6 | | /// <summary>Stepper was not broken.</summary> |
| | | 7 | | Continue = 0, |
| | | 8 | | /// <summary>Stepper was broken.</summary> |
| | | 9 | | Break = 1, |
| | | 10 | | } |
| | | 11 | | |
| | | 12 | | /// <summary>Polymorphism base for all data structures in the Towel framework.</summary> |
| | | 13 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | | 14 | | public interface ISteppable<T> |
| | | 15 | | { |
| | | 16 | | #region Methods |
| | | 17 | | |
| | | 18 | | /// <summary>Traverses values and invokes a function on every <typeparamref name="T"/> value.</summary> |
| | | 19 | | /// <typeparam name="TStep">The type of function to invoke on every <typeparamref name="T"/> value.</typeparam> |
| | | 20 | | /// <param name="step">The function to invoke on every <typeparamref name="T"/> value.</param> |
| | | 21 | | /// <returns>The status of the traversal.</returns> |
| | | 22 | | StepStatus StepperBreak<TStep>(TStep step = default) |
| | | 23 | | where TStep : struct, IFunc<T, StepStatus>; |
| | | 24 | | |
| | | 25 | | #endregion |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary>Static members for <see cref="ISteppable{T}"/>.</summary> |
| | | 29 | | public static class Steppable |
| | | 30 | | { |
| | | 31 | | #region Extension Methods |
| | | 32 | | |
| | | 33 | | /// <summary>Traverses values and invokes a function on every <typeparamref name="T"/> value.</summary> |
| | | 34 | | /// <typeparam name="T">The type of value to traverse.</typeparam> |
| | | 35 | | /// <param name="steppable">The values to step though.</param> |
| | | 36 | | /// <param name="step">The function to invoke on every <typeparamref name="T"/> value.</param> |
| | | 37 | | public static void Stepper<T>(this ISteppable<T> steppable, Action<T> step) |
| | 4996 | 38 | | { |
| | 4996 | 39 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| | 4996 | 40 | | steppable.Stepper<T, SAction<T>>(step); |
| | 4996 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary>Traverses values and invokes a function on every <typeparamref name="T"/> value.</summary> |
| | | 44 | | /// <typeparam name="T">The type of value to traverse.</typeparam> |
| | | 45 | | /// <typeparam name="TStep">The type of function to invoke on every <typeparamref name="T"/> value.</typeparam> |
| | | 46 | | /// <param name="steppable">The values to step though.</param> |
| | | 47 | | /// <param name="step">The function to invoke on every <typeparamref name="T"/> value.</param> |
| | | 48 | | public static void Stepper<T, TStep>(this ISteppable<T> steppable, TStep step = default) |
| | | 49 | | where TStep : struct, IAction<T> => |
| | 5008 | 50 | | steppable.StepperBreak<StepBreakFromAction<T, TStep>>(step); |
| | | 51 | | |
| | | 52 | | /// <summary>Traverses values and invokes a function on every <typeparamref name="T"/> value.</summary> |
| | | 53 | | /// <typeparam name="T">The type of value to traverse.</typeparam> |
| | | 54 | | /// <param name="steppable">The values to step though.</param> |
| | | 55 | | /// <param name="step">>The function to invoke on every <typeparamref name="T"/> value.</param> |
| | | 56 | | /// <returns>The status of the traversal.</returns> |
| | | 57 | | public static StepStatus StepperBreak<T>(this ISteppable<T> steppable, Func<T, StepStatus> step) |
| | 5 | 58 | | { |
| | 5 | 59 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| | 5 | 60 | | return steppable.StepperBreak<SFunc<T, StepStatus>>(step); |
| | 5 | 61 | | } |
| | | 62 | | |
| | | 63 | | #endregion |
| | | 64 | | } |