Seq.distinctBy<'T,'Key> Function (F#)
Visual Studio 2010
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
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