BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

LINQ
.NET 3.5+

A LINQ Style Range Generator

Language-Integrated Query (LINQ) provides the Enumerable.Range method that generates incrementing sequences of integers. This article describes a similar method that allows the creation of more complex ranges with the repeated application of a function.

Enumerable.Range

During the LINQ to Objects tutorial I described the Enumerable.Range method. This allows you to create ascending sequences of integer value by providing a starting value and the number if items you desire. Each value in the range is one greater than the previous. If you need a more complex range, you can combine the call to Range with the Select standard query operator, adding a lambda expression that performs a projection on each element.

For example, the following code produces eight of the powers of two:

foreach (int i in Enumerable.Range(0, 8).Select(x => Math.Pow(2, x)))
{
    Console.WriteLine(i);
}

/* OUTPUT

1
2
4
8
16
32
64
128

*/

The Range method is quite limited, as it only permits the generation of ascending ranges and can only create integer values. Even when adding projections, it is not easy to create a range where each value is based upon the previous or where the range contains complex types. In this article we'll create an alternative method that uses the style of LINQ and can be incorporated into your queries.

Creating the Class

The method could be added to a standard class or a static class and could be an instance or static method. For simplicity, we will create a static class in this article. The class is named, "RangeGenerator".

public static class RangeGenerator
{
}

Adding the Generate Method

The signature of the Generate method has an additional parameter to that of Enumerable.Range. The first parameter is still used to accept the initial value in the range and the final parameter determines the number of items to provide before the sequence is exhausted. The second parameter is new. This allows a Func delegate to be specified. After the first item in the sequence, each new element is generated by applying this delegate to the previous item.

The signature of the method is shown below. Note that this is a generic method. The initial value is of the generic type, "T". The function accepts and returns values of type T, these representing the previous and new elements of the sequence. The Generate method returns an IEnumerable<T>, allowing further LINQ operators to be applied and permitting the resultant sequence to be the source of a query created using LINQ's query expression syntax.

public static IEnumerable<T> Generate<T>(T initialValue, Func<T, T> func, int count)
{
}

The first step within the method is to validate that the function is valid. This mirrors the process used by the standard query operators; if the Func delegate is null, we throw an ArgumentNullException.

if (func == null) throw new ArgumentNullException("func");

We can now add the code for our iterator and call it from the Generate method, as shown below. We have to separate the implementation from the validation of the parameter as the use of yield return means that the code in the GenerateImpl method will not be executed until the sequence is accessed. If the null check is performed within the same method as the yield keyword, the exception will not be thrown immediately upon calling Range and may be encountered unexpectedly at the time the sequence is evaluated.

GenerateImpl starts by storing the initial value in the lastValue variable. On the first invocation of the iterator, this value is simply returned. The remainder of the invocations are handled by the for loop. Each iteration of the loop calculates a new value by applying the Func delegate to lastValue. The new value is stored in readiness for the next pass of the loop, before being returned using yield return.

public static IEnumerable<T> Generate<T>(T initialValue, Func<T, T> func, int count)
{
	if (func == null) throw new ArgumentNullException("func");

	return GenerateImpl(initialValue, func, count);
}

private static IEnumerable<T> GenerateImpl<T>(T initialValue, Func<T, T> func, int count)
{
	T lastValue = initialValue;

	yield return initialValue;
	for (int i = 1; i < count; i++)
	{
		lastValue = func(lastValue);
		yield return lastValue;
	}
}

Testing the Method

You can now try out the new method. Let's begin with a simple example, which could actually be achieved reasonably easily with Enumerable.Range. Here we create a sequence of five integers, starting at one. The lambda expression specifies subsequent elements are calculated by multiplying the previous value by ten.

foreach (int i in RangeGenerator.Generate(1, x => x * 10, 5))
{
    Console.WriteLine(i);
}

/* OUTPUT

1
10
100
1000
10000

*/

The next example is slightly more interesting. It estimates the amount of interest that could be paid on a bank account, where the interest rate is 5% and a fixed amount is added to the account annually. The initial value is the same as the annual deposit, in this case 1,000. The lambda expression first adds 5% to the previous balance, then adds the annual deposit. The final results are formatted according to your local currency settings so may vary slightly from the UK pounds version shown in the comments.

decimal annualDeposit = 1000;
foreach (decimal balance in RangeGenerator.Generate(
    annualDeposit, b => Math.Round((b * 1.05M) + annualDeposit, 2), 10))
{
    Console.WriteLine(balance.ToString("c").PadLeft(10));
}

/* OUTPUT

 £1,000.00
 £2,050.00
 £3,152.50
 £4,310.12
 £5,525.63
 £6,801.91
 £8,142.01
 £9,549.11
£11,026.57
£12,577.90

*/
1 April 2012