Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created December 15, 2018 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/c6fcc687167dab28e61e6277712926ac to your computer and use it in GitHub Desktop.
Save mjs3339/c6fcc687167dab28e61e6277712926ac to your computer and use it in GitHub Desktop.
C# IEnumerableStep - Generate A Sequence of Numbers within a Specific Range with a given Step
public static class IEnumerableStep
{
/// <summary>
/// Generates a sequence of 8-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<sbyte> Step(this sbyte from, sbyte to, int step)
{
return Step(from, to, step).Select(i => i);
}
/// <summary>
/// Generates a sequence of 8-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<byte> Step(this byte from, byte to, int step)
{
return Step(from, to, step).Select(i => i);
}
/// <summary>
/// Generates a sequence of character values within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<char> Step(this char from, char to, int step)
{
return Step(from, to, step).Select(i => i);
}
/// <summary>
/// Generates a sequence of 16-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<short> Step(this short from, short to, int step)
{
return Step(from, to, step).Select(i => i);
}
/// <summary>
/// Generates a sequence of 16-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<ushort> Step(this ushort from, ushort to, int step)
{
return Step(from, to, step).Select(i => i);
}
/// <summary>
/// Generates a sequence of 32-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<int> Step(this int from, int to, int step)
{
if (step <= 0)
step = step == 0 ? 1 : -step;
if (from <= to)
for (var i = from; i <= to; i += step)
yield return i;
else
for (var i = from; i >= to; i -= step)
yield return i;
}
/// <summary>
/// Generates a sequence of 32-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<uint> Step(this uint from, uint to, uint step)
{
if (step == 0U)
step = 1U;
if (from <= to)
for (var ui = from; ui <= to; ui += step)
yield return ui;
else
for (var ui = from; ui >= to; ui -= step)
yield return ui;
}
/// <summary>
/// Generates a sequence of 64-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<long> Step(this long from, long to, long step)
{
if (step <= 0L)
step = step == 0L ? 1L : -step;
if (from <= to)
for (var l = from; l <= to; l += step)
yield return l;
else
for (var l = from; l >= to; l -= step)
yield return l;
}
/// <summary>
/// Generates a sequence of 64-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<ulong> Step(this ulong from, ulong to, ulong step)
{
if (step == 0UL)
step = 1UL;
if (from <= to)
for (var ul = from; ul <= to; ul += step)
yield return ul;
else
for (var ul = from; ul >= to; ul -= step)
yield return ul;
}
/// <summary>
/// Generates a sequence of arbitrarily large signed numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<BigInteger> Step(this BigInteger from, BigInteger to, BigInteger step)
{
if (step == 0)
step = 1;
if (from <= to)
for (var ul = from; ul <= to; ul += step)
yield return ul;
else
for (var ul = from; ul >= to; ul -= step)
yield return ul;
}
/// <summary>
/// Generates a sequence of arbitrarily large floating point numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<BigRational> Step(this BigRational from, BigRational to, BigRational step)
{
if (step == 0)
step = 1;
if (from <= to)
for (var ul = from; ul <= to; ul += step)
yield return ul;
else
for (var ul = from; ul >= to; ul -= step)
yield return ul;
}
/// <summary>
/// Generates a sequence of single precision floating-point numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<float> Step(this float from, float to, float step)
{
if (step <= 0.0f)
step = step == 0.0f ? 1.0f : -step;
if (from <= to)
for (var f = from; f <= to; f += step)
yield return f;
else
for (var f = from; f >= to; f -= step)
yield return f;
}
/// <summary>
/// Generates a sequence of double precision floating-point numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<double> Step(this double from, double to, double step)
{
//if (step <= 0.0)
// step = step == 0.0 ? 1.0 : -step;
//if (from <= to)
for (var d = 0.0; d < to; d += step)
yield return from + d;
//else
// for (var d = 0.0; d > to; d -= step)
// yield return from + d;
}
/// <summary>
/// Generates a sequence of decimal numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<decimal> Step(this decimal from, decimal to, decimal step)
{
if (step <= 0.0m)
step = step == 0.0m ? 1.0m : -step;
if (from <= to)
for (var m = from; m <= to; m += step)
yield return m;
else
for (var m = from; m >= to; m -= step)
yield return m;
}
/// <summary>
/// Generates a sequence of DateTime numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<DateTime> Step(this DateTime from, DateTime to, double step)
{
if (step <= 0.0)
step = step == 0.0 ? 1.0 : -step;
if (from <= to)
for (var dt = from; dt <= to; dt = dt.AddDays(step))
yield return dt;
else
for (var dt = from; dt >= to; dt = dt.AddDays(-step))
yield return dt;
}
/// <summary>
/// Generates a sequence of 8-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<sbyte> Step(this sbyte from, sbyte to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of 8-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<byte> Step(this byte from, byte to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of character value within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<char> Step(this char from, char to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of 16-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<short> Step(this short from, short to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of 16-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<ushort> Step(this ushort from, ushort to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of 32-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<int> Step(this int from, int to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of 32-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<uint> Step(this uint from, uint to)
{
return Step(from, to, 1U);
}
/// <summary>
/// Generates a sequence of 64-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<long> Step(this long from, long to)
{
return Step(from, to, 1L);
}
/// <summary>
/// Generates a sequence of 64-bit unsigned numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<ulong> Step(this ulong from, ulong to)
{
return Step(from, to, 1UL);
}
/// <summary>
/// Generates a sequence of arbitrarily large signed numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<BigInteger> Step(this BigInteger from, BigInteger to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of arbitrarily large floating point numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<BigRational> Step(this BigRational from, BigRational to)
{
return Step(from, to, 1);
}
/// <summary>
/// Generates a sequence of single precision floating-point numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<float> Step(this float from, float to)
{
return Step(from, to, 1.0f);
}
/// <summary>
/// Generates a sequence of double precision floating-point numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<double> Step(this double from, double to)
{
return Step(from, to, 1.0);
}
/// <summary>
/// Generates a sequence of decimal numbers within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<decimal> Step(this decimal from, decimal to)
{
return Step(from, to, 1.0m);
}
/// <summary>
/// Generates a sequence of DateTime values within a specified range starting at
/// 'form' through 'to' with a single step value.
/// </summary>
public static IEnumerable<DateTime> Step(this DateTime from, DateTime to)
{
return Step(from, to, 1.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment