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

  • Home
  • SEARCH
  • 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 6162821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:43:10+00:00 2026-05-23T21:43:10+00:00

Is there an inverse/complement of IEnumerable.SelectMany ? That is, is there a method of

  • 0

Is there an inverse/complement of IEnumerable.SelectMany? That is, is there a method of the form IEnumerable<T>.InverseSelectMany(Func<IEnumerable<T>,T>) which will find a sequence in the input sequence and perform a transform to a single element and then flatten the whole thing out again?

For example, if you wanted to replace all escape sequences { 0x7E, 0x7E } in an HDLC frame with just a single byte 0x7E, you could do something like

byte[] byteArray = new byte[] { 0x01, 0x02, 0x7E, 0x7E, 0x04 }; // etc.
byte[] escapeSequence = new byte[] { 0x7E, 0x7E };
byte[] outputBytes = byteArray.InverseSelectMany<byte,byte>(x =>
{
   if (x.SequenceEqual(escapeSequence))
   {
      return new List<byte> { 0x7E };
   {
   else
   {
      return x;
   }
});

Does that make any sense or am I missing something critical here?

  • 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-23T21:43:11+00:00Added an answer on May 23, 2026 at 9:43 pm

    There isn’t anything built-in like that. The first problem is that by passing an arbitrary Func<IEnumerable<T>,T> to the enumerator, it won’t know how many bytes it will need to “take” and pass to the function. A more reasonable approach is shown below, where you can pass a sequence to be replaced, and the other sequence to replace, and do a simple search for that.

    public static class Extensions
    {
        public static IEnumerable<T> ReplaceSequence<T>(this IEnumerable<T> original, IEnumerable<T> toSearch, IEnumerable<T> toReplace) where T : IEquatable<T>
        {
            T[] toSearchItems = toSearch.ToArray();
            List<T> window = new List<T>();
            foreach (T value in original)
            {
                window.Add(value);
                if (window.Count == toSearchItems.Length)
                {
                    bool match = true;
                    for (int i = 0; i < toSearchItems.Length; i++)
                    {
                        if (!toSearchItems[i].Equals(window[i]))
                        {
                            match = false;
                            break;
                        }
                    }
    
                    if (match)
                    {
                        foreach (T toReplaceValue in toReplace)
                        {
                            yield return toReplaceValue;
                        }
    
                        window.Clear();
                    }
                    else
                    {
                        yield return window[0];
                        window.RemoveAt(0);
                    }
                }
            }
    
            foreach (T value in window)
            {
                yield return value;
            }
        }
    }
    // http://stackoverflow.com/q/6751533/751090
    public class StackOverflow_6751533
    {
        public static void Test()
        {
            byte[] byteArray = new byte[] { 0x01, 0x02, 0x7E, 0x7E, 0x04 };
            byte[] escapeSequence = new byte[] { 0x7E, 0x7E };
            byte[] unescapedSequence = new byte[] { 0x7E };
            byte[] outputBytes = byteArray.ReplaceSequence(escapeSequence, unescapedSequence).ToArray();
            for (int i = 0; i < outputBytes.Length; i++)
            {
                Console.Write("{0:X2} ", (int)outputBytes[i]);
            }
            Console.WriteLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bizzaro-Diff!!! Is there a away to do a bizzaro/inverse-diff that only displays the portions
Is there a function in MATLAB that performs the inverse of an absolute value
Are there any Java/Android libraries that have implemented the inverse fourier transform? I've found
Is there a super quick way to bind to the inverse of a boolean
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are numerous Agile software development methods. Which ones have you used in practice
There is a field in my company's Contacts table. In that table, there is
Is there some way to calculate the inverse factorials of real numbers? For example
I need to use beta distribution and inverse beta distribution in my project. There
Suppose we have some arbitrary positive number x . Is there a method to

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.