Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created January 12, 2021 14:24
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/7e699b8e1301f0fcec2277c65195e845 to your computer and use it in GitHub Desktop.
Save mjs3339/7e699b8e1301f0fcec2277c65195e845 to your computer and use it in GitHub Desktop.
Custom Linq Range Class
using System;
using System.Collections.Generic;
using System.Numerics;
public static class LinqHelper
{
/// <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> Range(char from, char to, char step = '\x0001')
{
var d = '\x0';
while((char) (from + (uint) d) < to)
{
yield return(char) (from + (uint) d);
d += step;
}
}
/// <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> Range(sbyte from, sbyte to, sbyte step = 1)
{
var d = 0;
while((sbyte) (from + d) < to)
{
yield return(sbyte) (from + d);
d += step;
}
}
/// <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> Range(byte from, byte to, byte step = 1)
{
var d = 0;
while((byte) (from + d) < to)
{
yield return(byte) (from + d);
d += step;
}
}
/// <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> Range(short from, short to, short step = 1)
{
var d = 0;
while((short) (from + d) < to)
{
yield return(short) (from + d);
d += step;
}
}
/// <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> Range(ushort from, ushort to, ushort step = 1)
{
var d = 0;
while((ushort) (from + d) < to)
{
yield return(ushort) (from + d);
d += step;
}
}
/// <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> Range(int from, int to, int step = 1)
{
var d = 0;
while(from + d < to)
{
yield return from + d;
d += step;
}
}
/// <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> Range(uint from, uint to, uint step = 1)
{
var d = 0u;
while(from + d < to)
{
yield return from + d;
d += step;
}
}
/// <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> Range(long from, long to, long step = 1)
{
var d = 0l;
while(from + d < to)
{
yield return from + d;
d += step;
}
}
/// <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> Range(ulong from, ulong to, ulong step = 1)
{
var d = 0ul;
while(from + d < to)
{
yield return from + d;
d += step;
}
}
/// <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> Range(BigInteger from, BigInteger to, BigInteger step)
{
var count = to - from;
for(var i = BigInteger.Zero; i < count; i = i + step)
yield return from + i;
}
/// <summary>
/// Generates a sequence of arbitrarily large floating point precision numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<BigRational> Range(BigRational from, BigRational to, BigRational step)
{
if(step == 0)
step = 1;
var count = to - from;
for(BigRational i = 0; i < count; i = +step)
yield return from + i;
}
/// <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> Range(float from, float to, float step = 1)
{
var d = 0f;
while(from + d < to)
{
yield return from + d;
d += step;
}
}
/// <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> Range(double from, double to, double step = 1)
{
var d = 0d;
while(from + d < to)
{
yield return from + d;
d += step;
}
}
/// <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> Range(decimal from, decimal to, decimal step = 1)
{
decimal d = 0;
while(from + d < to)
{
yield return from + d;
d += step;
}
}
/// <summary>
/// Generates a sequence of DateTime numbers within a specified range starting at
/// 'form' through 'to' with a given step value in days.
/// </summary>
public static IEnumerable<DateTime> Range(DateTime from, DateTime to, double step = 1)
{
var d = from;
while(d < to)
{
yield return d;
d = d.AddDays(step);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment