Skip to content

Commit

Permalink
Merge pull request dotnet#6 from liboz/manofstick
Browse files Browse the repository at this point in the history
Seq.except
  • Loading branch information
manofstick authored Oct 13, 2016
2 parents a066f34 + 85432cc commit 2dc69f0
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/fsharp/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@ namespace Microsoft.FSharp.Collections
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 ExceptFactory<'T when 'T: equality> (itemsToExclude: seq<'T>) =
inherit SeqComponentFactory<'T,'T> ()
override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast Except (itemsToExclude, next)

and FilterFactory<'T> (filter:'T->bool) =
inherit SeqComponentFactory<'T,'T> ()
Expand Down Expand Up @@ -791,6 +795,17 @@ namespace Microsoft.FSharp.Collections
else
false

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

let cached = lazy(HashSet(itemsToExclude, HashIdentity.Structural))

override __.ProcessNext (input:'T) : bool =
if cached.Value.Add 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 @@ -2311,17 +2326,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("Except")>]
let except (itemsToExclude: seq<'T>) (source: seq<'T>) =
checkNonNull "itemsToExclude" itemsToExclude
checkNonNull "source" source

seq {
use e = source.GetEnumerator()
if e.MoveNext() then
let cached = HashSet(itemsToExclude, HashIdentity.Structural)
let next = e.Current
if (cached.Add next) then yield next
while e.MoveNext() do
let next = e.Current
if (cached.Add next) then yield next }
source |> seqFactory (SeqComposer.ExceptFactory itemsToExclude)

[<CompiledName("ChunkBySize")>]
let chunkBySize chunkSize (source : seq<_>) =
Expand Down

0 comments on commit 2dc69f0

Please sign in to comment.