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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:45:19+00:00 2026-05-15T03:45:19+00:00

In the folder function of the Array.Fold operation, i want to look back arbitrary

  • 0

In the folder function of the Array.Fold operation, i want to look back arbitrary number of items.

The only way i can figure out is like this.

let aFolder (dataArray, curIdx, result) (dataItem) =
      let N = numItemsToLookBack(result, dataItem) 
      let recentNitems =  dataArray.[curIdx - N .. curIdx - 1]
      let result = aCalc(result, dataItem, recentNitems )

      dataArray, curIdx + 1, result

myDataArray |> Array.fold aFolder (myDataArray, 0, initResult)

As you can see, I passed the whole dataArray and index to the folder function to get the “recentNitems”.
But this way allows the folder function to access not only preceding data, but also the following data.

Is there a way to prevent this looking forward thing?
Or, is there a better (more functional or more efficient) way regardless of this looking forward problem?

  • 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-15T03:45:19+00:00Added an answer on May 15, 2026 at 3:45 am

    If you need to look back at arbitrary number of elements, then using Array.fold directly maybe isn’t the best option as the function is designed for scenarios where you can process elements one by one.

    There is nothing wrong with using direct access to arrays in F# – as long as you don’t mutate them, you’re still writing functional code (so maybe recursion + direct access using indices would work in your scenario).

    As Yin Zhu points out, you could generate array of portions of array (using Seq.windowed), but creating a O(n) number of small arrays may be quite an overhead. Here is a more sophisticated trick you could also use:

    I see you need to call the aCalc function with only the relevant part of the array as an argument. You could create a simple wrapper for the array that provides access only to the relevant part of the array like this:

    type ArraySlice<'a>(arr:'a[], from, max) = 
      member x.Item
        with get(index) =
          let index = index + from
          if index > max || index < from then failwith "out of range"
          arr.[index]
    

    As far as I understand your code, you need to generate the number of lookback items based on the result, so you’ll probably need to write this using single fold, which could be done roughly like this:

    // If you write this using lambdas or local 'let' binding, the 
    // 'dataArray' value will be in scope, so you don't need to keep it as
    // part of the state...
    (dataArray, (0, initResult)) ||> Array.fold (fun (i, result) item -> 
       let n = numItemsToLookBack (result, item)  
       let recent =  new ArraySlice<_>(dataArray, max 0 (i - n), i - 1)
       // Note: modify aCalc, so that it takes 'ArraySlice'
       let result = aCalc(result, item, recent) 
       i + 1, result)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use a function written within a .cpp saved in a folder
I want to display the subfolder names in a folder contained . How can
I want to use a function to recursively scan a folder, and assign the
I understand that within same folder, I can use include() function for external PHP
I have a function which returns an array of files in a folder recursive.
I have a function that locks all files in a certain folder: function lockFolder_files($folder='',$task=''){
This function is in a Class file in App_Code folder Public Shared Function CRUD(ByVal
var json_string=$.parseJSON(document.getElementById(json_db).innerHTML); $(function () { $(#folder_tree).jstree({ contextmenu: {items: customMenu}, json_data : {data:json_string}, plugins :
I use the NSHomeDirectory() function to get the app's home folder, and write to
abspath() function abspath() { echo $_SERVER['DOCUMENT_ROOT']; } directory() function directory() { echo '/folder/'; }

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.