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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:26:28+00:00 2026-05-26T03:26:28+00:00

I am pretty new to functional programming and therefore F# and I have serious

  • 0

I am pretty new to functional programming and therefore F# and I have serious trouble to come up with the right solution for this problem.

I have got a sequence of record types, say like:

type Invoice = {
    GrpText : string
    GrpRef : int
    Article : int
    Wkz : int
    Text : string
    Price : decimal
    InvoiceRef : int
}

Now I want to group or aggregate the sequence of Invoices by a given criteria and i.e. sum their prices. Invoices that does not match the criteria should not be grouped and just returned as they are.

A criteria function might look like this:

/// determines whether to group the two given Invoice items or not
let criteria item toCompareWith =
    (item.GrpRef > 0 && item.Article = toCompareWith.Article
        && item.InvoiceRef = toCompareWith.InvoiceRef) ||
    (item.Wkz <> 0 && item.Text = toCompareWith.Text)

The aggregation or grouping could look like this:

/// aggregate the given Invoice items
let combineInvoices item1 item2 =
    {item1 with Price = item1.Price + item2.Price; Wkz = 0}

The problem looks kind of simple but I am currently not experienced enough in functional programming to connect the dots.

Edit:

I just modified the criteria function in order to better show that it might be a bit more complex than grouping by one or multiple fields.

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

    Unless I’m missing something, there are two steps involved: group and reduce. The easiest way to group is Seq.groupBy. Since you want to use custom equality you need to either apply the [<CustomEquality>] attribute to your type and override Equals and GetHashCode, or roll your own key generating function that uses your concept of equality. Here’s an example of the latter.

    //custom key generator
    let genKeyWith compare =
      let lookup = ResizeArray()
      fun item ->
        match Seq.tryFindIndex (compare item) lookup with
        | Some idx -> idx
        | None ->
          lookup.Add(item)
          lookup.Count - 1
    

    Usage

    let getKey = genKeyWith criteria
    
    let invoices = Seq.init 10 (fun _ -> Unchecked.defaultof<Invoice>)
    
    invoices 
    |> Seq.groupBy getKey
    |> Seq.map (fun (_, items) -> Seq.reduce combineInvoices items)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to this type of programming and I'm having some trouble populating
I'm pretty new at C programming, and this type of thing keeps popping up.
I got the solution; however, I feel like the code is pretty awful. This
I'm new to functional programming and as I'm reading this book. It basically says
Being relatively new to functional programming, I expend lots of energy wondering is this
I have been programming C for a while (but still pretty new to C)
I'm pretty new to javascript and programming and have run into a brick wall
I'm pretty new to WPF however i've got a solid understanding of WindowsForms. When
I'm pretty new to programming for Linux environments, so I don't exactly know what
I am pretty new to the windows phone stuff and programming for windows phone.

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.