Skip to content

Commit

Permalink
Merge pull request dotnet#4 from liboz/manofstick
Browse files Browse the repository at this point in the history
Seq.truncate (and fixing a typo)
  • Loading branch information
manofstick authored Oct 12, 2016
2 parents 740073d + 2669cbf commit 3ce09d8
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions src/fsharp/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -718,17 +718,21 @@ 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> ()
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 [<AbstractClass>] SeqComponent<'T,'U> (next:ISeqComponent) =
abstract ProcessNext : input:'T -> bool
Expand Down Expand Up @@ -900,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 ()
Expand All @@ -939,6 +931,23 @@ namespace Microsoft.FSharp.Collections
result.Current <- input
true

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 < truncateCount then
count <- count + 1
if count = truncateCount then
result.StopFurtherProcessing ()
next.ProcessNext input
else
result.StopFurtherProcessing ()
false

module Enumerable =
[<AbstractClass>]
type EnumeratorBase<'T>(result:Result<'T>, seqComponent:ISeqComponent) =
Expand Down Expand Up @@ -1716,12 +1725,7 @@ namespace Microsoft.FSharp.Collections

[<CompiledName("Truncate")>]
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)

[<CompiledName("Pairwise")>]
let pairwise<'T> (source:seq<'T>) : seq<'T*'T> =
Expand Down

0 comments on commit 3ce09d8

Please sign in to comment.