From 5c69948be25f5e1f45d8f8afeeccf15e0606e466 Mon Sep 17 00:00:00 2001 From: Gustavo Leon <1261319+gusty@users.noreply.github.com> Date: Sun, 18 Sep 2022 23:56:06 +0200 Subject: [PATCH] + Applicative short-circuit test --- tests/FSharpPlus.Tests/SeqT.fs | 42 +++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/FSharpPlus.Tests/SeqT.fs b/tests/FSharpPlus.Tests/SeqT.fs index 56d09e32a..d2a3423f1 100644 --- a/tests/FSharpPlus.Tests/SeqT.fs +++ b/tests/FSharpPlus.Tests/SeqT.fs @@ -170,6 +170,46 @@ module ComputationExpressions = () +module Applicative = + [] + let applicateiveShortCircuits () = + + // This should be the case for any lazy monad stack with short-circuit applicative (Collections, Options, short-circuits Eithers) + + let mutable actions : string list = [] + + let getNumberO i : SeqT, int> = monad.plus { + let! _ = + monad' { + do! Task.Delay 10 |> Task.ignore + actions <- "init" :: actions + } + |> SeqT.lift + + let! x = + monad' { + do! Task.Delay 10 |> Task.ignore + actions <- ("read " + (string i)) :: actions + return (string i) + } + |>> tryParse + |>> Option.toList + |>> List.toSeq + |> SeqT + if x = i + 10 then yield x // will be always false + } + + let seqt = result (+) <*> getNumberO 1 <*> getNumberO 2 + + // Non-lazy stacks would have been executed at this point + CollectionAssert.AreEqual (actions, []) + + let res = SeqT.run seqt + + // Since the first value was an empty list, no further effect should be expected + CollectionAssert.AreEqual (actions, ["read 1"; "init"]) + CollectionAssert.AreEqual (res.Result, []) + module AsyncSeq = @@ -280,4 +320,4 @@ module ComputationExpressions = let expected = Seq.zip la lb |> Seq.map ((<||) (+)) |> SeqT.ofSeq Assert.True (EQ expected actual) - \ No newline at end of file +