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 4034094
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:55:46+00:00 2026-05-20T11:55:46+00:00

I have a sequence of elements. The sequence can only be iterated once and

  • 0

I have a sequence of elements. The sequence can only be iterated once and can be "infinite".

What is the best way get the head and the tail of such a sequence?

Update: A few clarifications that would have been nice if I included in the original question 🙂

  • Head is the first element of the sequence and tail is "the rest". That means the the tail is also "infinite".

  • When I say infinite, I mean "very large" and "I wouldn’t want to store it all in memory at once". It could also have been actually infinite, like sensor data for example (but it wasn’t in my case).

  • When I say that it can only be iterated once, I mean that generating the sequence is resource heavy, so I woundn’t want to do it again. It could also have been volatile data, again like sensor data, that won’t be the same on next read (but it wasn’t in my case).

  • 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-20T11:55:46+00:00Added an answer on May 20, 2026 at 11:55 am

    Decomposing IEnumerable<T> into head & tail isn’t particularly good for recursive processing (unlike functional lists) because when you use the tail operation recursively, you’ll create a number of indirections. However, you can write something like this:

    I’m ignoring things like argument checking and exception handling, but it shows the idea…

    Tuple<T, IEnumerable<T>> HeadAndTail<T>(IEnumerable<T> source) {
      // Get first element of the 'source' (assuming it is there)
      var en = source.GetEnumerator();
      en.MoveNext();
      // Return first element and Enumerable that iterates over the rest
      return Tuple.Create(en.Current, EnumerateTail(en));
    }
    
    // Turn remaining (unconsumed) elements of enumerator into enumerable
    IEnumerable<T> EnumerateTail<T>(IEnumerator en) {
      while(en.MoveNext()) yield return en.Current; 
    }
    

    The HeadAndTail method gets the first element and returns it as the first element of a tuple. The second element of a tuple is IEnumerable<T> that’s generated from the remaining elements (by iterating over the rest of the enumerator that we already created).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you have a sequence of block elements and you wanted to place margin
If I have sequence of sequences (maybe a list of tuples) I can use
I have a long sequence of hex digits in a string, such as 000000000000484240FA063DE5D0B744ADBED63A81FAEA390000C8428640A43D5005BD44
I have a sequence of migrations in a rails app which includes the following
I have a sequence of SQL calls, which I want to use to detect
Let's say I have a sequence of values (e.g., 3, 5, 8, 12, 15)
I have one hibernate sequence, that generates all sequence-numbers in my app. When I
I have a table that records a sequence of actions with a field that
I have a long string (a DNA sequence). It does not contain any whitespace
I have a number of tables that use the trigger/sequence column to simulate auto_increment

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.