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

  • Home
  • SEARCH
  • 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 1045183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:56:45+00:00 2026-05-16T15:56:45+00:00

Given a type (Candidate) which has muiltiple fields one may score (here one concrete

  • 0

Given a type (Candidate) which has muiltiple fields one may score (here one concrete example with _scoreXXX) and calculate the total percentage score:

type ScorableCandidate() =
    let mutable _scoreXXX: int  =  0 ;
    let mutable _percentXXX: float = 0. ;

    member this.scoreXXX
        with get() = _scoreXXX
        and  set(v) =
            _scoreXXX<- v
            propertyChanged.Trigger(this, new PropertyChangedEventArgs("scoreXXX"))

    member this.percentXXX
        with get() = _percentXXX
        and  set(v) =
            _percentXXX <- v
            propertyChanged.Trigger(this, new PropertyChangedEventArgs("percentXXX"))

I would like to absctract the calculation of the percentages so I don’t need to redefine the function everytime. Unfortunately I am lost on how to do this with member functions. The template of the code I am abstracting looks is:

let scoreXXXs () =
     let totalXXXScore  = List.fold (fun acc (candidate: ScorableCandidate) -> acc + candidate.scoreXXXs) 0 brokers

     let score (candidate: ScorableCandidate) =
                 candidate.percentXXXs <- (float candidate.scoreXXXs) / float totalXXXScore   * 100.

     if totalXXXScore  > 0 then
                List.iter score <| brokers

I guess I would like to be able to define the function as “score” and pass in the aapropriate member accessors. Such that i could write

    score XXX  // where xxx is some property in the class that needs to be scored
    score YYY  // where yyy is some property in the class that needs to be scored

One thought I had was passing in functions to get access, and that is easy enough to do for the getter but I can’t seem to figure out the setter. I realize I could defer to reflection for this – but I feel that might not be the best (f#) way… AT this point I am at:

    let scoreField (accessF : (ScorableCandidate -> int)) (setF : (float -> unit)) =
        let totalScore = List.fold (fun acc (candidate: ScorableCandidate) -> acc + accessF candidate) 0 brokers

        let score (candidate: ScorableCandidate) =
            setF <| (accessF candidate |> float) / float totalScore  * 100.

        if totalScore > 0 then
            List.iter score <| brokers

    scoreField (fun c -> c.scoreXXX) (fun f -> ())

But I don’t know how (or if it is possible) to represent the setter as a lambda function on the type (where maybe I can pass the instace as paraneter to lambda function and invoke it somehow).

Thoughts? Thanks ahead.

Update Found this approach (thoughts):
http://laurent.le-brun.eu/site/index.php/2009/10/17/52-dynamic-lookup-operator-aka-duck-typing-in-fsharp

  • 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-16T15:56:45+00:00Added an answer on May 16, 2026 at 3:56 pm

    You’re on the right track, but your settor is missing the object you’re going to set the field on. So the type of setF should be:

    setF : (ScorableCandidate -> float -> unit)
    

    so then you’d use it like:

    let score (candidate: ScorableCandidate) =
        (setF candidate) <| (accessF candidate |> float) / float totalScore  * 100.
    

    and call your scoreField thus:

    scoreField (fun c -> c.scoreXXX) (fun c f -> c.percentXXX <- f)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looping on the fields and properties on a given Type and I'd like
Given a data structure of: type Candidate = SalesRep of SalesRep | Analyst of
For any given type i want to know its default value. In C#, there
1) If we want to create an instance of a given type during runtime
I want to retrieve all objects (not DOM elements) of a given type created
I need to test, if an instance is exactly of a given type. But
My friend is trying to create a utility function that is given some Type
I have read the documentation that states that "given the type of the enum,
Given curves of type Circle and Circular-Arc in 3D space, what is a good
Given an object of type clsPerson: dim oP as New clsPerson oP.FirstName = Sam

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.