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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:50:22+00:00 2026-06-08T08:50:22+00:00

I have a function A similar to this which applies a function B to

  • 0

I have a function A similar to this which applies a function B to every file in a directory. Each file has a certain number of “entries”; the function B takes the current total of entries as a parameter and returns the number of new entries found in the current file.

Also, I need to count the number of files processed and display this count each time a file is processed. Due to my imperative background, I came up with 2 mutable variables and a for loop.

let files = Directory.EnumerateFiles sourceDirectory
let mutable numEntries = 0
let mutable numFiles = Seq.length files
let mutable index = 0
for file in files do
     printfn "done %d of %d" index numFiles
     let numNewEntries = processFile file numEntries
     numEntries <- numEntries + numNewEntries
     index <- index + 1

So, a few questions:

  • How can I write this in a more idiomatic, functional style?
  • Can you explain the advantages to a more idiomatic solution? I’m very
    new to functional programming and sometimes I don’t see what’s wrong
    with my dirty imperative for loops.
  • 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-08T08:50:26+00:00Added an answer on June 8, 2026 at 8:50 am

    Here is a more functional example:

    let files = Directory.EnumerateFiles sourceDirectory
    let numFiles = Seq.length files
    files 
    |> Seq.mapi (fun idx file -> (idx,file)) // Get access to the index in a loop
    |> Seq.fold (fun numentries (index,file) ->
             printfn "done %d of %d" index numFiles
             numentries + (processFile file numFiles)
             ) 0
    

    By using mapi I am able to get access to the index in the loop, eliminating the first mutable variable. The second is eliminated by using fold to keep track of the total number of files rather than a mutable variable.

    The main advantage of this is that without any mutable state it is possible to more easily convert the code to running in multiple threads. Also, as variables are constant, it becomes simpler to reason about the code.

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

Sidebar

Related Questions

I have a script which similar to this: foo.php class Foo { function Foo()
I have a similar code snippet like this class Search { public function search($for,
I have a little dropdown menu similar <select> , which contolled from external js-function.
Say I have this repeater which makes use of a public function called Test
Hi similar to the facebook and iphone app, i have an @ function which
I maintain a project that has a function definition similar to this: def f(a,
In zsh, I have a function called g which acts like this: with no
I have a function similar to below which is const and needs to check
I have the following code which does a function similar to the way the
This is a Windows Forms application. I have a function which captures some mouse

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.