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

Commitb9541ad

Browse files
forkilatkin
authored andcommitted
Implement Array.pairwise, List.pairwise
Commits: implementing "pairwise" for array and list Show possible System.ArgumentNullException in docs for Array.pairwise use Array.init version for Array.pairwise use Array.init version for List.pairwise Adding surface area for pairwise
1 parent0e57aa3 commitb9541ad

File tree

8 files changed

+59
-1
lines changed

8 files changed

+59
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,18 @@ type ArrayModule() =
10051005

10061006
()
10071007

1008+
[<Test>]
1009+
memberthis.``pairwise should return pairs of the input array``()=
1010+
Assert.AreEqual([||],Array.pairwise[||])
1011+
Assert.AreEqual([||],Array.pairwise[|1|])
1012+
Assert.AreEqual([|1,2|],Array.pairwise[|1;2|])
1013+
Assert.AreEqual([|1,2;2,3|],Array.pairwise[|1;2;3|])
1014+
Assert.AreEqual([|"H","E";"E","L";"L","L";"L","O"|],Array.pairwise[|"H";"E";"L";"L";"O"|])
1015+
1016+
[<Test>]
1017+
memberthis.``pairwise should not work on null``()=
1018+
CheckThrowsArgumentNullException(fun()-> Array.pairwisenull|> ignore)
1019+
10081020
memberprivatethis.MapTester mapInt(mapString:(string-> int)->array<string>->array<int>)=
10091021
// empty array
10101022
letf x= x+1

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,4 +694,13 @@ type ListModule() =
694694
Assert.AreEqual([null],List.singletonnull)
695695
Assert.AreEqual(["1"],List.singleton"1")
696696
Assert.AreEqual([[]],List.singleton[])
697-
Assert.AreEqual([[||]],List.singleton[||])
697+
Assert.AreEqual([[||]],List.singleton[||])
698+
()
699+
700+
[<Test>]
701+
memberthis.``pairwise should return pairs of the input list``()=
702+
Assert.AreEqual([],List.pairwise[])
703+
Assert.AreEqual([],List.pairwise[1])
704+
Assert.AreEqual([1,2],List.pairwise[1;2])
705+
Assert.AreEqual([1,2;2,3],List.pairwise[1;2;3])
706+
Assert.AreEqual(["H","E";"E","L";"L","L";"L","O"],List.pairwise["H";"E";"L";"L";"O"])

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T
111111
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
112112
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
113113
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
114+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
114115
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
115116
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
116117
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
@@ -272,6 +273,7 @@ Microsoft.FSharp.Collections.ListModule: Int32 CompareWith[T](Microsoft.FSharp.C
272273
Microsoft.FSharp.Collections.ListModule: Int32 FindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
273274
Microsoft.FSharp.Collections.ListModule: Int32 GetHashCode()
274275
Microsoft.FSharp.Collections.ListModule: Int32 Length[T](Microsoft.FSharp.Collections.FSharpList`1[T])
276+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
275277
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])
276278
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])
277279
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])

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T
105105
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
106106
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
107107
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
108+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
108109
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
109110
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
110111
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
@@ -266,6 +267,7 @@ Microsoft.FSharp.Collections.ListModule: Int32 CompareWith[T](Microsoft.FSharp.C
266267
Microsoft.FSharp.Collections.ListModule: Int32 FindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
267268
Microsoft.FSharp.Collections.ListModule: Int32 GetHashCode()
268269
Microsoft.FSharp.Collections.ListModule: Int32 Length[T](Microsoft.FSharp.Collections.FSharpList`1[T])
270+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
269271
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])
270272
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])
271273
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])

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ namespace Microsoft.FSharp.Collections
473473
[<CompiledName("Singleton")>]
474474
let inlinesingleton value=[|value|]
475475

476+
[<CompiledName("Pairwise")>]
477+
letpairwise(array:'T[])=
478+
checkNonNull"array" array
479+
if array.Length<2then[||]else
480+
init(array.Length-1)(fun i-> array.[i],array.[i+1])
481+
476482
[<CompiledName("Reduce")>]
477483
letreduce f(array:_[])=
478484
checkNonNull"array" array

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,17 @@ namespace Microsoft.FSharp.Collections
438438
[<CompiledName("OfSeq")>]
439439
val ofSeq:source:seq<'T>-> 'T[]
440440

441+
///<summary>Returns an array of each element in the input array and its predecessor,with the
442+
///exception of the first element which is only returned as the predecessor of the second element.</summary>
443+
///
444+
///<param name="array">The input array.</param>
445+
///
446+
///<returns>The result array.</returns>
447+
///
448+
///<exception cref="System.ArgumentNullException">Thrown when the input sequence is null.</exception>
449+
[<CompiledName("Pairwise")>]
450+
val pairwise:array:'T[]->('T* 'T)[]
451+
441452
///<summary>Splits the collection into two collections,containing the
442453
///elements for which the given predicate returns"true"and"false"
443454
///respectively.</summary>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ namespace Microsoft.FSharp.Collections
145145
| h::t-> loop(f.Invoke(s,h)) t
146146
loop s list
147147

148+
[<CompiledName("Pairwise")>]
149+
letpairwise(list:'T list)=
150+
letarray= List.toArray list
151+
if array.Length<2then[]else
152+
List.init(array.Length-1)(fun i-> array.[i],array.[i+1])
153+
154+
148155
[<CompiledName("Reduce")>]
149156
letreduce f list=
150157
match listwith

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ namespace Microsoft.FSharp.Collections
389389
[<CompiledName("OfSeq")>]
390390
val ofSeq:source:seq<'T>-> 'T list
391391

392+
///<summary>Returns a list of each element in the input list and its predecessor,with the
393+
///exception of the first element which is only returned as the predecessor of the second element.</summary>
394+
///
395+
///<param name="list">The input list.</param>
396+
///
397+
///<returns>The result list.</returns>
398+
[<CompiledName("Pairwise")>]
399+
val pairwise:list:'T list->('T* 'T)list
400+
392401
///<summary>Splits the collection into two collections,containing the
393402
///elements for which the given predicate returns <c>true</c> and <c>false</c>
394403
///respectively. Element order is preserved in both of the created lists.</summary>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp