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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:52:21+00:00 2026-05-23T19:52:21+00:00

Given a sequence of a group id/value tuples, it was easy to calculate group

  • 0

Given a sequence of a group id/value tuples, it was easy to calculate group totals (pretty much the same way I would do it with C# and LINQ):

let items = ["g1",5; "g2",10; "g1",20]

let groupsums = 
    items  
    |> Seq.groupBy (fun x -> fst x) 
    |> Seq.map (fun (g, s) -> Seq.fold (fun acc x -> acc + snd x) 0 s)

But being new to F#, I can’t see a way to so the same with lists. Do I have to use mutable variables, or is there a functional way to do the same with lists?

  • 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-23T19:52:22+00:00Added an answer on May 23, 2026 at 7:52 pm

    There is no built in List.groupBy. A number of F# built in types have functions that are assigned the seq version of said function. e.g. from list.fs

    let inline sumBy f (list : list<_>) = Seq.sumBy f list

    I’m pretty sure the designers of F# had many discussions about what to duplicate for the sake of consistency and what to omit for for sake of DRY. I personally wish they stuck with DRY.

    If you want to make your own “functional” List.groupBy I’d use map and list.

    let groupBy list =
        list 
        |> List.fold (fun group (g, x) -> 
            match group |> Map.tryFind g with
            | Some(s) -> group |> Map.remove g |> Map.add g (x::s)
            | None -> group |> Map.add g [x]
            ) Map.empty
        |> Map.toList 
    
    let groupsums = groupBy >> List.map (snd >> List.sum)
    

    You can skip keeping lists if you only need the sum.

    let groupAndSumBy list =
        list 
        |> List.fold (fun group (g, x) -> 
            match group |> Map.tryFind g with
            | Some(s) -> group |> Map.remove g |> Map.add g (x + s)
            | None -> group |> Map.add g x
            ) Map.empty
        |> Map.toList
        |> List.map snd
    

    Output

    > groupsums items;;
    val it : int list = [25; 10]
    
    > groupAndSumBy items;;
    val it : int list = [25; 10]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if anyone sees a pattern in the sequence given
Given an arbitrary sequence of points in space, how would you produce a smooth
Given an input sequence, what is the best way to find the longest (not
What would be the easiest way to generate nextval for some particular sequence with
Given a sequence of operations: a*b*a*b*a*a*b*a*b is there a way to get the optimal
Given a sequence of data (it may have duplicates), a fixed-sized moving window, move
Quite simply, given a sequence in F#, how does one obtain elements from index
Given the following sequence: normal %(b)BOLD% normal I want to transform this into the
Given a unsorted sequence of a[1,...,n] of integers, give an O(nlogn) runtime algorithm to
Given the following model, I want to index the fields (sequence,stock) class QuoteModel(models.Model): quotedate

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.