| title | List.countBy<'T,'Key> Function (F#) |
|---|---|
| description | List.countBy<'T,'Key> Function (F#) |
| keywords | visual f#, f#, functional programming |
| author | liboz |
| manager | danielfe |
| ms.date | 05/16/2016 |
| ms.topic | language-reference |
| ms.prod | visual-studio-dev14 |
| ms.technology | devlang-fsharp |
| ms.assetid | a0b6e7e0-c30a-4e5a-a9dd-f7f9c4761870 |
Applies a key-generating function to each element of a list and returns a list yielding unique keys and their number of occurrences in the original list.
Namespace/Module Path: Microsoft.FSharp.Collections.List
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
List.countBy : ('T -> 'Key) -> 'T list -> ('Key * int) list ('Key requires equality)
// Usage:
List.countBy projection sourceprojection Type: 'T -> 'Key
A function transforming each item of input list into a key to be compared against the others.
source Type: 'Tlist
The input list.
| Exception | Condition |
|---|---|
| ArgumentNullException | Thrown when the input list is null |
A list of unique keys and their number of occurrences in the original list.
Note that this function returns a list that traverses the whole initial list. As a result this function should not be used with large lists. The function makes no assumption on the ordering of the original list.
This function is named CountBy in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.
The following example demonstrates the use of List.countBy to determine the number of elements in a list that are odd or even.
[!code-fsharpMain]
Output
(1, 50) (0, 50)
Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2
F# Core Library Versions
Supported in: 4.0, Portable