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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:19:08+00:00 2026-06-14T10:19:08+00:00

As we all know, Enumerable.SelectMany flattens a sequence of sequences into a single sequence.

  • 0

As we all know, Enumerable.SelectMany flattens a sequence of sequences into a single sequence. What if we wanted a method that could flatten sequences of sequences of sequences, and so on recursively?

I came up quickly with an implementation using an ICollection<T>, i.e. eagerly evaluated, but I’m still scratching my head as to how to make a lazily-evaluated one, say, using the yield keyword.

static List<T> Flatten<T>(IEnumerable list)  {
    var rv = new List<T>();
    InnerFlatten(list, rv);
    return rv;
}

static void InnerFlatten<T>(IEnumerable list, ICollection<T> acc) {
    foreach (var elem in list) {
        var collection = elem as IEnumerable;
        if (collection != null) {
            InnerFlatten(collection, acc);
        }
        else {
            acc.Add((T)elem);
        }
    }
}

Any ideas? Examples in any .NET language welcome.

  • 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-06-14T10:19:09+00:00Added an answer on June 14, 2026 at 10:19 am

    This is trivial in F# with recursive sequence expressions.

    let rec flatten (items: IEnumerable) =
      seq {
        for x in items do
          match x with
          | :? 'T as v -> yield v
          | :? IEnumerable as e -> yield! flatten e
          | _ -> failwithf "Expected IEnumerable or %A" typeof<'T>
      }
    

    A test:

    // forces 'T list to obj list
    let (!) (l: obj list) = l
    let y = ![["1";"2"];"3";[!["4";["5"];["6"]];["7"]];"8"]
    let z : string list = flatten y |> Seq.toList
    // val z : string list = ["1"; "2"; "3"; "4"; "5"; "6"; "7"; "8"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We all know that openId has become such a popular method for user authentication,
We all know of sleep method available in java threads.. I understand that the
I know of Python's list method that can consume all elements from a generator.
We all know that CSS sprite images are great to reduce the amount of
We all know the effects that lots of thrown exceptions can have over the
We all know that a hash table has O(1) time for both inserts and
We all know it - this is the reading that lists the changes brought
We all know mutable structs are evil in general. I'm also pretty sure that
All numbers that divide evenly into x. I put in 4 it returns: 4,
I know that I can use the following code to enumerate all SQL Server

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.