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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:04:42+00:00 2026-05-11T07:04:42+00:00

I’m writing a program as follows: Find all files with the correct extension in

  • 0

I’m writing a program as follows:

  • Find all files with the correct extension in a given directory
  • Foreach, find all occurrences of a given string in those files
  • Print each line

I’d like to write this in a functional way, as a series of generator functions (things that call yield return and only return one item at a time lazily-loaded), so my code would read like this:

IEnumerable<string> allFiles = GetAllFiles(); IEnumerable<string> matchingFiles = GetMatches( '*.txt', allFiles ); IEnumerable<string> contents = GetFileContents( matchingFiles ); IEnumerable<string> matchingLines = GetMatchingLines( contents );  foreach( var lineText in matchingLines )   Console.WriteLine( 'Found: ' + lineText ); 

This is all fine, but what I’d also like to do is print some statistics at the end. Something like this:

Found 233 matches in 150 matching files. Scanned 3,297 total files in 5.72s 

The problem is, writing the code in a ‘pure functional’ style like above, each item is lazily loaded.
You only know how many files match in total until the final foreach loop completes, and because only one item is ever yielded at a time, the code doesn’t have any place to keep track of how many things it’s found previously. If you invoke LINQ’s matchingLines.Count() method, it will re-enumerate the collection!

I can think of many ways to solve this problem, but all of them seem to be somewhat ugly. It strikes me as something that people are bound to have done before, and I’m sure there’ll be a nice design pattern which shows a best practice way of doing this.

Any ideas? Cheers

  • 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. 2026-05-11T07:04:42+00:00Added an answer on May 11, 2026 at 7:04 am

    In a similar vein to other answers, but taking a slightly more generic approach …

    … why not create a Decorator class that can wrap an existing IEnumerable implementation and calculate the statistic as it passes other items through.

    Here’s a Counter class I just threw together – but you could create variations for other kinds of aggregation too.

    public class Counter<T> : IEnumerable<T> {     public int Count { get; private set; }      public Counter(IEnumerable<T> source)     {         mSource = source;         Count = 0;     }      public IEnumerator<T> GetEnumerator()     {         foreach (var T in mSource)         {             Count++;             yield return T;         }     }      IEnumerator IEnumerable.GetEnumerator()     {         foreach (var T in mSource)         {             Count++;             yield return T;         }     }      private IEnumerable<T> mSource; } 

    You could create three instances of Counter:

    1. One to wrap GetAllFiles() counting the total number of files;
    2. One to wrap GetMatches() counting the number of matching files; and
    3. One to wrap GetMatchingLines() counting the number of matching lines.

    The key with this approach is that you’re not layering multiple responsibilities onto your existing classes/methods – the GetMatchingLines() method only handles the matching, you’re not asking it to track stats as well.

    Clarification in response to a comment by Mitcham:

    The final code would look something like this:

    var files = new Counter<string>( GetAllFiles()); var matchingFiles = new Counter<string>(GetMatches( '*.txt', files )); var contents = GetFileContents( matchingFiles ); var linesFound = new Counter<string>(GetMatchingLines( contents ));  foreach( var lineText in linesFound )     Console.WriteLine( 'Found: ' + lineText );  string message      = String.Format(          'Found {0} matches in {1} matching files. Scanned {2} files',         linesFound.Count,         matchingFiles.Count,         files.Count); Console.WriteLine(message); 

    Note that this is still a functional approach – the variables used are immutable (more like bindings than variables), and the overall function has no side-effects.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a text area in my form which accepts all possible characters from
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.