score:0

your solution is okay. i don't think that linq adds any simplification or clarity in this situation. here is a fast solution that i find intuitive:

static list<item[]> getcontiguoussequences(item[] items)
{
    var result = new list<item[]>();
    int start = 0;
    while (start < items.length) {
        int end = start + 1;
        while (end < items.length && items[end].start == items[end - 1].stop) {
            end++;
        }

        int len = end - start;
        var a = new item[len];
        array.copy(items, start, a, 0, len);
        result.add(a);
        start = end;
    }
    return result;
}

score:1

i'm really not sure this is much better than your original, but for the purpose of another solution the general process is

  1. use select to project a list working out a grouping
  2. use groupby to group by the above
  3. use select again to project the grouped items to an array of item
  4. use tolist to project the result to a list

public static list<item[]> getcontiguoussequences2(item []items)
{
    var curridx = 1;
    return items.select( (item,index) =>  new {
            item = item,
            index = index == 0 || items[index-1].stop == item.start ? curridx : ++curridx
        })
        .groupby(x => x.index, x => x.item)
        .select(x => x.toarray())
        .tolist();      
}

live example: https://dotnetfiddle.net/mbfhru


another way is to do an aggregation using aggregate. this means maintaining a final result list and a curr list where you can aggregate your sequences, adding them to the result list as you find discontinuities. this method looks a little closer to your original

public static list<item[]> getcontiguoussequences3(item []items)
{
    var res = items.aggregate(new {result = new list<item[]>(), curr = new list<item>()}, (agg, item) => {
            if(!agg.curr.any() || agg.curr.last().stop == item.start) {
                agg.curr.add(item);
            } else {
                agg.result.add(agg.curr.toarray());
                agg.curr.clear();   
                agg.curr.add(item);
            }
        return agg;
    });     
    res.result.add(res.curr.toarray()); // remember to add the last group
    return res.result;
}

live example: https://dotnetfiddle.net/hl0vyj

score:1

you can implement contiguoussplit as a corutine: let's loop over source and either add item into current range or return it and start a new one.

  private static ienumerable<item[]> contiguoussplit(ienumerable<item> source) {
    list<item> current = new list<item>();

    foreach (var item in source) {
      if (current.count > 0 && current[current.count - 1].stop != item.start) {
        yield return current.toarray();

        current.clear();
      }

      current.add(item);
    }  

    if (current.count > 0)
      yield return current.toarray();
  }

then if you want materialization

list<item[]> getcontiguoussequences(item []items) => contiguoussplit(items).tolist();

Related Query

More Query from same tag