# Expand Range into List

S

#### Shapper

Hello,

I created a class that defines a Range of a type T:

Range<Int32> integers = new Range<Int32>(1, 8);
Range<Double> doubles = new Range<Double>(2.0, 3.2);
Range<DateTime> dates = new Range<DateTime>(DateTime.Now, DateTime.Now.AddDays(6));

I need to create an Expand extension that creates a IEnumerable from these Ranges:

var integersList = integers.Expand();
var doublesList = doubles.Expand(0.1);
var datesList = dates.Expand(DateTimeInterval.Day);

For doubles a precision must be supplied. And for dates an Time Interval.

So I ended up with the following:

public static class RandomExtensions {

public static IEnumerable<Int32> Expand(this Range<Int32> range) {
return Enumerable.Range(range.Minimum, 1 + range.Maximum - range.Minimum);

} // Expand

public static IEnumerable<Double> Expand(this Range<Double> range, Double precision) {

List<Double> doubles = new List<Double> { range.Minimum };
for (Int32 i = 1; i < (range.Maximum - range.Minimum) / precision; i++)
return doubles;

} // Expand

public static IEnumerable<DateTime> Expand(this Range<DateTime> range, DateTimeInterval interval) {

return Enumerable.Range(0, 1 + range.Maximum.Subtract(range.Minimum).Days).Select(offset => range.Minimum.Date.AddDays(offset));

} // Expand

} // RandomExtensions

public enum DateTimeInterval {
Day,
Hour,
Minute,
Month,
Second,
Year
} // DateTimeInterval

Expand for integers is working fine ...

For Doubles is working to ... But I think the code could be improved.

For DateTime I am not sure how to integrate the TimeInterval.

Could someone help me out with this?

BTW, my Range class is as follows:

public class Range<T> where T : IComparable<T> {

private T _minimum;
private T _maximum;

public T Minimum { get { return _minimum; } set { value = _minimum; } } // Minimum

public T Maximum { get { return _maximum; } set { value = _maximum; } } // Maximum

public Range(T minimum, T maximum) {
_minimum = minimum;
_maximum = maximum;
} // Range

public Boolean Contains(T value) {
return (Minimum.CompareTo(value) <= 0) && (value.CompareTo(Maximum) <= 0);
} // Contains

public Boolean IsValid() {
return Minimum.CompareTo(Maximum) <= 0;
} // IsValid

public override String ToString() {
return String.Format("[{0} - {1}]", Minimum, Maximum);
} // ToString

} // Range

Thank You,

Miguel

I created a class that defines a Range of a type T:

Range<Int32> integers = new Range<Int32>(1, 8);
Range<Double> doubles = new Range<Double>(2.0, 3.2);
Range<DateTime> dates = new Range<DateTime>(DateTime.Now, DateTime.Now.AddDays(6));

I need to create an Expand extension that creates a IEnumerable from these Ranges:

var integersList = integers.Expand();
var doublesList = doubles.Expand(0.1);
var datesList = dates.Expand(DateTimeInterval.Day);

For doubles a precision must be supplied. And for dates an Time Interval.

So I ended up with the following:

public static class RandomExtensions {

public static IEnumerable<Int32> Expand(this Range<Int32> range) {
return Enumerable.Range(range.Minimum, 1 + range.Maximum - range.Minimum);

} // Expand

public static IEnumerable<Double> Expand(this Range<Double> range, Double precision) {

List<Double> doubles = new List<Double> { range.Minimum };
for (Int32 i = 1; i < (range.Maximum - range.Minimum) / precision; i++)
return doubles;

} // Expand

public static IEnumerable<DateTime> Expand(this Range<DateTime> range, DateTimeInterval interval) {

return Enumerable.Range(0, 1 + range.Maximum.Subtract(range.Minimum).Days).Select(offset => range.Minimum.Date.AddDays(offset));

} // Expand

} // RandomExtensions

public enum DateTimeInterval {
Day,
Hour,
Minute,
Month,
Second,
Year
} // DateTimeInterval

Expand for integers is working fine ...

For Doubles is working to ... But I think the code could be improved.

For DateTime I am not sure how to integrate the TimeInterval.

I am not sure that the whole approach is that useful.

