Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/conceptual/collections.list-module-[fsharp].md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ For an overview of lists in F#, see [Lists (F#)](Lists-%5BFSharp%5D.
|[contains](list.contains%5B't%5D-function-%5Bfsharp%5D.md)<br />**: 'T -&gt; 'T list -&gt; bool**|Evaluates to `true` if the given element is in the input list.|
|[countBy](list.countby%5B't,'key%5D-function-%5Bfsharp%5D.md)<br />**: ('T -&gt; 'Key) -&gt; 'T list -&gt; ('Key &#42; int) list**|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.|
|[distinct](list.distinct%5B't%5D-function-%5Bfsharp%5D.md)<br />**: 'T -&gt; 'T list**|Returns a list that contains no duplicate entries according to generic hash and equality comparisons on the entries. If an element occurs multiple times in the list then the later occurrences are discarded.|
|[distinctBy](https://msdn.microsoft.com/library/9293293b-9420-49c8-848f-401a9cd49b75)<br />**: ('T -&gt; 'Key) -&gt; 'T list -&gt; 'T list**|Returns a list that contains no duplicate entries according to the generic hash and equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the list then the later occurrences are discarded.|
|[empty](https://msdn.microsoft.com/library/bb4cb501-3429-47c1-8d5f-71180d3bded7)<br />**: 'T list**|Returns an empty list of the given type.|
|[exists](https://msdn.microsoft.com/library/15a3ebd5-98f0-44c0-8220-7dedec3e68a8)<br />**: ('T -&gt; bool) -&gt; 'T list -&gt; bool**|Tests if any element of the list satisfies the given predicate.|
|[exists2](https://msdn.microsoft.com/library/7532b39e-3f4f-4534-a60b-d7721dc6fa7e)<br />**: ('T1 -&gt; 'T2 -&gt; bool) -&gt; 'T1 list -&gt; 'T2 list -&gt; bool**|Tests if any pair of corresponding elements of the lists satisfies the given predicate.|
Expand Down
84 changes: 84 additions & 0 deletions docs/conceptual/list.distinctby['t,'key]-function-[fsharp].md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: List.distinctBy<'T,'Key> Function (F#)
description: List.distinctBy<'T,'Key> Function (F#)
keywords: visual f#, f#, functional programming
author: erikschierboom
manager: danielfe
ms.date: 03/13/2017
ms.topic: language-reference
ms.prod: visual-studio-dev14
ms.technology: devlang-fsharp
ms.assetid: 14ed9913-9b68-4204-985b-8baff1d4f6c2
---

# List.distinctBy<'T,'Key> Function (F#)

Returns a list that contains no duplicate entries according to the generic hash and equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the list then the later occurrences are discarded.

**Namespace/Module Path**: Microsoft.FSharp.Collections.List

**Assembly**: FSharp.Core (in FSharp.Core.dll)


## Syntax

```fsharp
// Signature:
List.distinctBy : ('T -> 'Key) -> 'T list -> 'T list (requires equality)

// Usage:
List.distinctBy projection list
```

#### Parameters
*projection*
Type: **'T -&gt; 'Key**


A function that transforms the list items into comparable keys.


*list*
Type: **'T**[list](https://msdn.microsoft.com/library/c627b668-477b-4409-91ed-06d7f1b3e4a7)


The input list.

## Exceptions
|Exception|Condition|
|----|----|
|[ArgumentNullException](https://msdn.microsoft.com/library/system.argumentnullexception.aspx)|Thrown when the input list is null|

## Return Value
The result list.

## Remarks
This function is named `DistinctBy` in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

## Example

The following example demonstrates the use of `List.distinctBy` to keep only the elements in a list that have a distinct absolute value. The first element with a given result is retained in the new list, so the positive numbers from 1 to 5 are dropped in the list from -5 to +10.

[!code-fsharp[Main](snippets/fslists/snippet71.fs)]

```
Original list:
[-5; -4; -3; -2; -1; 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
List with distinct absolute values:
[-5; -4; -3; -2; -1; 0; 6; 7; 8; 9; 10]
```

## Platforms
Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2


## Version Information
**F# Core Library Versions**

Supported in: 4.0, Portable


## See Also
[Collections.List Module &#40;F&#35;&#41;](Collections.List-Module-%5BFSharp%5D.md)

[Microsoft.FSharp.Collections Namespace &#40;F&#35;&#41;](Microsoft.FSharp.Collections-Namespace-%5BFSharp%5D.md)
7 changes: 7 additions & 0 deletions docs/conceptual/snippets/fslists/snippet71.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let inputList = [ -5 .. 10 ]
let printList list1 = List.iter (printf "%A ") list1; printfn ""
printfn "Original list: "
printList inputList
printfn "\nListwith distinct absolute values: "
let listDistinctAbsoluteValue = List.distinctBy (fun elem -> abs elem) inputList
listDistinctAbsoluteValue |> printList