Skip to content

Commit

Permalink
Merge pull request dotnet#5 from liboz/manofstick
Browse files Browse the repository at this point in the history
Seq.distinct(By)?
  • Loading branch information
manofstick authored Oct 13, 2016
2 parents dc8b5b1 + 912304b commit a066f34
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/fsharp/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,14 @@ namespace Microsoft.FSharp.Collections
and ChooseFactory<'T,'U> (filter:'T->option<'U>) =
inherit SeqComponentFactory<'T,'U> ()
override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'U,'V>) : SeqComponent<'T,'V> = upcast Choose (filter, next)

and DistinctFactory<'T when 'T: equality> () =
inherit SeqComponentFactory<'T,'T> ()
override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast Distinct (next)

and DistinctByFactory<'T,'Key when 'Key: equality> (keyFunction:'T-> 'Key) =
inherit SeqComponentFactory<'T,'T> ()
override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast DistinctBy (keyFunction, next)

and FilterFactory<'T> (filter:'T->bool) =
inherit SeqComponentFactory<'T,'T> ()
Expand Down Expand Up @@ -761,6 +769,28 @@ namespace Microsoft.FSharp.Collections
| Some value -> Helpers.avoidTailCall (next.ProcessNext value)
| None -> false

and Distinct<'T,'V when 'T: equality> (next:SeqComponent<'T,'V>) =
inherit SeqComponent<'T,'V>(next)

let hashSet = HashSet<'T>(HashIdentity.Structural<'T>)

override __.ProcessNext (input:'T) : bool =
if hashSet.Add input then
Helpers.avoidTailCall (next.ProcessNext input)
else
false

and DistinctBy<'T,'Key,'V when 'Key: equality> (keyFunction: 'T -> 'Key, next:SeqComponent<'T,'V>) =
inherit SeqComponent<'T,'V>(next)

let hashSet = HashSet<'Key>(HashIdentity.Structural<'Key>)

override __.ProcessNext (input:'T) : bool =
if hashSet.Add(keyFunction input) then
Helpers.avoidTailCall (next.ProcessNext input)
else
false

and Filter<'T,'V> (filter:'T->bool, next:SeqComponent<'T,'V>) =
inherit SeqComponent<'T,'V>(next)

Expand Down Expand Up @@ -1944,19 +1974,11 @@ namespace Microsoft.FSharp.Collections

[<CompiledName("Distinct")>]
let distinct source =
checkNonNull "source" source
seq { let hashSet = HashSet<'T>(HashIdentity.Structural<'T>)
for v in source do
if hashSet.Add(v) then
yield v }
source |> seqFactory (SeqComposer.DistinctFactory ())

[<CompiledName("DistinctBy")>]
let distinctBy keyf source =
checkNonNull "source" source
seq { let hashSet = HashSet<_>(HashIdentity.Structural<_>)
for v in source do
if hashSet.Add(keyf v) then
yield v }
source |> seqFactory (SeqComposer.DistinctByFactory keyf)

[<CompiledName("SortBy")>]
let sortBy keyf source =
Expand Down

0 comments on commit a066f34

Please sign in to comment.