Udostępnij za pośrednictwem


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

Returns a sequence 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 sequence then the later occurrences are discarded.

Namespace/Module Path: Microsoft.FSharp.Collections.Seq

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

// Signature:
Seq.distinctBy : ('T -> 'Key) -> seq<'T> -> seq<'T> (requires equality)

// Usage:
Seq.distinctBy projection source

Parameters

  • projection
    Type: 'T -> 'Key

    A function that transforms the sequence items into comparable keys.

  • source
    Type: seq<'T>

    The input sequence.

Exceptions

Exception

Condition

ArgumentNullException

Thrown when the input sequence is null.

Return Value

The result sequence.

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 Seq.distinctBy to keep only the elements in a sequence that have a distinct absolute value. The first element with a given result is retained in the new sequence, so the positive numbers from 1 to 5 are dropped in the sequence from -5 to +10.

let inputSequence = { -5 .. 10 }
let printSeq seq1 = Seq.iter (printf "%A ") seq1; printfn ""
printfn "Original sequence: "
printSeq inputSequence
printfn "\nSequence with distinct absolute values: "
let seqDistinctAbsoluteValue = Seq.distinctBy (fun elem -> abs elem) inputSequence
seqDistinctAbsoluteValue |> printSeq
Original sequence: 
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 

Sequence with distinct absolute values: 
-5 -4 -3 -2 -1 0 6 7 8 9 10 

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)