Skip to content

Commit c3e871d

Browse files
committed
Add test for Seq.iter
1 parent 4e5f010 commit c3e871d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/fsharp/FSharp.Compiler.Unittests/SeqFusion.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ type SeqFusionTestsModule() =
3939
// evaluate it
4040
Assert.areEqual [15; 15; 3] (Seq.toList result)
4141

42+
Assert.areEqual ["hello"; "5"; "world"; "5"; "!"; "1"] (Seq.toList list)
43+
44+
[<Test>]
45+
member this.FusisonOfMapIntoIterKeepsSideEffectOrder() =
46+
let list = List<string>()
47+
let data = ["hello"; "world"; "!"]
48+
let results = List<int>()
49+
50+
Seq.iter (fun x -> results.Add x) (Seq.map (fun x -> list.Add(x.ToString()); x * 3) (Seq.map (fun y -> list.Add y; y.Length) data))
51+
52+
Assert.areEqual [15; 15; 3] (Seq.toList results)
4253
Assert.areEqual ["hello"; "5"; "world"; "5"; "!"; "1"] (Seq.toList list)

src/fsharp/Optimizer.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,14 +2619,13 @@ and OptimizeApplication cenv env (f0,f0ty,tyargs,args,m) =
26192619
| _ ->
26202620
match expr' with
26212621
// Rewrite Seq.map f (Seq.map g) xs into Seq.map (fun x -> f(g x)) xs
2622-
| Expr.App(Expr.Val(valRef,_,_) as outerSeqMap,ttype1,[_;fOutType],
2622+
| Expr.App(Expr.Val(outerValRef,_,_) as outerSeqMap,ttype1,[_;fOutType],
26232623
[(Expr.Lambda(_,None,None,_,_,m1,fRetType) as f)
2624-
Expr.App(Expr.Val(valRef2,_,_),_,[gInType;_],
2624+
Expr.App(Expr.Val(innerValRef,_,_),_,[gInType;_],
26252625
[Expr.Lambda(_,None,None,gVals,g,_,gRetType)
26262626
rest],_)],m2) when
2627-
valRefEq cenv.g valRef cenv.g.seq_map_vref &&
2628-
valRefEq cenv.g valRef2 cenv.g.seq_map_vref
2629-
->
2627+
valRefEq cenv.g innerValRef cenv.g.seq_map_vref &&
2628+
valRefEq cenv.g outerValRef cenv.g.seq_map_vref ->
26302629
let newApp = Expr.App(f,TType_fun(gRetType, fRetType),[],[g],m2)
26312630

26322631
let reduced =

0 commit comments

Comments
 (0)