File tree 1 file changed +41
-1
lines changed
1 file changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,46 @@ module ComputationExpressions =
170
170
171
171
()
172
172
173
+ module Applicative =
174
+ [<Test>]
175
+ let applicateiveShortCircuits () =
176
+
177
+ // This should be the case for any lazy monad stack with short-circuit applicative (Collections, Options, short-circuits Eithers)
178
+
179
+ let mutable actions : string list = []
180
+
181
+ let getNumberO i : SeqT < Task < _ >, int > = monad.plus {
182
+ let! _ =
183
+ monad' {
184
+ do ! Task.Delay 10 |> Task.ignore
185
+ actions <- " init" :: actions
186
+ }
187
+ |> SeqT.lift
188
+
189
+ let! x =
190
+ monad' {
191
+ do ! Task.Delay 10 |> Task.ignore
192
+ actions <- ( " read " + ( string i)) :: actions
193
+ return ( string i)
194
+ }
195
+ |>> tryParse
196
+ |>> Option.toList
197
+ |>> List.toSeq
198
+ |> SeqT
199
+ if x = i + 10 then yield x // will be always false
200
+ }
201
+
202
+ let seqt = result (+) <*> getNumberO 1 <*> getNumberO 2
203
+
204
+ // Non-lazy stacks would have been executed at this point
205
+ CollectionAssert.AreEqual ( actions, [])
206
+
207
+ let res = SeqT.run seqt
208
+
209
+ // Since the first value was an empty list, no further effect should be expected
210
+ CollectionAssert.AreEqual ( actions, [ " read 1" ; " init" ])
211
+ CollectionAssert.AreEqual ( res.Result, [])
212
+
173
213
174
214
module AsyncSeq =
175
215
@@ -280,4 +320,4 @@ module ComputationExpressions =
280
320
let expected = Seq.zip la lb |> Seq.map ((<||) (+)) |> SeqT.ofSeq
281
321
Assert.True ( EQ expected actual)
282
322
283
-
323
+
You can’t perform that action at this time.
0 commit comments