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

Commit8407ad9

Browse files
simonhdicksonlatkin
authored andcommitted
Implement Array.tryLast, List.tryLast, Seq.tryLast
commit ff0d1ea2277d37d076abb389a29a80a131f0422fAuthor: latkin <latkin@microsoft.com>Date: Thu Oct 30 17:58:43 2014 -0700 Changing to Assert.IsTrue from Assert.True, due to older NUnit version used internallycommit 82dfc564304e15b3b354715a120e786138b0d32cAuthor: latkin <latkin@microsoft.com>Date: Thu Oct 30 15:48:47 2014 -0700 Update portable surface areacommit 0e00446c7e90667543d68552506c717c4ac919d9Merge:83106bf 5915b4bAuthor: latkin <latkin@microsoft.com>Date: Thu Oct 30 15:46:58 2014 -0700 Merge branch 'trylast' ofhttps://git01.codeplex.com/forks/simonhdickson/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule.fs src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fscommit 5915b4b601f5d76f16be08c79e14ce01272a13b8Author: Simon Dickson <simonhdickson@gmail.com>Date: Thu Oct 23 22:09:46 2014 +0100 Corrections from reviewcommit 6d504f10e67a0645898a1094f88a380c218a9ea1Author: Simon Dickson <simonhdickson@gmail.com>Date: Fri Aug 8 18:04:31 2014 +0100 Add tryLast function to the Array modulecommit 26a0f48f553cbd6829f5ab4761ee5c6778f527d8Author: Simon Dickson <simonhdickson@gmail.com>Date: Fri Aug 8 17:43:04 2014 +0100 Fix surfacearea testcommit e67048950f21cc17b71fea44fc1effd6a19fb68eAuthor: Simon Dickson <simonhdickson@gmail.com>Date: Fri Aug 8 17:37:48 2014 +0100 Simplify Seq tryHead testscommit 5162da9df1f033f2af36aaf13ae7b2f4335d0b9dAuthor: Simon Dickson <simonhdickson@gmail.com>Date: Fri Aug 8 17:37:12 2014 +0100 Add tryLast function to the List modulecommit 7dea0dabf963f2527cba50008f6a7666d0bbad2cAuthor: Simon Dickson <simonhdickson@gmail.com>Date: Fri Aug 8 17:01:13 2014 +0100 Fixing testscommit 72a274e8c04e3ac81088259f0964f6c7532b0828Author: Simon Dickson <simonhdickson@gmail.com>Date: Fri Aug 8 16:15:06 2014 +0100 Add tryLast function to the Seq module
1 parent83106bf commit8407ad9

File tree

11 files changed

+114
-2
lines changed

11 files changed

+114
-2
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,27 @@ type ArrayModule() =
776776
Assert.AreEqual(1, Array.last[|1|])
777777
Assert.AreEqual("2", Array.last[|"1";"3";"2"|])
778778
Assert.AreEqual(["4"], Array.last[|["1";"3"];[];["4"]|])
779-
779+
780+
[<Test>]
781+
memberthis.TryLast()=
782+
// integers array
783+
letIntSeq=[|1..9|]
784+
letintResult= Array.tryLast IntSeq
785+
Assert.AreEqual(9, intResult.Value)
786+
787+
// string array
788+
letstrResult= Array.tryLast[|"first";"second";"third"|]
789+
Assert.AreEqual("third", strResult.Value)
790+
791+
// Empty array
792+
letemptyResult= Array.tryLast Array.empty
793+
Assert.IsTrue(emptyResult.IsNone)
794+
795+
// null array
796+
letnullArr=null:string[]
797+
CheckThrowsArgumentNullException(fun()->Array.tryLast nullArr|> ignore)
798+
()
799+
780800
[<Test>]
781801
memberthis.ToSeq()=
782802
letintArr=[|1..10|]

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,22 @@ type ListModule() =
721721

722722
letresultNone= List.tryHead[]
723723
Assert.AreEqual(None, resultNone)
724-
724+
725+
[<Test>]
726+
memberthis.TryLast()=
727+
// integer List
728+
letintResult= List.tryLast[1..9]
729+
Assert.AreEqual(9, intResult.Value)
730+
731+
// string List
732+
letstrResult= List.tryLast(["first";"second";"third"])
733+
Assert.AreEqual("third", strResult.Value)
734+
735+
// Empty List
736+
letemptyResult= List.tryLast List.empty
737+
Assert.IsTrue(emptyResult.IsNone)
738+
()
739+
725740
[<Test>]
726741
memberthis.last()=
727742
// last should fail on empty list

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ type SeqModule2() =
101101
letnullSeq:seq<'a>=null
102102
CheckThrowsArgumentNullException(fun()->Seq.last nullSeq)
103103
()
104+
105+
[<Test>]
106+
memberthis.TryLast()=
107+
108+
letIntSeq=
109+
seq{for iin0..9-> i}
110+
111+
letintResult= Seq.tryLast IntSeq
112+
Assert.AreEqual(9, intResult.Value)
113+
114+
// string Seq
115+
letstrResult= Seq.tryLast(seq["first";"second";"third"])
116+
Assert.AreEqual("third", strResult.Value)
117+
118+
// Empty Seq
119+
letemptyResult= Seq.tryLast Seq.empty
120+
Assert.IsTrue(emptyResult.IsNone)
121+
122+
// null Seq
123+
letnullSeq:seq<'a>=null
124+
CheckThrowsArgumentNullException(fun()->Seq.tryLast nullSeq|> ignore)
125+
()
104126

105127
[<Test>]
106128
memberthis.ExactlyOne()=

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T
114114
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
115115
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](T[])
116116
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, T[])
117+
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](T[])
117118
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
118119
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
119120
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
@@ -339,6 +340,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T]
339340
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])
340341
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](Microsoft.FSharp.Collections.FSharpList`1[T])
341342
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
343+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](Microsoft.FSharp.Collections.FSharpList`1[T])
342344
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
343345
Microsoft.FSharp.Collections.ListModule: System.String ToString()
344346
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]])
@@ -422,6 +424,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T]
422424
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])
423425
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
424426
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, System.Collections.Generic.IEnumerable`1[T])
427+
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](System.Collections.Generic.IEnumerable`1[T])
425428
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
426429
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])
427430
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
@@ -108,6 +108,7 @@ Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T
108108
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryFind[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
109109
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](T[])
110110
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, T[])
111+
Microsoft.FSharp.Collections.ArrayModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](T[])
111112
Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](T[])
112113
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
113114
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
@@ -333,6 +334,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T]
333334
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])
334335
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](Microsoft.FSharp.Collections.FSharpList`1[T])
335336
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
337+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](Microsoft.FSharp.Collections.FSharpList`1[T])
336338
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
337339
Microsoft.FSharp.Collections.ListModule: System.String ToString()
338340
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]])
@@ -416,6 +418,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T]
416418
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])
417419
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryHead[T](System.Collections.Generic.IEnumerable`1[T])
418420
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryItem[T](Int32, System.Collections.Generic.IEnumerable`1[T])
421+
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](System.Collections.Generic.IEnumerable`1[T])
419422
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
420423
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])
421424
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
@@ -39,6 +39,12 @@ namespace Microsoft.FSharp.Collections
3939
if array.Length=0then invalidArg"array" InputArrayEmptyString
4040
array.[array.Length-1]
4141

42+
[<CompiledName("TryLast")>]
43+
lettryLast(array:'T[])=
44+
checkNonNull"array" array
45+
if array.Length=0then None
46+
else Some array.[array.Length-1]
47+
4248
[<CompiledName("Initialize")>]
4349
let inlineinit count f= Microsoft.FSharp.Primitives.Basics.Array.init count f
4450

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,14 @@ namespace Microsoft.FSharp.Collections
458458
///<returns>The length of the array.</returns>
459459
[<CompiledName("Length")>]
460460
val length:array:'T[]->int
461+
462+
///<summary>Returns the last element of the array.
463+
///Return <c>None</c> if no such element exists.</summary>
464+
///<param name="array">The input array.</param>
465+
///<returns>The last element of the array or None.</returns>
466+
///<exception cref="System.ArgumentNullException">Thrown when the input sequence is null.</exception>
467+
[<CompiledName("TryLast")>]
468+
val tryLast:array:'T[]-> 'T option
461469

462470
///<summary>Builds a new array whose elements are the results of applying the given function
463471
///to each of the elements of the array.</summary>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ namespace Microsoft.FSharp.Collections
2828
|_:: tail-> last tail
2929
|[]-> invalidArg"list"(SR.GetString(SR.inputListWasEmpty))
3030

31+
[<CompiledName("TryLast")>]
32+
let rectryLast(list:'T list)=
33+
match listwith
34+
|[x]-> Some x
35+
|_:: tail-> tryLast tail
36+
|[]-> None
37+
3138
[<CompiledName("Reverse")>]
3239
letrev list= Microsoft.FSharp.Primitives.Basics.List.rev list
3340

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ namespace Microsoft.FSharp.Collections
367367
[<CompiledName("Length")>]
368368
val length:list:'T list->int
369369

370+
///<summary>Returns the last element of the list.
371+
///Return <c>None</c> if no such element exists.</summary>
372+
///<param name="list">The input list.</param>
373+
///<returns>The last element of the list or None.</returns>
374+
[<CompiledName("TryLast")>]
375+
val tryLast:list:'T list-> 'T option
376+
370377
///<summary>Builds a new collection whose elements are the results of applying the given function
371378
///to each of the elements of the collection.</summary>
372379
///<param name="mapping">The function to transform elements from the input list.</param>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,16 @@ namespace Microsoft.FSharp.Collections
17341734
else
17351735
invalidArg"source" InputSequenceEmptyString
17361736

1737+
[<CompiledName("TryLast")>]
1738+
lettryLast(source:seq<_>)=
1739+
checkNonNull"source" source
1740+
use e= source.GetEnumerator()
1741+
if e.MoveNext()then
1742+
let mutableres= e.Current
1743+
while(e.MoveNext())do res<- e.Current
1744+
Some res
1745+
else
1746+
None
17371747

17381748
[<CompiledName("ExactlyOne")>]
17391749
letexactlyOne(source:seq<_>)=

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp