Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 308927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:41:43+00:00 2026-05-12T07:41:43+00:00

As a follow up to the method that collapses overlapping ranges I thought I

  • 0

As a follow up to the method that collapses overlapping ranges I thought I would try to create a method that combines adjacent ranges.

Basically, after running the Collapse method you may end up with for example 1 to 5 and 6 to 10. I would like to combine those into one range, 1 to 10.

This is what I have come up with so far, but it doesn’t really work very well. Does anyone spot my problem or have good alternative solution?

    public static IEnumerable<Range<T>> MergeAdjacent<T>(this IEnumerable<Range<T>> source, Func<T, T, bool> isAdjacent)
    {
        using (var sourceIterator = source.GetEnumerator())
        {
            if (!sourceIterator.MoveNext())
                yield break;

            var first = sourceIterator.Current;

            while (sourceIterator.MoveNext())
            {
                var second = sourceIterator.Current;

                if (isAdjacent(first.End, second.Start))
                {
                    yield return Range.Create(first.Start, second.End);
                }
                else
                    yield return first;

                first = second;
            }

            yield return first;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:41:43+00:00Added an answer on May 12, 2026 at 7:41 am

    I’ve reached this solution. One prerequisite is that ranges are ordered ascending/descending depending on the Func. It will merge adjacent ranges and it’s still deferred execution. I didn’t perform a lot of tests so there might be edge cases that break this. Be gentile 🙂

    Edit: Shortened down the code a bit. As far as I can see it works. Left out null checks though.

        public static IEnumerable<Range<T>> MergeAdjacent<T>(this IEnumerable<Range<T>> source, Func<T, T, bool> isAdjacent)
        {
            using (var it = source.GetEnumerator())
            {
                if (!it.MoveNext())
                    yield break;
    
                var item = it.Current;
    
                while (it.MoveNext())
                    if (isAdjacent(item.End, it.Current.Start))
                    {
                        item = Range.Create(item.Start, it.Current.End);
                    }
                    else
                    {
                        yield return item;
                        item = it.Current;
                    }
    
                yield return item;
            }
        }
    

        static void Main(string[] args)
        {
            var ranges = new List<Range<int>>
            {
                Range.Create(1,3), Range.Create(4,5), Range.Create(7,10), 
                Range.Create(11,17), Range.Create(20,32), Range.Create(33,80), 
                Range.Create(90,100), 
            };
    
            foreach (var range in ranges.MergeAdjacent((r1, r2) => r1 + 1 == r2))
                Console.WriteLine(range);
        }
    
        // Result: 1-5, 7-20, 25-80, 90-100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is a follow-up from How to indicate that a method was unsuccessful
I would like to write a method that deletes my application's Core Data store,
So I am trying to create a request that will automatically follow the redirects
I have the follow method that returns a dynamic object representing an IEnumerable<'a> ('a=anonymous
I have a need for methods in several classes that must always follow a
This question is a follow up to: Why can’t I call a method outside
In follow up to this question , it appears that some numbers cannot be
I am really trying to follow the DRY principle. I have a sub that
I have this method that receives an ID number and downloads an HTML website
Maybe there is a method that does this that I don't know about -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.