But if you insist then:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace E
{
public class Range<T>
{
public T Minimum { get; set; }
public T Maximum { get; set; }
}
public enum DateTimeInterval {
Day,
Hour,
Minute,
Second
}
public static class RandomExtensions
{
public static IEnumerable<Int32> Expand(this Range<Int32> range)
{
return Enumerable.Range(range.Minimum, range.Maximum -
range.Minimum + 1);
}
public static IEnumerable<Double> Expand(this Range<Double>
range, Double precision) {
return Enumerable.Range(0, (int)((range.Maximum -
range.Minimum) / precision + 1)).Select(v => range.Minimum + v * precision);
}
private static Dictionary<DateTimeInterval, Func<TimeSpan,
int>> getValue = new Dictionary<DateTimeInterval, Func<TimeSpan, int>> {
{ DateTimeInterval.Day, (ts) => (int)ts.TotalDays },

{
DateTimeInterval.Hour, (ts) => (int)ts.TotalHours },

{
DateTimeInterval.Minute, (ts) => (int)ts.TotalMinutes },

{
DateTimeInterval.Second, (ts) => (int)ts.TotalSeconds } };
private static Dictionary<DateTimeInterval, Func<DateTime, int,
DateTime>> addValue = new Dictionary<DateTimeInterval, Func<DateTime,
int, DateTime>> { { DateTimeInterval.Day, (dt,v) => dt.AddDays(v)},

{ DateTimeInterval.Second, (dt,v) => dt.AddSeconds(v)} };
public static IEnumerable<DateTime> Expand(this Range<DateTime>
range, DateTimeInterval interval) {
return Enumerable.Range(0,
getValue[interval].Invoke(range.Maximum - range.Minimum) + 1).Select(v
}
}
public class Program
{
public static void Main(string[] args)
{
foreach(int iv in (new Range<int> { Minimum=1, Maximum=3
}).Expand())
{
Console.WriteLine(iv);
}
foreach(double xv in (new Range<double> { Minimum=1.0,
Maximum=2.0 }).Expand(0.25))
{
Console.WriteLine(xv);
}
foreach(double xv in (new Range<double> { Minimum=1.0,
Maximum=2.0 }).Expand(0.125))
{
Console.WriteLine(xv);
}
foreach(DateTime dt in (new Range<DateTime> {
}).Expand(DateTimeInterval.Day))
{
Console.WriteLine(dt);
}
foreach(DateTime dt in (new Range<DateTime> {
}).Expand(DateTimeInterval.Hour))
{
Console.WriteLine(dt);
}
}
}
}

Arne

Hello,

I created a class that defines a Range of a type T:

Range<Int32> integers = new Range<Int32>(1, 8);

Range<Double> doubles = new Range<Double>(2.0, 3.2);

Range<DateTime> dates = new Range<DateTime>(DateTime.Now, DateTime.Now.AddDays(6));

I need to create an Expand extension that creates a IEnumerable from these Ranges:

var integersList = integers.Expand();

var doublesList = doubles.Expand(0.1);

var datesList = dates.Expand(DateTimeInterval.Day);

For doubles a precision must be supplied. And for dates an Time Interval.

So I ended up with the following:

public static class RandomExtensions {

public static IEnumerable<Int32> Expand(this Range<Int32> range) {

return Enumerable.Range(range.Minimum, 1 + range.Maximum - range.Minimum);

} // Expand

public static IEnumerable<Double> Expand(this Range<Double> range, Double precision) {

List<Double> doubles = new List<Double> { range.Minimum };

for (Int32 i = 1; i < (range.Maximum - range.Minimum) / precision; i++)

return doubles;

} // Expand

public static IEnumerable<DateTime> Expand(this Range<DateTime> range, DateTimeInterval interval) {

return Enumerable.Range(0, 1 + range.Maximum.Subtract(range.Minimum).Days).Select(offset => range.Minimum.Date.AddDays(offset));

} // Expand

} // RandomExtensions

public enum DateTimeInterval {

Day,

Hour,

Minute,

Month,

Second,

Year

} // DateTimeInterval

Expand for integers is working fine ...

For Doubles is working to ... But I think the code could be improved.

For DateTime I am not sure how to integrate the TimeInterval.

Could someone help me out with this?

BTW, my Range class is as follows:

public class Range<T> where T : IComparable<T> {

private T _minimum;

private T _maximum;

public T Minimum { get { return _minimum; } set { value = _minimum; } } // Minimum

public T Maximum { get { return _maximum; } set { value = _maximum; } } // Maximum

public Range(T minimum, T maximum) {

_minimum = minimum;

_maximum = maximum;

} // Range

public Boolean Contains(T value) {

return (Minimum.CompareTo(value) <= 0) && (value.CompareTo(Maximum) <= 0);

} // Contains

public Boolean IsValid() {

return Minimum.CompareTo(Maximum) <= 0;

} // IsValid

public override String ToString() {

return String.Format("[{0} - {1}]", Minimum, Maximum);

} // ToString

} // Range

Thank You,

Miguel

That was what I was looking for.

Thank You once more for the help,
Miguel