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++)
doubles.Add(range.Minimum + i * precision);
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++)
doubles.Add(range.Minimum + i * precision);
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