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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:57:29+00:00 2026-05-15T21:57:29+00:00

I’m working on a problem which I know I can solve with C#. I

  • 0

I’m working on a problem which I know I can solve with C#. I would like to prove to my boss that F# would be able solve in a more succinct way. However my understanding of functional programming is still fairly immature.

The Problem:

I’m working with a list of ‘Trade’ classes. The definition of the class is as follows:

type Trade(brokerId : string, productId : string, marketId : string, buySideId : string, tradeDate : string, ruleId : int) = class

    member this.BrokerId = brokerId
    member this.ProductId = productId
    member this.MarketId = marketId
    member this.BuySideId = buySideId
    member this.TradeDate = tradeDate
end

I need to be able to group the trades and then apply a rule to each of the resulting groups of data.

However I cannot guarantee the grouping of the data i.e. the rule for determining the grouping will potentially change every time the program is run – so for instance I may have to group by:

  • TradeDate, BrokerId
  • TradeDate only
  • TradeDate, BrokerId, AccountId

… and so on.

Once I have the distinct groups it would be easy (I think) to apply a rule (such as ‘is the total TradeAmount greater than 10,000’).

Any help / pointers with creating a functional orientated solution to this problem would be very welcome.

Many thanks.

  • 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-15T21:57:30+00:00Added an answer on May 15, 2026 at 9:57 pm

    If I understand the problem correctly, then you essentially want to call the Seq.groupBy function. The problem is that you don’t quite know the lambda function that you want to pass it as an argument when writing the code, because the function may vary depending on the choice of keys that should be used for grouping. Here is one relatively simple way to do this…

    We’ll create a dictionary of functions that gives us a function for reading the specified property of the Trade (this could be, in principle, constructed automatically, but it is probably easier to just write it):

    let keyfunctions : IDictionary<string, Trade -> obj> = 
      dict [ "TradeDate", (fun t -> box t.TradeDate);  
             "BrokerId", (fun t -> box t.BrokerId);
             "MarketId", (fun t -> box t.MarketId); ]
    

    Now, if we wanted to use multiple keys, we need a way to combine two functions that give us parts of the key into a single function. We can write a combinator that takes two functions and returns a single one that produces boxed tuple as the key:

    let combine f1 f2 = (fun t -> box (f1 t, f2 t))
    

    If you have a list of strings that specifies your keys, then you just need to pick function from the dictionary for each of the keys and combine them into a single function using combine:

    let grouping = [ "TradeDate"; "MarketId" ]
    let func = grouping |> Seq.map (fun n -> keyfunctions.[n]) |> Seq.reduce combine
    

    And now you have a function that can be used as an argument to Seq.groupBy:

    trades |> Seq.groupBy func
    

    There are probably other ways to do this in F#, but I think this is a relatively simple approach that could convince your boss :-). As a side-note, you could write essentially the same thing in C# 3.0, although it would look a bit uglier due to more heavy syntax…

    EDIT 1: A nice thing about this approach is that you don’t need to use any reflection. Everything runs as compiled code, so it should be pretty efficient. The composed function just calls several other functions (.NET methods) and boxes the returned values…

    EDIT 2: Regarding the order – this approach will work (when comparing tuples, the first elements are compared first), but I’m not entirely sure in which order the items are aggregated when using Seq.reduce, so maybe this example works the other way round…

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

Sidebar

Related Questions

Hi there Im working on a project which needs to solve the TSP problem.
Here is the Problem Description : Suppose that we wish to know which stories
i've been working on a problem for a while now, which involves targeting the
Real problem with an existing webapp which has been working for the last couple
My problem/issue We are working on an opensource project which we have hosted on
I have a problem in my coding which was working fine with an older
I am having a problem I am working on a class which is subclass
i am working on a recipe site (which is almost finished except this problem)
I'm currently working a problem that requires my web application to generate a chart
I'm working on a project which is using Selenium and I'd like to try

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.