260

F#: Reduce a list of tuples by grouping one of the elements into arrays

Question:

I have a list of tuples which i want to group by one of its elements as a key. For example, if i had this list of tuples:

<blockquote>

[(A, "hello"), (A, "stack"), (A,"over"), (A, "flow"), (B, "how"), (B, "you"), (C, "doin")]

</blockquote>

I would like to get a result in the form:

<blockquote>

[(A, ["hello", "stack", "over", "flow"]), (B, ["how", "you"]), (C, ["doin"])]

</blockquote>

I am new to F# so I am all out of ideas on how to do this. I thank you in advance.

cheers

Answer1:

I think you are using incorrect delimiter for list elements - instead of , you need to use ;.

To get results use this snippet:

[("A", "hello"); ("A", "stack"); ("A","over"); ("A", "flow"); ("B", "how"); ("B", "you"); ("C", "doin")] |> Seq.groupBy fst |> Seq.map (fun (key,groupping) -> key, (groupping |> Seq.map snd |> Seq.toList)) |> Seq.toList

Recommend

  • how to check all available variable that have been passed form controller to twig template
  • How to retrieve values from JSON object in javascript [duplicate]
  • Change variable scope of function in Javascript
  • PyQt5: Using QtDesigner, how do you connect a signal with a slot/callable defined in a module
  • What's the difference between [String] vs. [(String)]?
  • Objective C difference between self.variable and variable assignments
  • How to test labels in QTP
  • Vertical alignment of float:left div's
  • Grouping by blank nodes
  • how to read a file in prolog?
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Runtime.exec() gives Error: Could not find or load main class
  • Is it possible to define rest argument in OCaml?
  • quiver not drawing arrows just lots of blue, matlab
  • Python pickle not one-to-one: different pickles give same object
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Use of this Javascript
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Read text file and split every line in MSBuild
  • Get object from AWS S3 as a stream
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • jQuery tmpl and DataLink beta
  • R: gsub and capture
  • php design question - will a Helper help here?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • SQL merge duplicate rows and join values that are different
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Benchmarking RAM performance - UWP and C#
  • How to set the response of a form post action to a iframe source?
  • embed rChart in Markdown
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Add sale price programmatically to product variations
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • How do I use LINQ to get all the Items that have a particular SubItem?