Skip to content

Commit 9744c13

Browse files
committed
For is too eager
1 parent c39e9a0 commit 9744c13

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/FSharp.Control.AsyncSeq/AsyncSeq.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,11 @@ module AsyncSeq =
234234
/// For loop that iterates over a synchronous sequence (and generates
235235
/// all elements generated by the asynchronous body)
236236
member x.For(seq:seq<'T>, action:'T -> AsyncSeq<'TResult>) =
237-
let enum = seq.GetEnumerator()
238-
x.TryFinally(x.While((fun () -> enum.MoveNext()), x.Delay(fun () ->
239-
action enum.Current)), (fun () ->
240-
if enum <> null then enum.Dispose() ))
237+
x.Delay(fun () ->
238+
let enum = seq.GetEnumerator()
239+
x.TryFinally(x.While((fun () -> enum.MoveNext()), x.Delay(fun () ->
240+
action enum.Current)), (fun () ->
241+
if enum <> null then enum.Dispose() )))
241242

242243
/// Asynchronous for loop - for all elements from the input sequence,
243244
/// generate all elements produced by the body (asynchronously). See

tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,15 @@ let ``AsyncSeq.skipUntil should not skip with completed signal``() =
284284
[<Test>]
285285
let ``AsyncSeq.skipUntil should skip everything with never signal``() =
286286
let actual = [1;2;3;4] |> AsyncSeq.ofSeq |> AsyncSeq.skipUntil AsyncOps.never
287-
Assert.True(EQ AsyncSeq.empty actual)
287+
Assert.True(EQ AsyncSeq.empty actual)
288+
289+
290+
291+
[<Test>]
292+
let ``asyncSeq.For should delay``() =
293+
let (s:seq<int>) =
294+
{ new System.Collections.Generic.IEnumerable<int> with
295+
member x.GetEnumerator() = failwith "fail"
296+
interface System.Collections.IEnumerable with
297+
member x.GetEnumerator() = failwith "fail" }
298+
Assert.DoesNotThrow(fun _ -> asyncSeq.For(s, (fun v -> AsyncSeq.empty)) |> ignore)

0 commit comments

Comments
 (0)