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

Commitb10392d

Browse files
Rodrigo Vidallatkin
Rodrigo Vidal
authored andcommitted
Implement Array.tryHead, List.tryHead, Seq.tryHead
Commits: add tryHead function to the list module add tryHead function to the array module add tryHead function to the seq module add exception possibility to array tryhead docs optimize Array.tryHead add surface area tests Minor cleanup
1 parent1fc6479 commitb10392d

File tree

11 files changed

+100
-1
lines changed

11 files changed

+100
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,26 @@ type ArrayModule() =
457457
Assert.IsTrue((nullArr=[|null;null;null|]))
458458

459459
()
460+
461+
462+
[<Test>]
463+
memberthis.TryHead()=
464+
// integer array
465+
letresultInt= Array.tryHead[|2..2..20|]
466+
Assert.AreEqual(2, resultInt.Value)
467+
468+
// string array
469+
letresultStr= Array.tryHead[|"a";"b";"c";"d"|]
470+
Assert.AreEqual("a", resultStr.Value)
471+
472+
// empty array
473+
letresultNone= Array.tryHead[||]
474+
Assert.AreEqual(None, resultNone)
475+
476+
// null array
477+
letnullArr=null:string[]
478+
CheckThrowsArgumentNullException(fun()-> Array.tryHead nullArr|> ignore)
479+
()
460480

461481
[<Test>]
462482
memberthis.Exists()=

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,19 @@ type ListModule() =
601601
[<Test>]
602602
memberthis.``exactlyOne should fail on lists with more than one element``()=
603603
CheckThrowsArgumentException(fun()-> List.exactlyOne["1";"2"]|> ignore)
604+
605+
[<Test>]
606+
memberthis.TryHead()=
607+
// integer List
608+
letresultInt= List.tryHead[2..2..20]
609+
Assert.AreEqual(2, resultInt.Value)
610+
611+
// string List
612+
letresultStr= List.tryHead["a";"b";"c";"d"]
613+
Assert.AreEqual("a", resultStr.Value)
614+
615+
letresultNone= List.tryHead[]
616+
Assert.AreEqual(None, resultNone)
604617

605618
[<Test>]
606619
memberthis.last()=

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ type SeqModule2() =
3939
letnullSeq:seq<'a>=null
4040
CheckThrowsArgumentNullException(fun()->Seq.head nullSeq)
4141
()
42+
43+
[<Test>]
44+
memberthis.TryHead()=
45+
// int Seq
46+
letIntSeq=
47+
seq{for iin0..9-> i}
48+
49+
letintResult= Seq.tryHead IntSeq
50+
51+
// string Seq
52+
letstrResult= Seq.tryHead(seq["first";"second";"third"])
53+
Assert.AreEqual("first", strResult.Value)
54+
55+
// Empty Seq
56+
letemptyResult= Seq.tryHead Seq.empty
57+
Assert.AreEqual(None, emptyResult)
58+
59+
// null Seq
60+
letnullSeq:seq<'a>=null
61+
CheckThrowsArgumentNullException(fun()->Seq.head nullSeq)
62+
()
4263

4364

4465
[<Test>]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Collections.FSharpLis
109109
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
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[])
112+
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](T[])
112113
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
113114
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
114115
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
@@ -314,6 +315,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
314315
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
315316
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])
316317
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])
318+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](Microsoft.FSharp.Collections.FSharpList`1[T])
317319
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
318320
Microsoft.FSharp.Collections.ListModule: System.String ToString()
319321
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]])
@@ -388,9 +390,9 @@ Microsoft.FSharp.Collections.SeqModule: Int32 GetHashCode()
388390
Microsoft.FSharp.Collections.SeqModule: Int32 Length[T](System.Collections.Generic.IEnumerable`1[T])
389391
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Collections.FSharpList`1[T] ToList[T](System.Collections.Generic.IEnumerable`1[T])
390392
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
391-
392393
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])
393394
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])
395+
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
394396
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
395397
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])
396398
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
@@ -103,6 +103,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Collections.FSharpLis
103103
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
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[])
106+
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](T[])
106107
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
107108
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
108109
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
@@ -308,6 +309,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
308309
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
309310
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])
310311
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])
312+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](Microsoft.FSharp.Collections.FSharpList`1[T])
311313
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
312314
Microsoft.FSharp.Collections.ListModule: System.String ToString()
313315
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]])
@@ -384,6 +386,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Collections.FSharpList`
384386
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
385387
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])
386388
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])
389+
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
387390
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
388391
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])
389392
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ namespace Microsoft.FSharp.Collections
5252
array.[i]<- x
5353
array
5454

55+
[<CompiledName("TryHead")>]
56+
lettryHead(array:'T[])=
57+
checkNonNull"array" array
58+
if array.Length=0then None
59+
else Some array.[0]
5560

5661
[<CompiledName("IsEmpty")>]
5762
letisEmpty(array:'T[])=

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ namespace Microsoft.FSharp.Collections
100100
///<returns>The created array.</returns>
101101
[<CompiledName("Create")>]
102102
val create:count:int->value:'T-> 'T[]
103+
104+
///<summary>Returns the first element of the array,or
105+
///<c>None</c> if the array is empty.</summary>
106+
///<param name="array">The input array.</param>
107+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
108+
///<returns>The first element of the array or None.</returns>
109+
[<CompiledName("TryHead")>]
110+
val tryHead:array:'T[]-> 'T option
103111

104112
///<summary>Applies the given function to successive elements,returning the first
105113
///result where function returns <c>Some(x)</c> for some <c>x</c>. If the function

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ namespace Microsoft.FSharp.Collections
5959
[<CompiledName("Head")>]
6060
lethead list=match listwith(x::_)-> x|[]-> invalidArg"list"(SR.GetString(SR.inputListWasEmpty))
6161

62+
[<CompiledName("TryHead")>]
63+
lettryHead list=match listwith(x::_)-> Some x|[]-> None
64+
6265
[<CompiledName("Tail")>]
6366
lettail list=match listwith(_:: t)-> t|[]-> invalidArg"list"(SR.GetString(SR.inputListWasEmpty))
6467

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,13 @@ namespace Microsoft.FSharp.Collections
619619
[<CompiledName("ToSeq")>]
620620
val toSeq:list:'T list->seq<'T>
621621

622+
///<summary>Returns the first element of the list,or
623+
///<c>None</c> if the list is empty.</summary>
624+
///<param name="list">The input list.</param>
625+
///<returns>The first element of the list or None.</returns>
626+
[<CompiledName("TryHead")>]
627+
val tryHead:list:'T list-> 'T option
628+
622629
///<summary>Applies the given function to successive elements,returning <c>Some(x)</c> the first
623630
///result where function returns <c>Some(x)</c> for some x. If no such element
624631
///exists then return <c>None</c>.</summary>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,13 @@ namespace Microsoft.FSharp.Collections
15471547
if(e.MoveNext())then e.Current
15481548
else invalidArg"source" InputSequenceEmptyString
15491549

1550+
[<CompiledName("TryHead")>]
1551+
lettryHead(source:seq<_>)=
1552+
checkNonNull"source" source
1553+
use e= source.GetEnumerator()
1554+
if(e.MoveNext())then Some e.Current
1555+
else None
1556+
15501557
[<CompiledName("Last")>]
15511558
letlast(source:seq<_>)=
15521559
checkNonNull"source" source

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp