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

Commit70146cf

Browse files
forkilatkin
authored andcommitted
Implement Array.last, List.last
Commits: implementing "last" for array and list Show possible System.ArgumentNullException in docs for Array.last Use direct array lookup for Array.last use only one test method for "last" Align "last" docs with the rest Using tail rec version for List.last Adding surface area for "last"
1 parentb9541ad commit70146cf

File tree

9 files changed

+56
-4
lines changed

9 files changed

+56
-4
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,19 @@ type ArrayModule() =
569569

570570
// make it not found
571571
CheckThrowsKeyNotFoundException(fun()-> Array.pick(fun n-> None) intArr|> ignore)
572+
573+
[<Test>]
574+
memberthis.last()=
575+
// last should fail on empty array
576+
CheckThrowsArgumentException(fun()-> Array.last[||]|> ignore)
577+
578+
// last should fail on null
579+
CheckThrowsArgumentNullException(fun()-> Array.lastnull|> ignore)
580+
581+
// last should return the last element from arrays
582+
Assert.AreEqual(1, Array.last[|1|])
583+
Assert.AreEqual("2", Array.last[|"1";"3";"2"|])
584+
Assert.AreEqual(["4"], Array.last[|["1";"3"];[];["4"]|])
572585

573586
[<Test>]
574587
memberthis.ToSeq()=

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,16 @@ type ListModule() =
523523
memberthis.``exactlyOne should fail on lists with more than one element``()=
524524
CheckThrowsArgumentException(fun()-> List.exactlyOne["1";"2"]|> ignore)
525525

526+
[<Test>]
527+
memberthis.last()=
528+
// last should fail on empty list
529+
CheckThrowsArgumentException(fun()-> List.last[]|> ignore)
530+
531+
// last should return the last element from lists
532+
Assert.AreEqual(1, List.last[1])
533+
Assert.AreEqual("2", List.last["1";"3";"2"])
534+
Assert.AreEqual(["4"], List.last[["1";"3"];[];["4"]])
535+
526536
[<Test>]
527537
memberthis.Init()=
528538
// integer List
@@ -703,4 +713,4 @@ type ListModule() =
703713
Assert.AreEqual([],List.pairwise[1])
704714
Assert.AreEqual([1,2],List.pairwise[1;2])
705715
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"])
716+
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
@@ -122,6 +122,7 @@ Microsoft.FSharp.Collections.ArrayModule: T Average[T](T[])
122122
Microsoft.FSharp.Collections.ArrayModule: T ExactlyOne[T](T[])
123123
Microsoft.FSharp.Collections.ArrayModule: T Find[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
124124
Microsoft.FSharp.Collections.ArrayModule: T Get[T](T[], Int32)
125+
Microsoft.FSharp.Collections.ArrayModule: T Last[T](T[])
125126
Microsoft.FSharp.Collections.ArrayModule: T MaxBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], T[])
126127
Microsoft.FSharp.Collections.ArrayModule: T Max[T](T[])
127128
Microsoft.FSharp.Collections.ArrayModule: T MinBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], T[])
@@ -315,6 +316,7 @@ Microsoft.FSharp.Collections.ListModule: T ExactlyOne[T](Microsoft.FSharp.Collec
315316
Microsoft.FSharp.Collections.ListModule: T Find[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
316317
Microsoft.FSharp.Collections.ListModule: T Get[T](Microsoft.FSharp.Collections.FSharpList`1[T], Int32)
317318
Microsoft.FSharp.Collections.ListModule: T Head[T](Microsoft.FSharp.Collections.FSharpList`1[T])
319+
Microsoft.FSharp.Collections.ListModule: T Last[T](Microsoft.FSharp.Collections.FSharpList`1[T])
318320
Microsoft.FSharp.Collections.ListModule: T MaxBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Collections.FSharpList`1[T])
319321
Microsoft.FSharp.Collections.ListModule: T Max[T](Microsoft.FSharp.Collections.FSharpList`1[T])
320322
Microsoft.FSharp.Collections.ListModule: T MinBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,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
@@ -116,6 +116,7 @@ Microsoft.FSharp.Collections.ArrayModule: T Average[T](T[])
116116
Microsoft.FSharp.Collections.ArrayModule: T ExactlyOne[T](T[])
117117
Microsoft.FSharp.Collections.ArrayModule: T Find[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
118118
Microsoft.FSharp.Collections.ArrayModule: T Get[T](T[], Int32)
119+
Microsoft.FSharp.Collections.ArrayModule: T Last[T](T[])
119120
Microsoft.FSharp.Collections.ArrayModule: T MaxBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], T[])
120121
Microsoft.FSharp.Collections.ArrayModule: T Max[T](T[])
121122
Microsoft.FSharp.Collections.ArrayModule: T MinBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], T[])
@@ -309,6 +310,7 @@ Microsoft.FSharp.Collections.ListModule: T ExactlyOne[T](Microsoft.FSharp.Collec
309310
Microsoft.FSharp.Collections.ListModule: T Find[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
310311
Microsoft.FSharp.Collections.ListModule: T Get[T](Microsoft.FSharp.Collections.FSharpList`1[T], Int32)
311312
Microsoft.FSharp.Collections.ListModule: T Head[T](Microsoft.FSharp.Collections.FSharpList`1[T])
313+
Microsoft.FSharp.Collections.ListModule: T Last[T](Microsoft.FSharp.Collections.FSharpList`1[T])
312314
Microsoft.FSharp.Collections.ListModule: T MaxBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Collections.FSharpList`1[T])
313315
Microsoft.FSharp.Collections.ListModule: T Max[T](Microsoft.FSharp.Collections.FSharpList`1[T])
314316
Microsoft.FSharp.Collections.ListModule: T MinBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,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
@@ -29,6 +29,12 @@ namespace Microsoft.FSharp.Collections
2929

3030
[<CompiledName("Length")>]
3131
letlength(array:_[])= array.Length
32+
33+
[<CompiledName("Last")>]
34+
let inlinelast(array:'T[])=
35+
checkNonNull"array" array
36+
if array.Length=0then invalidArg"array" InputArrayEmptyString
37+
array.[array.Length-1]
3238

3339
[<CompiledName("Initialize")>]
3440
let inlineinit count f= Microsoft.FSharp.Primitives.Basics.Array.init count f

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ namespace Microsoft.FSharp.Collections
341341
[<CompiledName("IterateIndexed2")>]
342342
val iteri2:action:(int-> 'T1-> 'T2->unit)->array1:'T1[]->array2:'T2[]->unit
343343

344+
///<summary>Returns the last element of the array.</summary>
345+
///<param name="array">The input array.</param>
346+
///<returns>The last element of the array.</returns>
347+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
348+
///<exception cref="System.ArgumentException">Thrown when the input does not have any elements.</exception>
349+
[<CompiledName("Last")>]
350+
val inline last:array:'T[]-> 'T
351+
344352
///<summary>Returns the length of an array. You can also use property arr.Length.</summary>
345353
///<param name="array">The input array.</param>
346354
///<returns>The length of the array.</returns>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ namespace Microsoft.FSharp.Collections
1818

1919
[<CompiledName("Length")>]
2020
letlength(list:'T list)= list.Length
21+
22+
[<CompiledName("Last")>]
23+
let reclast(list:'T list)=
24+
match listwith
25+
|[x]-> x
26+
|_:: tail-> last tail
27+
|[]-> invalidArg"list"(SR.GetString(SR.inputListWasEmpty))
2128

2229
[<CompiledName("Reverse")>]
2330
letrev list= Microsoft.FSharp.Primitives.Basics.List.rev list

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ namespace Microsoft.FSharp.Collections
282282
[<CompiledName("IterateIndexed2")>]
283283
val iteri2:action:(int-> 'T1-> 'T2->unit)->list1:'T1 list->list2:'T2 list->unit
284284

285+
///<summary>Returns the last element of the list.</summary>
286+
///<param name="list">The input list.</param>
287+
///<returns>The last element of the list.</returns>
288+
///<exception cref="System.ArgumentException">Thrown when the input does not have any elements.</exception>
289+
[<CompiledName("Last")>]
290+
val last:list:'T list-> 'T
291+
285292
///<summary>Returns the length of the list.</summary>
286293
///<param name="list">The input list.</param>
287294
///<returns>The length of the list.</returns>

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,8 @@ namespace Microsoft.FSharp.Collections
408408
val head:source:seq<'T>-> 'T
409409

410410
///<summary>Returns the last element of the sequence.</summary>
411-
///
412411
///<param name="source">The input sequence.</param>
413-
///
414412
///<returns>The last element of the sequence.</returns>
415-
///
416413
///<exception cref="System.ArgumentNullException">Thrown when the input sequence is null.</exception>
417414
///<exception cref="System.ArgumentException">Thrown when the input does not have any elements.</exception>
418415
[<CompiledName("Last")>]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp