Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit17ad03f

Browse files
Smaug123KevinRansom
authored andcommitted
Stop eagerly evaluating seqs (revert parts ofdotnet#5348,dotnet#5370) (dotnet#5947)
* Fix and tests, unbuilt and untested* Correct one of the testsGot the order wrong for the output of `countBy`.* Repair the groupBy testgroupBy test was bad: it assumed (ridiculously) that groupBy wouldn't iterate the original sequence even though it somehow knew the groups in the sequence.
1 parent87ea864 commit17ad03f

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

‎src/fsharp/FSharp.Core/seq.fs‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,6 @@ namespace Microsoft.FSharp.Collections
10321032
let inlinegroupByImpl(comparer:IEqualityComparer<'SafeKey>)(keyf:'T->'SafeKey)(getKey:'SafeKey->'Key)(seq:seq<'T>)=
10331033
checkNonNull"seq" seq
10341034

1035-
if isEmpty seqthen emptyelse
1036-
10371035
letdict= Dictionary<_,ResizeArray<_>> comparer
10381036

10391037
// Previously this was 1, but I think this is rather stingy, considering that we are already paying
@@ -1087,7 +1085,6 @@ namespace Microsoft.FSharp.Collections
10871085
[<CompiledName("Distinct")>]
10881086
letdistinct source=
10891087
checkNonNull"source" source
1090-
if isEmpty sourcethen emptyelse
10911088
seq{lethashSet= HashSet<'T>(HashIdentity.Structural<'T>)
10921089
for vin sourcedo
10931090
if hashSet.Add(v)then
@@ -1096,7 +1093,6 @@ namespace Microsoft.FSharp.Collections
10961093
[<CompiledName("DistinctBy")>]
10971094
letdistinctBy projection source=
10981095
checkNonNull"source" source
1099-
if isEmpty sourcethen emptyelse
11001096
seq{lethashSet= HashSet<_>(HashIdentity.Structural<_>)
11011097
for vin sourcedo
11021098
if hashSet.Add(projection v)then
@@ -1140,7 +1136,6 @@ namespace Microsoft.FSharp.Collections
11401136

11411137
let inlinecountByImpl(comparer:IEqualityComparer<'SafeKey>)(keyf:'T->'SafeKey)(getKey:'SafeKey->'Key)(source:seq<'T>)=
11421138
checkNonNull"source" source
1143-
if isEmpty sourcethen emptyelse
11441139

11451140
letdict= Dictionary comparer
11461141

‎tests/FSharp.Core.UnitTests/FSharp.Core.UnitTests.fsproj‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
<HintPathCondition="'$(TargetDotnetProfile)' == 'net40'">$(FsCheckLibDir)\net452\FsCheck.dll</HintPath>
5353
</Reference>
5454
<ReferenceInclude="System.ValueTuple">
55-
<HintPath>..\..\packages\System.ValueTuple.$(SystemValueTuplePackageVersion)\lib\portable-net40+sl4+win8+wp8\System.ValueTuple.dll</HintPath>
56-
<Private>True</Private>
55+
<HintPath>..\..\packages\System.ValueTuple.$(SystemValueTuplePackageVersion)\lib\portable-net40+sl4+win8+wp8\System.ValueTuple.dll</HintPath>
56+
<Private>True</Private>
5757
</Reference>
5858
</ItemGroup>
5959
<ItemGroup>
@@ -94,6 +94,7 @@
9494
<CompileInclude="FSharp.Core\Microsoft.FSharp.Collections\SeqProperties.fs" />
9595
<CompileInclude="FSharp.Core\Microsoft.FSharp.Collections\CollectionModulesConsistency.fs" />
9696
<CompileInclude="FSharp.Core\Microsoft.FSharp.Collections\StringModule.fs" />
97+
<CompileInclude="FSharp.Core\Microsoft.FSharp.Collections\SeqMultipleIteration.fs" />
9798
<CompileInclude="FSharp.Core\PrimTypes.fs" />
9899
<CompileInclude="FSharp.Core\ComparersRegression.fs" />
99100
<CompileInclude="FSharp.Core\DiscrimantedUnionType.fs" />
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespaceFSharp.Core.UnitTests.FSharp_Core.Microsoft_FSharp_Collections
2+
3+
openNUnit.Framework
4+
5+
[<TestFixture>]
6+
moduleSeqMultipleIteration=
7+
letmakeNewSeq()=
8+
lethaveCalled=false|> ref
9+
seq{
10+
if!haveCalledthen failwith"Should not have iterated this sequence before"
11+
haveCalled:= true
12+
yield3
13+
}, haveCalled
14+
15+
[<Test>]
16+
let``Seq.distinct only evaluates the seq once``()=
17+
lets,haveCalled= makeNewSeq()
18+
letdistincts= Seq.distinct s
19+
Assert.IsFalse!haveCalled
20+
CollectionAssert.AreEqual(distincts|> Seq.toList,[3])
21+
Assert.IsTrue!haveCalled
22+
23+
[<Test>]
24+
let``Seq.distinctBy only evaluates the seq once``()=
25+
lets,haveCalled= makeNewSeq()
26+
letdistincts= Seq.distinctBy id s
27+
Assert.IsFalse!haveCalled
28+
CollectionAssert.AreEqual(distincts|> Seq.toList,[3])
29+
Assert.IsTrue!haveCalled
30+
31+
[<Test>]
32+
let``Seq.groupBy only evaluates the seq once``()=
33+
lets,haveCalled= makeNewSeq()
34+
letgroups:seq<int*seq<int>>= Seq.groupBy id s
35+
Assert.IsFalse!haveCalled
36+
letgroups:list<int*seq<int>>= Seq.toList groups
37+
// Seq.groupBy iterates the entire sequence as soon as it begins iteration.
38+
Assert.IsTrue!haveCalled
39+
40+
[<Test>]
41+
let``Seq.countBy only evaluates the seq once``()=
42+
lets,haveCalled= makeNewSeq()
43+
letcounts:seq<int*int>= Seq.countBy id s
44+
Assert.IsFalse!haveCalled
45+
letcounts:list<int*int>= Seq.toList counts
46+
Assert.IsTrue!haveCalled
47+
CollectionAssert.AreEqual(counts|> Seq.toList,[(3,1)])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp