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

Commit321dcde

Browse files
forkilatkin
authored andcommitted
Implement Array.countBy, List.countBy
commit 30c0a3284b42ec0fd59bd57d8a63f63613b77301Author: latkin <latkin@microsoft.com>Date: Sun Oct 12 12:35:13 2014 -0700 Changing List.countBy to dedicated implementationcommit 976c6e995bdad2ac55ec8314aa1d03c66f644423Merge:15f04de c521808Author: latkin <latkin@microsoft.com>Date: Sun Oct 12 10:43:21 2014 -0700 Merge branch 'countBy3' ofhttps://git01.codeplex.com/forks/forki/fsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule.fs src/fsharp/FSharp.Core/seq.fsicommit c5218084e04c4fb9ea5a1ee6c92c8647258cb542Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Sat Jul 5 10:47:27 2014 +0200 ok variable is not needed in countBycommit fa934cf2aa536662df38175388d94665c7d87132Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Fri Jul 4 15:36:20 2014 +0200 Adding surface area for countBycommit 63e669155017bf186bfdeb7db47a2f59bdce6765Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Wed Jul 2 16:59:46 2014 +0200 Use direct array implementation for Array.countBycommit 0a975603f071973a19a10f1cb4c4cedc78d05158Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Wed Jul 2 16:34:50 2014 +0200 Use only one test method for countBycommit a25a085cab89b1a9af193e19f8469e25a984f0bfAuthor: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Wed Jul 2 16:32:51 2014 +0200 Fix typo in countBy docscommit 5d9af6ea78000d5aab885f6361556a72a6d04078Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Wed Jul 2 16:32:40 2014 +0200 Rename to array in Array.countBycommit deeaba955ce60ef3981797d16ec423a1f34f863aAuthor: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Mon Jun 30 16:15:02 2014 +0200 Show possible System.ArgumentNullException in docs for Array.countBycommit 532cdccafd47561517e276acc98b990e5f78b6f1Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Mon Jun 30 12:10:39 2014 +0200 implementing "countBy" for array and list
1 parent15f04de commit321dcde

File tree

10 files changed

+88
-6
lines changed

10 files changed

+88
-6
lines changed

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule.fs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,18 @@ type ArrayModule() =
421421
CheckThrowsNullRefException(fun()-> Array.concat nullArrays|> ignore)
422422

423423
()
424-
424+
425+
[<Test>]
426+
memberthis.countBy()=
427+
// countBy should work on empty array
428+
Assert.AreEqual([],Array.countBy(fun _-> failwith"should not be executed")[||])
429+
430+
// countBy should not work on null
431+
CheckThrowsArgumentNullException(fun()-> Array.countBy(fun _-> failwith"should not be executed")null|> ignore)
432+
433+
// countBy should count by the given key function
434+
Assert.AreEqual([|5,1;2,2;3,2|],Array.countBy id[|5;2;2;3;3|])
435+
Assert.AreEqual([|3,3;2,2;1,3|],Array.countBy(fun x->if x<3then xelse3)[|5;2;1;2;3;3;1;1|])
425436

426437
[<Test>]
427438
memberthis.Copy()=

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule.fs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ type ListModule() =
263263
CheckThrowsInvalidOperationExn(fun()-> List.splitAt1[]|> ignore)
264264
CheckThrowsArgumentException(fun()-> List.splitAt-1[0;1]|> ignore)
265265
CheckThrowsInvalidOperationExn(fun()-> List.splitAt5["str1";"str2";"str3";"str4"]|> ignore)
266+
()
267+
268+
[<Test>]
269+
memberthis.countBy()=
270+
// countBy should work on empty list
271+
Assert.AreEqual([],List.countBy(fun _-> failwith"should not be executed")[])
272+
273+
// countBy should count by the given key function
274+
Assert.AreEqual([5,1;2,2;3,2],List.countBy id[5;2;2;3;3])
275+
Assert.AreEqual([3,3;2,2;1,3],List.countBy(fun x->if x<3then xelse3)[5;2;1;2;3;3;1;1])
266276

267277
[<Test>]
268278
memberthis.Exists()=

‎src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
115115
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
116116
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
117117
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
118+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,System.Int32][] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
118119
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
119120
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] SplitAt[T](Int32, T[])
120121
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`3[T1,T2,T3][] Zip3[T1,T2,T3](T1[], T2[], T3[])
@@ -284,6 +285,7 @@ Microsoft.FSharp.Collections.ListModule: Int32 GetHashCode()
284285
Microsoft.FSharp.Collections.ListModule: Int32 Length[T](Microsoft.FSharp.Collections.FSharpList`1[T])
285286
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
286287
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] Zip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
288+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
287289
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[T1,T2,T3]] Zip3[T1,T2,T3](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2], Microsoft.FSharp.Collections.FSharpList`1[T3])
288290
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[TResult] Choose[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Collections.FSharpList`1[T])
289291
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[TResult] Collect[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Collections.FSharpList`1[TResult]], Microsoft.FSharp.Collections.FSharpList`1[T])

‎src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
109109
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
110110
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
111111
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
112+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,System.Int32][] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
112113
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
113114
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] SplitAt[T](Int32, T[])
114115
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`3[T1,T2,T3][] Zip3[T1,T2,T3](T1[], T2[], T3[])
@@ -278,6 +279,7 @@ Microsoft.FSharp.Collections.ListModule: Int32 GetHashCode()
278279
Microsoft.FSharp.Collections.ListModule: Int32 Length[T](Microsoft.FSharp.Collections.FSharpList`1[T])
279280
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
280281
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] Zip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
282+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
281283
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[T1,T2,T3]] Zip3[T1,T2,T3](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2], Microsoft.FSharp.Collections.FSharpList`1[T3])
282284
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[TResult] Choose[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Collections.FSharpList`1[T])
283285
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[TResult] Collect[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Collections.FSharpList`1[TResult]], Microsoft.FSharp.Collections.FSharpList`1[T])

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ namespace Microsoft.FSharp.Collections
168168
Array.Copy(array,0, res,0, count)
169169
res
170170

171+
[<CompiledName("CountBy")>]
172+
letcountBy projection(array:'T[])=
173+
checkNonNull"array" array
174+
letdict=new Dictionary<Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers.StructBox<'Key>,int>(Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers.StructBox<'Key>.Comparer)
175+
176+
// Build the groupings
177+
for vin arraydo
178+
letkey= Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers.StructBox(projection v)
179+
let mutableprev= Unchecked.defaultof<_>
180+
if dict.TryGetValue(key,&prev)then dict.[key]<- prev+1else dict.[key]<-1
181+
182+
letres= Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked dict.Count
183+
let mutablei=0
184+
for groupin dictdo
185+
res.[i]<- group.Key.Value, group.Value
186+
i<- i+1
187+
res
188+
171189
[<CompiledName("Append")>]
172190
letappend(array1:'T[])(array2:'T[])=
173191
checkNonNull"array1" array1

‎src/fsharp/FSharp.Core/array.fsi‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ namespace Microsoft.FSharp.Collections
9494
[<CompiledName("Copy")>]
9595
val copy:array:'T[]-> 'T[]
9696

97+
///<summary>Applies a key-generating function to each element of an array and returns an array yielding unique
98+
///keys and their number of occurrences in the original array.</summary>
99+
///
100+
///<param name="projection">A function transforming each item of the input array into a key to be
101+
///compared against the others.</param>
102+
///<param name="array">The input array.</param>
103+
///
104+
///<returns>The result array.</returns>
105+
///
106+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
107+
[<CompiledName("CountBy")>]
108+
val countBy:projection:('T-> 'Key)->array:'T[]->('Key*int)[]when 'Key:equality
109+
97110
///<summary>Creates an array whose elements are all initially the given value.</summary>
98111
///<param name="count">The length of the array to create.</param>
99112
///<param name="value">The value for the elements.</param>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ namespace Microsoft.FSharp.Collections
3232
[<CompiledName("Concat")>]
3333
letconcat lists= Microsoft.FSharp.Primitives.Basics.List.concat lists
3434

35+
[<CompiledName("CountBy")>]
36+
letcountBy projection(list:'T list)=
37+
letdict=new Dictionary<Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers.StructBox<'Key>,int>(Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers.StructBox<'Key>.Comparer)
38+
let recloop srcList=
39+
match srcListwith
40+
|[]->()
41+
| h::t->
42+
letkey= Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers.StructBox(projection h)
43+
let mutableprev=0
44+
if dict.TryGetValue(key,&prev)then dict.[key]<- prev+1else dict.[key]<-1
45+
loop t
46+
loop list
47+
let mutableresult=[]
48+
for groupin dictdo
49+
result<-(group.Key.Value, group.Value):: result
50+
result|> rev
51+
3552
[<CompiledName("Map")>]
3653
letmap f list= Microsoft.FSharp.Primitives.Basics.List.map f list
3754

‎src/fsharp/FSharp.Core/list.fsi‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ namespace Microsoft.FSharp.Collections
109109
[<CompiledName("DistinctBy")>]
110110
val distinctBy:projection:('T-> 'Key)->list:'T list-> 'T list when 'Key:equality
111111

112+
///<summary>Applies a key-generating function to each element of a list and returns a list yielding unique
113+
///keys and their number of occurrences in the original list.</summary>
114+
///
115+
///<param name="projection">A function transforming each item of the input list into a key to be
116+
///compared against the others.</param>
117+
///<param name="list">The input list.</param>
118+
///
119+
///<returns>The result list.</returns>
120+
[<CompiledName("CountBy")>]
121+
val countBy:projection:('T-> 'Key)->list:'T list->('Key*int)list when 'Key:equality
122+
112123
///<summary>Returns an empty list of the given type.</summary>
113124
[<GeneralizableValue>]
114125
[<CompiledName("Empty")>]

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,9 +1369,7 @@ namespace Microsoft.FSharp.Collections
13691369
source|> iter(fun v->
13701370
letkey= StructBox(keyf v)
13711371
let mutableprev= Unchecked.defaultof<_>
1372-
letok= dict.TryGetValue(key,&prev)
1373-
if okthen dict.[key]<- prev+1
1374-
else dict.[key]<-1)
1372+
if dict.TryGetValue(key,&prev)then dict.[key]<- prev+1else dict.[key]<-1)
13751373

13761374
dict|> map(fun group->(group.Key.Value, group.Value)))
13771375

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ namespace Microsoft.FSharp.Collections
183183
[<CompiledName("Contains")>]
184184
val inline contains:value:'T->source:seq<'T>->bool when 'T:equality
185185

186-
///<summary>Applies a key-generating function to each element of a sequence andreturn a sequence yielding unique
186+
///<summary>Applies a key-generating function to each element of a sequence andreturns a sequence yielding unique
187187
///keys and their number of occurrences in the original sequence.</summary>
188188
///
189189
///<remarks>Note that this function returns a sequence that digests the whole initial sequence as soon as
190190
///that sequence is iterated. As a result this function should not be used with
191191
///large or infinite sequences. The function makes no assumption on the ordering of the original
192192
///sequence.</remarks>
193193
///
194-
///<param name="projection">A function transforming each item of input sequence into a key to be
194+
///<param name="projection">A function transforming each item oftheinput sequence into a key to be
195195
///compared against the others.</param>
196196
///<param name="source">The input sequence.</param>
197197
///

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp