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

Commit959cbdc

Browse files
mexxlatkin
authored andcommitted
Implement Array.tryItem, List.tryItem, Seq.tryItem
commit a244af4e93e403a46bc23f1e665063213585bc86Author: latkin <latkin@microsoft.com>Date: Wed Oct 15 16:04:17 2014 -0700 Do index check for Seq before calling GetEnumerator()commit e34c04aaaa40ae3328321188797f9c0c285b7eeaMerge: 7b5cc80 371ba03Author: latkin <latkin@microsoft.com>Date: Wed Oct 15 15:55:21 2014 -0700 Merge branch 'tryItem' ofhttps://git01.codeplex.com/forks/mexx24/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs src/fsharp/FSharp.Core/array.fs src/fsharp/FSharp.Core/list.fs src/fsharp/FSharp.Core/seq.fscommit 371ba03294c6489fdbba245c684ea3ec9221559fAuthor: Max Malook <community@malook.de>Date: Fri Jul 11 22:55:59 2014 +0200 add Array.tryItem functioncommit 61a1d7a1f7fdc27c7fe48e82c34e054bba4481b4Author: Max Malook <community@malook.de>Date: Fri Jul 11 22:54:25 2014 +0200 add List.tryItem functioncommit f090b728272bf6d75063549d65650369dcf8e15bAuthor: Max Malook <community@malook.de>Date: Fri Jul 11 21:54:01 2014 +0200 add Seq.tryItem function
1 parentf028ee4 commit959cbdc

File tree

11 files changed

+136
-1
lines changed

11 files changed

+136
-1
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,32 @@ type ArrayModule2() =
866866
// Out of range
867867
for i=11to20do
868868
CheckThrowsIndexOutRangException(fun()-> Array.item i[|1..8|]|> ignore)
869+
870+
[<Test>]
871+
memberthis.tryItem()=
872+
// integer array
873+
letintArr=[|3;4;7;8;10|]
874+
letresultInt= Array.tryItem3 intArr
875+
Assert.AreEqual(Some(8), resultInt)
876+
877+
// string array
878+
letstrArr=[|"Lists";"are";"commonly";"list"|]
879+
letresultStr= Array.tryItem1 strArr
880+
Assert.AreEqual(Some("are"), resultStr)
881+
882+
// empty array
883+
letemptyArr:int[]=[||]
884+
letresultEmpty= Array.tryItem1 emptyArr
885+
Assert.AreEqual(None, resultEmpty)
886+
887+
// null array
888+
letnullArr=null:string[]
889+
CheckThrowsArgumentNullException(fun()-> Array.tryItem0 nullArr|> ignore)
890+
891+
// Negative index
892+
letresultNegativeIndex= Array.tryItem-1[|3;1;6;2|]
893+
Assert.AreEqual(None, resultNegativeIndex)
894+
895+
// Index greater than length
896+
letresultIndexGreater= Array.tryItem14[|3;1;6;2|]
897+
Assert.AreEqual(None, resultIndexGreater)

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,4 +699,26 @@ type ListModule02() =
699699
letempTriple:(obj*obj*obj)list=[]
700700
Assert.AreEqual(empTriple, resultEpt)
701701

702-
()
702+
()
703+
704+
[<Test>]
705+
memberthis.tryItem()=
706+
// integer List
707+
letresultInt= List.tryItem4[3;7;9;4;8;1;1;2]
708+
Assert.AreEqual(Some(8), resultInt)
709+
710+
// string List
711+
letresultStr= List.tryItem2["a";"b";"c";"d"]
712+
Assert.AreEqual(Some("c"), resultStr)
713+
714+
// empty List
715+
letresultEmpty= List.tryItem0 List.empty
716+
Assert.AreEqual(None, resultEmpty)
717+
718+
// Negative index
719+
letresultNegativeIndex= List.tryItem-1[3;1;6;2]
720+
Assert.AreEqual(None, resultNegativeIndex)
721+
722+
// Index equals to length
723+
letresultIndexGreater= List.tryItem4[3;1;6;2]
724+
Assert.AreEqual(None, resultIndexGreater)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,3 +1452,29 @@ type SeqModule2() =
14521452
CheckThrowsArgumentNullException(fun()-> Seq.tryPick funcNull nullSeq|> ignore)
14531453

14541454
()
1455+
1456+
[<Test>]
1457+
memberthis.tryItem()=
1458+
// integer Seq
1459+
letresultInt= Seq.tryItem3{10..20}
1460+
Assert.AreEqual(Some(13), resultInt)
1461+
1462+
// string Seq
1463+
letresultStr= Seq.tryItem2(seq["Lists";"Are";"Cool";"List"])
1464+
Assert.AreEqual(Some("Cool"), resultStr)
1465+
1466+
// empty Seq
1467+
letresultEmpty= Seq.tryItem0 Seq.empty
1468+
Assert.AreEqual(None, resultEmpty)
1469+
1470+
// null Seq
1471+
letnullSeq:seq<'a>=null
1472+
CheckThrowsArgumentNullException(fun()-> Seq.tryItem3 nullSeq|> ignore)
1473+
1474+
// Negative index
1475+
letresultNegativeIndex= Seq.tryItem-1{10..20}
1476+
Assert.AreEqual(None, resultNegativeIndex)
1477+
1478+
// Index greater than length
1479+
letresultIndexGreater= Seq.tryItem31{10..20}
1480+
Assert.AreEqual(None, resultIndexGreater)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[S
110110
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], 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: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](T[])
113+
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, T[])
113114
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
114115
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
115116
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
@@ -322,6 +323,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[Sy
322323
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Collections.FSharpList`1[T])
323324
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
324325
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](Microsoft.FSharp.Collections.FSharpList`1[T])
326+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
325327
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
326328
Microsoft.FSharp.Collections.ListModule: System.String ToString()
327329
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T1],Microsoft.FSharp.Collections.FSharpList`1[T2]] Unzip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]])
@@ -400,6 +402,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[Sys
400402
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], System.Collections.Generic.IEnumerable`1[T])
401403
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
402404
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
405+
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, System.Collections.Generic.IEnumerable`1[T])
403406
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
404407
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])
405408
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
@@ -104,6 +104,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[S
104104
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], 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: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](T[])
107+
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, T[])
107108
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
108109
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
109110
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
@@ -316,6 +317,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[Sy
316317
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Collections.FSharpList`1[T])
317318
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
318319
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](Microsoft.FSharp.Collections.FSharpList`1[T])
320+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
319321
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
320322
Microsoft.FSharp.Collections.ListModule: System.String ToString()
321323
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T1],Microsoft.FSharp.Collections.FSharpList`1[T2]] Unzip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]])
@@ -394,6 +396,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[Sys
394396
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], System.Collections.Generic.IEnumerable`1[T])
395397
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
396398
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
399+
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, System.Collections.Generic.IEnumerable`1[T])
397400
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
398401
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])
399402
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,12 @@ namespace Microsoft.FSharp.Collections
826826
letitem n(array:_[])=
827827
array.[n]
828828

829+
[<CompiledName("TryItem")>]
830+
lettryItem index(array:'T[])=
831+
checkNonNull"array" array
832+
if index<0|| index>= array.Lengththen None
833+
else Some(array.[index])
834+
829835
[<CompiledName("Get")>]
830836
letget(array:_[])n=
831837
array.[n]

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,15 @@ namespace Microsoft.FSharp.Collections
775775
[<CompiledName("TryFindIndex")>]
776776
val tryFindIndex:predicate:('T->bool)->array:'T[]->int option
777777

778+
///<summary>Tries to find the nth element in the array.
779+
///Returns <c>None</c> if index is negative or the input array does not contain enough elements.</summary>
780+
///<param name="index">The index of element to retrieve.</param>
781+
///<param name="source">The input array.</param>
782+
///<returns>The nth element of the array or <c>None</c>.</returns>
783+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
784+
[<CompiledName("TryItem")>]
785+
val tryItem:index:int->array:'T[]-> 'T option
786+
778787
///<summary>Splits an array of pairs into two arrays.</summary>
779788
///<param name="array">The input array.</param>
780789
///<returns>The two arrays.</returns>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ namespace Microsoft.FSharp.Collections
9696
|_->
9797
invalidArg"index"(SR.GetString(SR.indexOutOfBounds))
9898

99+
[<CompiledName("TryItem")>]
100+
let rectryItem index list=
101+
match listwith
102+
| h::twhen index>=0->
103+
if index=0then Some helse tryItem(index-1) t
104+
|_->
105+
None
106+
99107
[<CompiledName("Get")>]
100108
letnth list index= item index list
101109

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,14 @@ namespace Microsoft.FSharp.Collections
675675
[<CompiledName("TryFindIndex")>]
676676
val tryFindIndex:predicate:('T->bool)->list:'T list->int option
677677

678+
///<summary>Tries to find the nth element in the list.
679+
///Returns <c>None</c> if index is negative or the list does not contain enough elements.</summary>
680+
///<param name="index">The index to retrieve.</param>
681+
///<param name="list">The input list.</param>
682+
///<returns>The value at the given index or <c>None</c>.</returns>
683+
[<CompiledName("TryItem")>]
684+
val tryItem:index:int->list:'T list-> 'T option
685+
678686
///<summary>Splits a list of pairs into two lists.</summary>
679687
///<param name="list">The input list.</param>
680688
///<returns>Two lists of split elements.</returns>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ namespace Microsoft.FSharp.Collections
5858

5959
letEmpty<'T>()=(new EmptyEnumerator<'T>():> IEnumerator<'T>)
6060

61+
let rectryItem index(e:IEnumerator<'T>)=
62+
ifnot(e.MoveNext())then None
63+
elif index=0then Some(e.Current)
64+
else tryItem(index-1) e
65+
6166
let recnth index(e:IEnumerator<'T>)=
6267
ifnot(e.MoveNext())then invalidArg"index"(SR.GetString(SR.notEnoughElements))
6368
if index=0then e.Current
@@ -903,6 +908,13 @@ namespace Microsoft.FSharp.Collections
903908
use e= source.GetEnumerator()
904909
IEnumerator.nth i e
905910

911+
[<CompiledName("TryItem")>]
912+
lettryItem i(source:seq<'T>)=
913+
checkNonNull"source" source
914+
if i<0then Noneelse
915+
use e= source.GetEnumerator()
916+
IEnumerator.tryItem i e
917+
906918
[<CompiledName("Get")>]
907919
letnth i(source:seq<'T>)= item i source
908920

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp