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

Commit9ceff4c

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Array.indexed, List.indexed, Seq.indexed
commit 2397fdaedebf1204838e8542776c4ba2d52f8523Merge: 6ee1d56 d78737bAuthor: latkin <latkin@microsoft.com>Date: Thu Oct 30 16:15:31 2014 -0700 Doc'ing exception for arraycommit d78737b70de1d281d1ab0fad34a9f6f13bb87cc8Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Sun Sep 7 14:37:04 2014 +0100 Implement indexed in Seq, List and Array modules
1 parent8407ad9 commit9ceff4c

File tree

13 files changed

+126
-0
lines changed

13 files changed

+126
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ type ArrayModule2() =
4141

4242
()
4343

44+
[<Test>]
45+
memberthis.Indexed()=
46+
// integer array
47+
letresultInt= Array.indexed[|10..2..20|]
48+
Assert.AreEqual([|(0,10);(1,12);(2,14);(3,16);(4,18);(5,20)|], resultInt)
49+
50+
// string array
51+
letfuncStr(x:int)(y:string)= x+ y.Length
52+
letresultStr= Array.indexed[|"Lists";"Are";"Commonly";"List"|]
53+
Assert.AreEqual([|(0,"Lists");(1,"Are");(2,"Commonly");(3,"List")|], resultStr)
54+
55+
// empty array
56+
letemptyArr:int[]=[||]
57+
letresultEpt= Array.indexed emptyArr
58+
Assert.AreEqual([||], resultEpt)
59+
60+
// null array
61+
letnullArr=null:string[]
62+
CheckThrowsArgumentNullException(fun()-> Array.indexed nullArr|> ignore)
63+
64+
()
65+
4466
[<Test>]
4567
memberthis.Map()=
4668
// integer array

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ type ListModule02() =
176176

177177
()
178178

179+
[<Test>]
180+
memberthis.Indexed()=
181+
// integer List
182+
Assert.AreEqual([(0,10);(1,12);(2,14);(3,16);(4,18);(5,20)], List.indexed[10..2..20])
183+
Assert.AreEqual([(0,10)], List.indexed[10])
184+
185+
// string List
186+
letresultStr= List.indexed["a";"b";"c";"d"]
187+
Assert.AreEqual([(0,"a");(1,"b");(2,"c");(3,"d")], resultStr)
188+
189+
// empty List
190+
letemptyList:string list=[]
191+
letresultEpt= List.indexed emptyList
192+
Assert.AreEqual(List.empty<int*string>, resultEpt)
193+
194+
()
195+
179196
[<Test>]
180197
memberthis.Max()=
181198
// integer List

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,29 @@ type SeqModule2() =
737737
VerifySeqsEqual(seq[3;6;9;12;15;18;21;24;27;30])(testSeqLengths shorterSeq longerSeq)
738738
VerifySeqsEqual(seq[3;6;9;12;15;18;21;24;27;30])(testSeqLengths longerSeq shorterSeq)
739739

740+
[<Test>]
741+
memberthis.Indexed()=
742+
743+
// integer Seq
744+
letresultInt= Seq.indexed{10..2..20}
745+
letexpectedint= seq[(0,10);(1,12);(2,14);(3,16);(4,18);(5,20)]
746+
747+
VerifySeqsEqual expectedint resultInt
748+
749+
// string Seq
750+
letresultStr= Seq.indexed(seq["Lists";"Are";"Commonly";"List"])
751+
letexpectedStr= seq[(0,"Lists");(1,"Are");(2,"Commonly");(3,"List")]
752+
753+
VerifySeqsEqual expectedStr resultStr
754+
755+
// empty Seq
756+
letresultEpt= Seq.indexed Seq.empty
757+
VerifySeqsEqual Seq.empty resultEpt
758+
759+
// null Seq
760+
letnullSeq:seq<'a>=null
761+
CheckThrowsArgumentNullException(fun()-> Seq.indexed nullSeq|> ignore)
762+
740763
()
741764

742765
[<Test>]

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T
117117
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](T[])
118118
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
119119
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
120+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[System.Int32,T][] Indexed[T](T[])
120121
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
121122
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
122123
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
@@ -297,6 +298,7 @@ Microsoft.FSharp.Collections.ListModule: Int32 FindIndexBack[T](Microsoft.FSharp
297298
Microsoft.FSharp.Collections.ListModule: Int32 FindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
298299
Microsoft.FSharp.Collections.ListModule: Int32 GetHashCode()
299300
Microsoft.FSharp.Collections.ListModule: Int32 Length[T](Microsoft.FSharp.Collections.FSharpList`1[T])
301+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[System.Int32,T]] Indexed[T](Microsoft.FSharp.Collections.FSharpList`1[T])
300302
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
301303
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])
302304
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,Microsoft.FSharp.Collections.FSharpList`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
@@ -425,6 +427,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T]
425427
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
426428
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, System.Collections.Generic.IEnumerable`1[T])
427429
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](System.Collections.Generic.IEnumerable`1[T])
430+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.Int32,T]] Indexed[T](System.Collections.Generic.IEnumerable`1[T])
428431
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
429432
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] Zip[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
430433
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Collections.Generic.IEnumerable`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])

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

Lines changed: 3 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] TryLast[T](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[System.Int32,T][] Indexed[T](T[])
114115
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
115116
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
116117
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
@@ -291,6 +292,7 @@ Microsoft.FSharp.Collections.ListModule: Int32 FindIndexBack[T](Microsoft.FSharp
291292
Microsoft.FSharp.Collections.ListModule: Int32 FindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
292293
Microsoft.FSharp.Collections.ListModule: Int32 GetHashCode()
293294
Microsoft.FSharp.Collections.ListModule: Int32 Length[T](Microsoft.FSharp.Collections.FSharpList`1[T])
295+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[System.Int32,T]] Indexed[T](Microsoft.FSharp.Collections.FSharpList`1[T])
294296
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
295297
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])
296298
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,Microsoft.FSharp.Collections.FSharpList`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
@@ -419,6 +421,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T]
419421
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
420422
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, System.Collections.Generic.IEnumerable`1[T])
421423
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](System.Collections.Generic.IEnumerable`1[T])
424+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.Int32,T]] Indexed[T](System.Collections.Generic.IEnumerable`1[T])
422425
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
423426
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] Zip[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
424427
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Collections.Generic.IEnumerable`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ namespace Microsoft.FSharp.Collections
240240
checkNonNull"list" list
241241
Microsoft.FSharp.Primitives.Basics.List.toArray list
242242

243+
[<CompiledName("Indexed")>]
244+
letindexed(array:'T[])=
245+
checkNonNull"array" array
246+
letlen= array.Length
247+
letres= Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
248+
for i=0to len-1do
249+
res.[i]<-(i,array.[i])
250+
res
243251

244252
[<CompiledName("Iterate")>]
245253
let inlineiter f(array:'T[])=

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ namespace Microsoft.FSharp.Collections
384384
[<CompiledName("GroupBy")>]
385385
val groupBy:projection:('T-> 'Key)->array:'T[]->('Key* 'T[])[]when 'Key:equality
386386

387+
///<summary>Builds a new array whose elements are the corresponding elements of the input array
388+
///paired with the integer index(from 0)of each element.</summary>
389+
///<param name="array">The input array.</param>
390+
///<returns>The array of indexed elements.</returns>
391+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
392+
[<CompiledName("Indexed")>]
393+
val indexed:array:'T[]->(int* 'T)[]
394+
387395
///<summary>Creates an array given the dimension and a generator function to compute the elements.</summary>
388396
///<param name="count">The number of elements to initialize.</param>
389397
///<param name="initializer">The function to generate the initial values for each index.</param>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ namespace Microsoft.FSharp.Collections
6464
[<CompiledName("MapIndexed")>]
6565
letmapi f list= Microsoft.FSharp.Primitives.Basics.List.mapi f list
6666

67+
[<CompiledName("Indexed")>]
68+
letindexed list= Microsoft.FSharp.Primitives.Basics.List.indexed list
69+
6770
[<CompiledName("Iterate")>]
6871
letiter f list= Microsoft.FSharp.Primitives.Basics.List.iter f list
6972

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ namespace Microsoft.FSharp.Collections
303303
[<CompiledName("Head")>]
304304
val head:list:'T list-> 'T
305305

306+
///<summary>Returns a new list whose elements are the corresponding elements
307+
///of the input list paired with the index(from 0)of each element.</summary>
308+
///<param name="list">The input list.</param>
309+
///<returns>The list of indexed elements.</returns>
310+
[<CompiledName("Indexed")>]
311+
val indexed:list:'T list->(int* 'T)list
312+
306313
///<summary>Creates a list by calling the given generator on each index.</summary>
307314
///<param name="length">The length of the list to generate.</param>
308315
///<param name="initializer">The function to generate an element from an index.</param>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ module internal List =
131131
cons
132132
|_-> invalidArg"xs2"(SR.GetString(SR.listsHadDifferentLengths))
133133

134+
let recindexedToFreshConsTail cons xs i=
135+
match xswith
136+
|[]->
137+
setFreshConsTail cons[];
138+
|(h::t)->
139+
letcons2= freshConsNoTail(i,h)
140+
setFreshConsTail cons cons2;
141+
indexedToFreshConsTail cons2 t(i+1)
142+
143+
letindexed xs=
144+
match xswith
145+
|[]->[]
146+
|[h]->[(0,h)]
147+
|(h::t)->
148+
letcons= freshConsNoTail(0,h)
149+
indexedToFreshConsTail cons t1
150+
cons
151+
134152
let recforall f xs1=
135153
match xs1with
136154
|[]->true

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp