0

Don't know how to put it simpler, hence the strange title.

The problem is i need a finer control for executing methods per nth item, the upper bound is not known.

But here the nth is a double, it could be per 1 or 3rd item, but could be also 1.5 or 2.5, let me explain.

Users of my software can set per which item the processing will run, and thats the whole problem.

Resolution is to low, if they set 1, every item will be processed, if they set 2, it will be per 2nd. and so on, just a simple per item int option.

So 10 or 5 if there were 10 items, between them there is a gap, the gap will be even larger for 100 items, so i would like, a simple option that they can set also a double value.

So if they set for example 1.5 it would process 6 item from 10, for 2.5 - 4 items and so on, notice that the upper bound will be unknown.

How to do that?

2
  • You need to explain better how you plan to execute every 1.5 item.
    – D Stanley
    Nov 20, 2013 at 1:57
  • It is just a simplification, of course it will need some calculation to make it work, all you have is the step and processed item number. I dont know how to make the calculations. Nov 20, 2013 at 11:04

1 Answer 1

1

Assuming that the underlying collection has an indexer, you could just increment and round to the nearest to choose the item to process:

for(double i = 0; (int)Math.Round(i) < items.Count; i += step)
{
   process(items[(int)Math.Round(i)];
}

If you don't have an indexer - you just ned to keep track of the "current" index manually:

public static IEnumerable<T> GetEveryNthItem<T>(this IEnumerable<T> items, double step)
{
    int cur = 0;
    double next = 0;
    foreach(var item in items)
    {
        if((int)Math.Round(next,MidpointRounding.AwayFromZero) == cur)
        {
            yield return item;
            next += step;
        }
        cur++;
    }
}

you can use the extension method like this:

for each(var item in items.GetEveryNthItem(2.5)
{
    process(item);
}
4
  • This wont work, it will not have an indexer, the upper bound is not known. Nov 20, 2013 at 11:06
  • The updated version doesn't work, have you tested it? In linqpad List<string> items = new List<string>(); items.AddRange(new string[]{ "1","2","3","4","5","6","7","8","9","10" }); foreach(var item in items.GetEveryNthItem(1.5)) { item.Dump(); } Return only 1 Nov 20, 2013 at 19:56
  • Works now, but what if items are also an indexer? Is this even possible? Now it just returns one value for linqpad IEnumerable<string> GetNext() { int x = 1; while (x<=10) { yield return x.ToString(); x++; } } and foreach(var item in GetNext().GetEveryNthItem(1.5)) Nov 20, 2013 at 20:25
  • Should still work fine. I get 7 items when I run your code in LinqPad.
    – D Stanley
    Nov 20, 2013 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.