Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TaskSeq.skipWhile, skipWhileAsync, skipWhileInclusive and skipWhileInclusiveAsync #219

Merged
merged 13 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ This is what has been implemented so far, is planned or skipped:
| ✅ [#90][] | `singleton` | `singleton` | | |
| ✅ [#209][]| `skip` | `skip` | | |
| ✅ [#209][]| | `drop` | | |
| | `skipWhile` | `skipWhile` | `skipWhileAsync` | |
| | | `skipWhileInclusive` | `skipWhileInclusiveAsync` | |
| ✅ [#219][]| `skipWhile` | `skipWhile` | `skipWhileAsync` | |
| ✅ [#219][]| | `skipWhileInclusive` | `skipWhileInclusiveAsync` | |
| ❓ | `sort` | | | [note #1](#note1 "These functions require a form of pre-materializing through 'TaskSeq.cache', similar to the approach taken in the corresponding 'Seq' functions. It doesn't make much sense to have a cached async sequence. However, 'AsyncSeq' does implement these, so we'll probably do so eventually as well.") |
| ❓ | `sortBy` | | | [note #1](#note1 "These functions require a form of pre-materializing through 'TaskSeq.cache', similar to the approach taken in the corresponding 'Seq' functions. It doesn't make much sense to have a cached async sequence. However, 'AsyncSeq' does implement these, so we'll probably do so eventually as well.") |
| ❓ | `sortByAscending` | | | [note #1](#note1 "These functions require a form of pre-materializing through 'TaskSeq.cache', similar to the approach taken in the corresponding 'Seq' functions. It doesn't make much sense to have a cached async sequence. However, 'AsyncSeq' does implement these, so we'll probably do so eventually as well.") |
Expand Down Expand Up @@ -603,6 +603,7 @@ module TaskSeq =
[#133]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues/133
[#209]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues/209
[#217]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues/217
[#219]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues/219

[issues]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues
[nuget]: https://www.nuget.org/packages/FSharp.Control.TaskSeq/
5 changes: 3 additions & 2 deletions assets/nuget-package-readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ This is what has been implemented so far, is planned or skipped:
| ✅ [#90][] | `singleton` | `singleton` | | |
| ✅ [#209][]| `skip` | `skip` | | |
| ✅ [#209][]| | `drop` | | |
| | `skipWhile` | `skipWhile` | `skipWhileAsync` | |
| | | `skipWhileInclusive` | `skipWhileInclusiveAsync` | |
| ✅ [#219][]| `skipWhile` | `skipWhile` | `skipWhileAsync` | |
| ✅ [#219][]| | `skipWhileInclusive` | `skipWhileInclusiveAsync` | |
| ❓ | `sort` | | | [note #1](#note1 "These functions require a form of pre-materializing through 'TaskSeq.cache', similar to the approach taken in the corresponding 'Seq' functions. It doesn't make much sense to have a cached async sequence. However, 'AsyncSeq' does implement these, so we'll probably do so eventually as well.") |
| ❓ | `sortBy` | | | [note #1](#note1 "These functions require a form of pre-materializing through 'TaskSeq.cache', similar to the approach taken in the corresponding 'Seq' functions. It doesn't make much sense to have a cached async sequence. However, 'AsyncSeq' does implement these, so we'll probably do so eventually as well.") |
| ❓ | `sortByAscending` | | | [note #1](#note1 "These functions require a form of pre-materializing through 'TaskSeq.cache', similar to the approach taken in the corresponding 'Seq' functions. It doesn't make much sense to have a cached async sequence. However, 'AsyncSeq' does implement these, so we'll probably do so eventually as well.") |
Expand Down Expand Up @@ -308,3 +308,4 @@ _The motivation for `readOnly` in `Seq` is that a cast from a mutable array or l
[#126]: https://github.com/fsprojects/FSharp.Control.TaskSeq/pull/126
[#209]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues/209
[#217]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues/217
[#219]: https://github.com/fsprojects/FSharp.Control.TaskSeq/issues/219
7 changes: 4 additions & 3 deletions release-notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ Release notes:
0.4.x (unreleased)
- overhaul all doc comments, add exceptions, improve IDE quick-info experience, #136
- new surface area functions, fixes #208:
* TaskSeq.take, TaskSeq.skip, #209
* TaskSeq.truncate, TaskSeq.drop, #209
* TaskSeq.where, TaskSeq.whereAsync, #217
* TaskSeq.take, skip, #209
* TaskSeq.truncate, drop, #209
* TaskSeq.where, whereAsync, #217
* TaskSeq.skipWhile, skipWhileInclusive, skipWhileAsync, skipWhileInclusiveAsync, #219

- Performance: less thread hops with 'StartImmediateAsTask' instead of 'StartAsTask', fixes #135
- BINARY INCOMPATIBILITY: 'TaskSeq' module is now static members on 'TaskSeq<_>', fixes #184
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<Compile Include="TaskSeq.Pick.Tests.fs" />
<Compile Include="TaskSeq.Singleton.Tests.fs" />
<Compile Include="TaskSeq.Skip.Tests.fs" />
<Compile Include="TaskSeq.SkipWhile.Tests.fs" />
<Compile Include="TaskSeq.Tail.Tests.fs" />
<Compile Include="TaskSeq.Take.Tests.fs" />
<Compile Include="TaskSeq.TakeWhile.Tests.fs" />
Expand Down
322 changes: 322 additions & 0 deletions src/FSharp.Control.TaskSeq.Test/TaskSeq.SkipWhile.Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
module TaskSeq.Tests.skipWhile

open System

open Xunit
open FsUnit.Xunit

open FSharp.Control

//
// TaskSeq.skipWhile
// TaskSeq.skipWhileAsync
// TaskSeq.skipWhileInclusive
// TaskSeq.skipWhileInclusiveAsync
//

exception SideEffectPastEnd of string


module EmptySeq =

// TaskSeq-skipWhile+A stands for:
// skipWhile + skipWhileAsync etc.

[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-skipWhile+A has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhile ((=) 12)
|> verifyEmpty

do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhileAsync ((=) 12 >> Task.fromResult)
|> verifyEmpty
}

[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-skipWhileInclusive+A has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhileInclusive ((=) 12)
|> verifyEmpty

do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhileInclusiveAsync ((=) 12 >> Task.fromResult)
|> verifyEmpty
}

module Immutable =

// TaskSeq-skipWhile+A stands for:
// skipWhile + skipWhileAsync etc.

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhile+A filters correctly`` variant = task {
// truth table for f(x) = x < 5
// 1 2 3 4 5 6 7 8 9 10
// T T T T F F F F F F (stops at first F)
// x x x x _ _ _ _ _ _ (skips exclusive)
// A B C D E F G H I J

do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhile (fun x -> x < 5)
|> verifyDigitsAsString "EFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileAsync (fun x -> task { return x < 5 })
|> verifyDigitsAsString "EFGHIJ"
}

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhile+A does not skip first item when false`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhile ((=) 0)
|> verifyDigitsAsString "ABCDEFGHIJ" // all 10 remain!

do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileAsync ((=) 0 >> Task.fromResult)
|> verifyDigitsAsString "ABCDEFGHIJ" // all 10 remain!
}

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A filters correctly`` variant = task {
// truth table for f(x) = x < 5
// 1 2 3 4 5 6 7 8 9 10
// T T T T F F F F F F (stops at first F)
// x x x x x _ _ _ _ _ (skips inclusively)
// A B C D E F G H I J

do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusive (fun x -> x < 5)
|> verifyDigitsAsString "FGHIJ" // last 4

do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 })
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved
|> verifyDigitsAsString "FGHIJ"
}


[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A returns the empty sequence if always true`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusive (fun x -> x > -1) // always true
|> verifyEmpty

do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x > -1))
|> verifyEmpty
}

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A always skips at least the first item`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusive ((=) 0)
|> verifyDigitsAsString "BCDEFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusiveAsync ((=) 0 >> Task.fromResult)
|> verifyDigitsAsString "BCDEFGHIJ"
}

module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhile+A filters correctly`` variant = task {
// truth table for f(x) = x < 6
// 1 2 3 4 5 6 7 8 9 10
// T T T T T F F F F F (stops at first F)
// x x x x x _ _ _ _ _ (skips exclusively)
// A B C D E F G H I J

do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhile (fun x -> x < 6)
|> verifyDigitsAsString "FGHIJ"

do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhileAsync (fun x -> task { return x < 6 })
|> verifyDigitsAsString "FGHIJ"
}

[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A filters correctly`` variant = task {
// truth table for f(x) = x < 6
// 1 2 3 4 5 6 7 8 9 10
// T T T T T F F F F F (stops at first F)
// x x x x x x _ _ _ _ (skips inclusively)
// A B C D E F G H I J

do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhileInclusive (fun x -> x < 6)
|> verifyDigitsAsString "GHIJ"

do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 6 })
|> verifyDigitsAsString "GHIJ"
}

[<Fact>]
let ``TaskSeq-skipWhile and variants prove it reads the entire input stream`` () =

let mutable x = 42

let items = taskSeq {
yield x
yield x * 2
x <- x + 1 // we are proving we ALWAYS get here
}

// this needs to be lifted from the task or it raises the infamous
// warning FS3511 on CI: This state machine is not statically compilable
let testSkipper skipper expected = task {
let! first = items |> skipper |> TaskSeq.toArrayAsync
return first |> should equal expected
}

task {
do! testSkipper (TaskSeq.skipWhile ((=) 42)) [| 84 |]
x |> should equal 43

do! testSkipper (TaskSeq.skipWhileInclusive ((=) 43)) [||]
x |> should equal 44

do! testSkipper (TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x = 44))) [| 88 |]
x |> should equal 45

do! testSkipper (TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x = 45))) [||]
x |> should equal 46
}

[<Fact>]
let ``TaskSeq-skipWhile and variants prove side effects are properly executed`` () =
let mutable x = 41

let items = taskSeq {
x <- x + 1
yield x
x <- x + 2
yield x * 2
x <- x + 200 // as previously proven, we should ALWAYS trigger this
}

// this needs to be lifted from the task or it raises the infamous
// warning FS3511 on CI: This state machine is not statically compilable
let testSkipper skipper expected = task {
let! first = items |> skipper |> TaskSeq.toArrayAsync
return first |> should equal expected
}

task {
do! testSkipper (TaskSeq.skipWhile ((=) 42)) [| 88 |]
x |> should equal 244

do! testSkipper (TaskSeq.skipWhileInclusive ((=) 245)) [||]
x |> should equal 447

do! testSkipper (TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x = 448))) [| 900 |]
x |> should equal 650

do! testSkipper (TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x = 651))) [||]
x |> should equal 853
}

[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhile consumes the prefix of a longer sequence, with mutation`` variant = task {
let ts = Gen.getSeqWithSideEffect variant

let! first =
TaskSeq.skipWhile (fun x -> x < 5) ts
|> TaskSeq.toArrayAsync

let expected = [| 5..10 |]
first |> should equal expected

// side effect, reiterating causes it to resume from where we left it (minus the failing item)
// which means the original sequence has now changed due to the side effect
let! repeat =
TaskSeq.skipWhile (fun x -> x < 5) ts
|> TaskSeq.toArrayAsync

repeat |> should not' (equal expected)
}

[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhileInclusiveAsync consumes the prefix for a longer sequence, with mutation`` variant = task {
let ts = Gen.getSeqWithSideEffect variant

let! first =
TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 }) ts
|> TaskSeq.toArrayAsync

let expected = [| 6..10 |]
first |> should equal expected

// side effect, reiterating causes it to resume from where we left it (minus the failing item)
// which means the original sequence has now changed due to the side effect
let! repeat =
TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 }) ts
|> TaskSeq.toArrayAsync

repeat |> should not' (equal expected)
}

module Other =
[<Fact>]
let ``TaskSeq-skipWhileXXX should include all items after predicate fails`` () = task {
do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhile (fun x -> x <= 2)
|> verifyDigitsAsString "CCBA"

do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhileInclusive (fun x -> x <= 2)
|> verifyDigitsAsString "CBA"

do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x <= 2))
|> verifyDigitsAsString "CCBA"

do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x <= 2))
|> verifyDigitsAsString "CBA"
}

[<Fact>]
let ``TaskSeq-skipWhileXXX stops consuming after predicate fails`` () =
let testSkipper skipper =
fun () ->
seq {
yield! [ 1; 2; 2; 3; 3 ]
yield SideEffectPastEnd "Too far" |> raise
}
|> TaskSeq.ofSeq
|> skipper
|> consumeTaskSeq
|> should throwAsyncExact typeof<SideEffectPastEnd>

testSkipper (TaskSeq.skipWhile (fun x -> x <= 2))
testSkipper (TaskSeq.skipWhileInclusive (fun x -> x <= 2))
testSkipper (TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x <= 2)))
testSkipper (TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x <= 2)))
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved