From 81ec98ce03eb2294bf2c8f9c184aa250f0feb1c2 Mon Sep 17 00:00:00 2001 From: liboz Date: Tue, 11 Oct 2016 16:28:24 -0400 Subject: [PATCH 1/3] fix typo --- src/fsharp/FSharp.Core/seq.fs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fsharp/FSharp.Core/seq.fs b/src/fsharp/FSharp.Core/seq.fs index 24e42f51c14..7c7c92376f6 100644 --- a/src/fsharp/FSharp.Core/seq.fs +++ b/src/fsharp/FSharp.Core/seq.fs @@ -718,13 +718,13 @@ namespace Microsoft.FSharp.Collections inherit SeqComponentFactory<'T,'T> () override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast Skip (count, next) - and SkipWhileFactory<'T> (perdicate:'T->bool) = + and SkipWhileFactory<'T> (predicate:'T->bool) = inherit SeqComponentFactory<'T,'T> () - override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast SkipWhile (perdicate, next) + override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast SkipWhile (predicate, next) - and TakeWhileFactory<'T> (perdicate:'T->bool) = + and TakeWhileFactory<'T> (predicate:'T->bool) = inherit SeqComponentFactory<'T,'T> () - override __.Create<'V> (result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast TakeWhile (perdicate, result, next) + override __.Create<'V> (result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast TakeWhile (predicate, result, next) and TakeFactory<'T> (count:int) = inherit SeqComponentFactory<'T,'T> () From 4ebe33612b1b47a288838ed69aa07ad971f5db50 Mon Sep 17 00:00:00 2001 From: liboz Date: Tue, 11 Oct 2016 21:16:38 -0400 Subject: [PATCH 2/3] truncate --- src/fsharp/FSharp.Core/seq.fs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/fsharp/FSharp.Core/seq.fs b/src/fsharp/FSharp.Core/seq.fs index 7c7c92376f6..f2c5b5c8db0 100644 --- a/src/fsharp/FSharp.Core/seq.fs +++ b/src/fsharp/FSharp.Core/seq.fs @@ -729,6 +729,10 @@ namespace Microsoft.FSharp.Collections and TakeFactory<'T> (count:int) = inherit SeqComponentFactory<'T,'T> () override __.Create<'V> (result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast Take (count, result, next) + + and TruncateFactory<'T> (count:int) = + inherit SeqComponentFactory<'T,'T> () + override __.Create<'V> (result:Result<'V>) (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> = upcast Truncate (count, result, next) and [] SeqComponent<'T,'U> (next:ISeqComponent) = abstract ProcessNext : input:'T -> bool @@ -939,6 +943,21 @@ namespace Microsoft.FSharp.Collections result.Current <- input true + and Truncate<'T,'V> (takeCount:int, result:Result<'V>, next:SeqComponent<'T,'V>) = + inherit SeqComponent<'T,'V>(next) + + let mutable count = 0 + + override __.ProcessNext (input:'T) : bool = + if count < takeCount then + count <- count + 1 + if count = takeCount then + result.StopFurtherProcessing () + next.ProcessNext input + else + result.StopFurtherProcessing () + false + module Enumerable = [] type EnumeratorBase<'T>(result:Result<'T>, seqComponent:ISeqComponent) = @@ -1716,12 +1735,7 @@ namespace Microsoft.FSharp.Collections [] let truncate n (source: seq<'T>) = - checkNonNull "source" source - seq { let i = ref 0 - use ie = source.GetEnumerator() - while !i < n && ie.MoveNext() do - i := !i + 1 - yield ie.Current } + source |> seqFactory (SeqComposer.TruncateFactory n) [] let pairwise<'T> (source:seq<'T>) : seq<'T*'T> = From 2669cbf77ed4a11f6e6753fc0e90324a6459a168 Mon Sep 17 00:00:00 2001 From: liboz Date: Tue, 11 Oct 2016 22:36:09 -0400 Subject: [PATCH 3/3] using inheritance for take --- src/fsharp/FSharp.Core/seq.fs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/fsharp/FSharp.Core/seq.fs b/src/fsharp/FSharp.Core/seq.fs index f2c5b5c8db0..e7f40e14e69 100644 --- a/src/fsharp/FSharp.Core/seq.fs +++ b/src/fsharp/FSharp.Core/seq.fs @@ -904,24 +904,12 @@ namespace Microsoft.FSharp.Collections Helpers.avoidTailCall (next.ProcessNext input) and Take<'T,'V> (takeCount:int, result:Result<'V>, next:SeqComponent<'T,'V>) = - inherit SeqComponent<'T,'V>(next) - - let mutable count = 0 - - override __.ProcessNext (input:'T) : bool = - if count < takeCount then - count <- count + 1 - if count = takeCount then - result.StopFurtherProcessing () - next.ProcessNext input - else - result.StopFurtherProcessing () - false + inherit Truncate<'T, 'V>(takeCount, result, next) interface ISeqComponent with - override __.OnComplete () = - if count < takeCount then - let x = takeCount - count + override this.OnComplete () = + if this.Count < takeCount then + let x = takeCount - this.Count invalidOpFmt "tried to take {0} {1} past the end of the seq" [|SR.GetString SR.notEnoughElements; x; (if x=1 then "element" else "elements")|] (Helpers.UpcastISeqComponent next).OnComplete () @@ -943,15 +931,17 @@ namespace Microsoft.FSharp.Collections result.Current <- input true - and Truncate<'T,'V> (takeCount:int, result:Result<'V>, next:SeqComponent<'T,'V>) = + and Truncate<'T,'V> (truncateCount:int, result:Result<'V>, next:SeqComponent<'T,'V>) = inherit SeqComponent<'T,'V>(next) let mutable count = 0 + member __.Count = count + override __.ProcessNext (input:'T) : bool = - if count < takeCount then + if count < truncateCount then count <- count + 1 - if count = takeCount then + if count = truncateCount then result.StopFurtherProcessing () next.ProcessNext input else