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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:08:36+00:00 2026-05-17T00:08:36+00:00

Given the following code: static member private getIntValue (_map:Map<string, int>) (_key:string) = if (_map.ContainsKey

  • 0

Given the following code:

static member private getIntValue (_map:Map<string, int>) (_key:string) =
    if (_map.ContainsKey _key) then
        _map.[_key], _map
    else
        let i = doSomething ()
        i, if i > 0 then _map.Add (_key, i) else _map

static member private getDataFn<'T> (_getFn:Map<string, 'T> -> string -> 'T * Map<string, 'T>) =
    let dataMap = ref Map.empty
    fun _key ->
        let value, updatedMap = _getFn !dataMap _key
        dataMap := updatedMap
        value

static member getIndexNumber = getDataFn<int> getIntValue

… the value of the dataMap reference cell in the first line of the function definition (i.e. fun _key -> …) of getDataFn<‘T> is always empty (i.e. dataMap.Count = 0), no matter how many times I call getIndexNumber.

Obviously, I’m expecting dataMap to be updated whenever a non-existent key is passed to getIndexNumber, but this isn’t happening. An empty Map is being passed to getIntValue every time.

Why is this happening?

(P.S. I know that I could just use a Dictionary, that’s beside the point.)

  • 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-17T00:08:37+00:00Added an answer on May 17, 2026 at 12:08 am

    Here’s some code to unblock you:

    let doSomething() = 2
    type Foo() =
        static let indexer = Foo.getDataFn<int> Foo.getIntValue 
        static member private getIntValue (_map:Map<string, int>) (_key:string) = 
            if (_map.ContainsKey _key) then 
                _map.[_key], _map 
            else 
                let i = doSomething () 
                i, if i > 0 then _map.Add (_key, i) else _map 
    
        static member private getDataFn<'T> (_getFn:Map<string, 'T> -> string -> 'T * Map<string, 'T>) : (string -> 'T)= 
            let dataMap = ref Map.empty 
            fun _key -> 
                printfn "count = %d" (!dataMap).Count 
                let value, updatedMap = _getFn !dataMap _key 
                dataMap := updatedMap 
                value 
    
        static member getIndexNumber = indexer
    //    static member getIndexNumber = Foo.getDataFn<int> Foo.getIntValue 
    
    let r = Foo.getIndexNumber("foo")
    printfn "%d" r
    let r2 = Foo.getIndexNumber("foo")
    printfn "%d" r2
    

    The key is that getIndexNumber is a property, which is re-evaluated each time it’s called, which ends up calling getDataFn each time, which allocates a fresh ref each time. I moved the call to getDataFn into a static let so it only gets called once.

    This code all looks very un-idiomatic, though. Might I suggest code more like this?

    let doSomething() = 2
    
    type StringIndexer() =
        let mutable map : Map<string,int> = Map.empty 
        let get(s) =
            printfn "count = %d" map.Count 
            match map.TryFind s with
            | None -> 
                let i = doSomething()
                map <- map.Add(s,i)
                i
            | Some(i) -> i
        member this.GetIndexNumber(s) = get(s)
    
    let si = new StringIndexer()
    let r = si.GetIndexNumber("foo")
    printfn "%d" r
    let r2 = si.GetIndexNumber("foo")
    printfn "%d" r2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following RX code: static void Main(string[] args) { int x = 0;
Given the following code... class Program { static void Main(string[] args) { Foo foo
Given the following code sample: public class WeirdStuff { public static int doSomething() {
Given the following code: class C { static void m(String s) { ... }
Given the following code, I try to implement the public static Point operator +(int
Given the following code public static Bitmap PrintWindow(IntPtr hWnd) { int width = GetWindowInfo(hWnd).rcWindow.Width;
given the following code: public static class Helpers { private static Char[] myChars =
Given the following Java code: public class Test { static private class MyThread extends
Given the following code: final class retVal { int photo_id; } Gson gson =
Given the following code. EventLoopScheduler scheduler = new EventLoopScheduler(ts => new Thread(ts)); BehaviorSubject<int> subject

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.