I think you're doing a second loop that you don't need mathematically.
where x + y == length
So therefore
x = length - y
Since length is fixed and y is the variable. x seems to be calculated simply to satisfy the where, and can therefore be removed as an iterator and simply calculated.
Also the upper loop only went as far as length-1, so if totalWeight[thisBlock].Num
is bigger, we use that instead.
var lower = Enumerable.Range(1, Math.Min(totalWeight[thisBlock].Num, length-1));
var query = from y in lower
select Tuple.Create(length - y, y);
Doing this also collapses the LINQ query, which in turn allows us to remove the Tuple. Instead we can just subsitute (length - y) wherever we need the original x.
var lower = Enumerable.Range(1, Math.Min(totalWeight[thisBlock].Num, length-1));
foreach (var t in lower)
{
string upperBlock = string.Join(",", recentBlock.Skip(length - 1 - (length-y)));
string lowerBlock = string.Join(",", thisBlock.Split(',').Take(y));
yield return upperBlock + "," + lowerBlock;
}
Turn the Enumerable.Range/Foreach into a for loop.
Also length - 1 - (length-y)
becomes y - 1
for (int y = 1; y <= Math.Min(totalWeight[thisBlock].Num, length-1); y++)
{
string upperBlock = string.Join(",", recentBlock.Skip(y - 1));
string lowerBlock = string.Join(",", thisBlock.Split(',').Take(y));
yield return upperBlock + "," + lowerBlock;
}
Optimize out the splitting of thisBlock just so it's not done in a loop.
Also combine the string join into 1 function using concat.
So you end up with...
IEnumerable<string> MixedBlock(List<string> reelLayout, string thisBlock, int length)
{
List<string> recentBlock = RecentBlock(length - 1, reelLayout);
var thisSplitBlock = thisBlock.Split(',');
for (int y = 1; y <= Math.Min(totalWeight[thisBlock].Num, length-1); y++)
{
yield return string.Join(",",
recentBlock.Skip(y - 1)
.Concat(thisSplitBlock.Take(y));
}
}
Note: This will output items in the reverse order from the original. If the order is important simply reverse the for loop.
for (int y = Math.Min(totalWeight[thisBlock].Num, length-1); y >= 1; y--)
RecentBlock()
? What istotalWeight
? – Jeff Mercado Aug 26 '11 at 1:44