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

Commit4b52be7

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement findBack/findIndexBack/tryFindBack/tryFindIndexBack for Array, List, Seq
commit e24d01f6105eb0b8853f62e2a214303af5c02ad6Author: latkin <latkin@microsoft.com>Date: Wed Oct 22 15:03:34 2014 -0700 Adding ArgumentNull doc comments to Arraycommit 3775111bc1f37f8931e407de2c9b6381d9e7963dMerge:2d72eea 2b3da49Author: latkin <latkin@microsoft.com>Date: Wed Oct 22 14:20:40 2014 -0700 Merge branch 'findBack' ofhttps://git01.codeplex.com/forks/patrickmcdonald/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule.fs src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule.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/array.fsi src/fsharp/FSharp.Core/list.fsi src/fsharp/FSharp.Core/seq.fsicommit 2b3da49a2da69db65359576418b78e0cfebc12eaAuthor: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 16 15:14:12 2014 +0100 Further tweaking of findBack doc commentscommit a85640087188fac4fcc5a052ec52afb2f952ead3Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Mon Sep 15 19:54:46 2014 +0100 Fix *find*Back doc commentscommit 4ebcc8b9a3cc3f87e64bceb580f04088bc44c947Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Sun Sep 7 00:11:38 2014 +0100 Updates to findback, tryFindBack, findIndexBack and tryFindIndexBack after review of pull request.commit a77382e82574a344c268b05eab1b4fb74dbda93dAuthor: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 2 23:57:46 2014 +0100 Implement findBack, tryFindBack, findIndexBack, and tryFindIndexBack in modules Seq, List and Array
1 parent2d72eea commit4b52be7

File tree

16 files changed

+559
-17
lines changed

16 files changed

+559
-17
lines changed

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,14 +661,43 @@ type ArrayModule() =
661661

662662
// empty array
663663
letemptyArr:int[]=[||]
664-
CheckThrowsKeyNotFoundException(fun()-> Array.find(fun x->true) emptyArr|> ignore)
664+
CheckThrowsKeyNotFoundException(fun()-> Array.find(fun _->true) emptyArr|> ignore)
665+
666+
// not found
667+
CheckThrowsKeyNotFoundException(fun()-> Array.find(fun _->false) intArr|> ignore)
665668

666669
// null array
667670
letnullArr=null:string[]
668671
CheckThrowsArgumentNullException(fun()-> Array.find funcStr nullArr|> ignore)
669672

670673
()
671674

675+
[<Test>]
676+
memberthis.FindBack()=
677+
// integer array
678+
letfuncInt x=if(x%5=0)thentrueelsefalse
679+
Assert.AreEqual(20, Array.findBack funcInt[|1..20|])
680+
Assert.AreEqual(15, Array.findBack funcInt[|1..19|])
681+
Assert.AreEqual(5, Array.findBack funcInt[|5..9|])
682+
683+
// string array
684+
letstrArr=[|"Lists";"are";"a";"commonly";"data";"structor"|]
685+
letfuncStr(x:string)= x.Length>7
686+
letresultStr= Array.findBack funcStr strArr
687+
Assert.AreEqual("structor", resultStr)
688+
689+
// empty array
690+
CheckThrowsKeyNotFoundException(fun()-> Array.findBack(fun _->true)[||]|> ignore)
691+
692+
// not found
693+
CheckThrowsKeyNotFoundException(fun()-> Array.findBack(fun _->false)[|1..20|]|> ignore)
694+
695+
// null array
696+
letnullArr=null:string[]
697+
CheckThrowsArgumentNullException(fun()-> Array.findBack funcStr nullArr|> ignore)
698+
699+
()
700+
672701
[<Test>]
673702
memberthis.FindIndex()=
674703
// integer array
@@ -685,15 +714,43 @@ type ArrayModule() =
685714

686715
// empty array
687716
letemptyArr:int[]=[||]
688-
CheckThrowsKeyNotFoundException(fun()-> Array.findIndex(funx->true) emptyArr|> ignore)
717+
CheckThrowsKeyNotFoundException(fun()-> Array.findIndex(fun_->true) emptyArr|> ignore)
689718

719+
// not found
720+
CheckThrowsKeyNotFoundException(fun()-> Array.findIndex(fun _->false) intArr|> ignore)
690721

691722
// null array
692723
letnullArr=null:string[]
693724
CheckThrowsArgumentNullException(fun()-> Array.findIndex funcStr nullArr|> ignore)
694725

695726
()
696727

728+
[<Test>]
729+
memberthis.FindIndexBack()=
730+
// integer array
731+
letfuncInt x=if(x%5=0)thentrueelsefalse
732+
Assert.AreEqual(19, Array.findIndexBack funcInt[|1..20|])
733+
Assert.AreEqual(14, Array.findIndexBack funcInt[|1..19|])
734+
Assert.AreEqual(0, Array.findIndexBack funcInt[|5..9|])
735+
736+
// string array
737+
letstrArr=[|"Lists";"are";"a";"commonly";"data";"structor"|]
738+
letfuncStr(x:string)=if(x.Length>7)thentrueelsefalse
739+
letresultStr= Array.findIndexBack funcStr strArr
740+
Assert.AreEqual(5, resultStr)
741+
742+
// empty array
743+
CheckThrowsKeyNotFoundException(fun()-> Array.findIndexBack(fun _->true)[||]|> ignore)
744+
745+
// not found
746+
CheckThrowsKeyNotFoundException(fun()-> Array.findIndexBack(fun _->false)[|1..20|]|> ignore)
747+
748+
// null array
749+
letnullArr=null:string[]
750+
CheckThrowsArgumentNullException(fun()-> Array.findIndexBack funcStr nullArr|> ignore)
751+
752+
()
753+
697754
[<Test>]
698755
memberthis.Pick()=
699756
// integers

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,30 @@ type ArrayModule2() =
786786

787787
()
788788

789-
789+
[<Test>]
790+
memberthis.TryFindBack()=
791+
// integer array
792+
letfuncInt x= x%5=0
793+
Assert.AreEqual(Some20,[|1..20|]|> Array.tryFindBack funcInt)
794+
Assert.AreEqual(Some15,[|1..19|]|> Array.tryFindBack funcInt)
795+
Assert.AreEqual(Some5,[|5..9|]|> Array.tryFindBack funcInt)
796+
797+
// string array
798+
letresultStr=[|"Lists";"are";"commonly";"list"|]|> Array.tryFindBack(fun(x:string)-> x.Length>4)
799+
Assert.AreEqual(Some"commonly", resultStr)
800+
801+
// empty array
802+
Assert.AreEqual(None,[||]|> Array.tryFindBack(fun _-> failwith"error"))
803+
804+
// not found
805+
Assert.AreEqual(None,[|1..20|]|> Array.tryFindBack(fun _->false))
806+
807+
// null array
808+
letnullArr=null:string[]
809+
CheckThrowsArgumentNullException(fun()-> Array.tryFindBack(fun _-> failwith"error") nullArr|> ignore)
810+
811+
()
812+
790813
[<Test>]
791814
memberthis.TryFindIndex()=
792815
// integer array
@@ -807,6 +830,30 @@ type ArrayModule2() =
807830

808831
()
809832

833+
[<Test>]
834+
memberthis.TryFindIndexBack()=
835+
// integer array
836+
letfuncInt x= x%5=0
837+
Assert.AreEqual(Some19,[|1..20|]|> Array.tryFindIndexBack funcInt)
838+
Assert.AreEqual(Some14,[|1..19|]|> Array.tryFindIndexBack funcInt)
839+
Assert.AreEqual(Some0,[|5..9|]|> Array.tryFindIndexBack funcInt)
840+
841+
// string array
842+
letresultStr=[|"Lists";"are";"commonly";"list"|]|> Array.tryFindIndexBack(fun(x:string)-> x.Length>4)
843+
Assert.AreEqual(Some2, resultStr)
844+
845+
// empty array
846+
Assert.AreEqual(None,[||]|> Array.tryFindIndexBack(fun _->true))
847+
848+
// not found
849+
Assert.AreEqual(None,[|1..20|]|> Array.tryFindIndexBack(fun _->false))
850+
851+
// null array
852+
letnullArr=null:string[]
853+
CheckThrowsArgumentNullException(fun()-> Array.tryFindIndexBack(fun(x:string)-> x.Length>4) nullArr|> ignore)
854+
855+
()
856+
810857
[<Test>]
811858
memberthis.Unzip()=
812859
// integer array

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

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,11 @@ type ListModule() =
384384

385385
// empty List
386386
letemptyArr:int list=[]
387-
CheckThrowsKeyNotFoundException(fun()-> List.find(funx->true) emptyArr|> ignore)
387+
CheckThrowsKeyNotFoundException(fun()-> List.find(fun_->true) emptyArr|> ignore)
388388

389+
// not found
390+
CheckThrowsKeyNotFoundException(fun()-> List.find(fun _->false) intArr|> ignore)
391+
389392
()
390393

391394
[<Test>]
@@ -398,6 +401,29 @@ type ListModule() =
398401

399402
CheckThrowsArgumentException(fun()-> List.replicate-1null|> ignore)
400403

404+
[<Test>]
405+
memberthis.FindBack()=
406+
// integer List
407+
letfuncInt x=if(x%5=0)thentrueelsefalse
408+
Assert.AreEqual(20, List.findBack funcInt[1..20])
409+
Assert.AreEqual(15, List.findBack funcInt[1..19])
410+
Assert.AreEqual(5, List.findBack funcInt[5..9])
411+
412+
// string List
413+
letstrArr=[".";"..";"...";"...."]
414+
letfuncStr(x:string)= x.Length>2
415+
letresultStr= List.findBack funcStr strArr
416+
Assert.AreEqual("....", resultStr)
417+
418+
// empty List
419+
CheckThrowsKeyNotFoundException(fun()-> List.findBack(fun _->true)[]|> ignore)
420+
421+
// not found
422+
CheckThrowsKeyNotFoundException(fun()-> List.findBack(fun _->false)[1..20]|> ignore)
423+
424+
()
425+
426+
401427
[<Test>]
402428
memberthis.FindIndex()=
403429
// integer List
@@ -408,16 +434,41 @@ type ListModule() =
408434

409435
// string List
410436
letstrArr=[".";"..";"...";"...."]
411-
letfuncStr(x:string)=if(x.Length>2)thentrueelsefalse
437+
letfuncStr(x:string)=if(x.Length>2)thentrueelsefalse
412438
letresultStr= List.findIndex funcStr strArr
413439
Assert.AreEqual(2, resultStr)
414440

415441
// empty List
416442
letemptyArr:int list=[]
417-
CheckThrowsKeyNotFoundException(fun()-> List.findIndex(funx->true) emptyArr|> ignore)
443+
CheckThrowsKeyNotFoundException(fun()-> List.findIndex(fun_->true) emptyArr|> ignore)
418444

445+
// not found
446+
CheckThrowsKeyNotFoundException(fun()-> List.findIndex(fun _->false) intArr|> ignore)
447+
419448
()
420449

450+
[<Test>]
451+
memberthis.FindIndexBack()=
452+
// integer List
453+
letfuncInt x=if(x%5=0)thentrueelsefalse
454+
Assert.AreEqual(19, List.findIndexBack funcInt[1..20])
455+
Assert.AreEqual(14, List.findIndexBack funcInt[1..19])
456+
Assert.AreEqual(0, List.findIndexBack funcInt[5..9])
457+
458+
// string List
459+
letstrArr=[".";"..";"...";"...."]
460+
letfuncStr(x:string)= x.Length>2
461+
letresultStr= List.findIndexBack funcStr strArr
462+
Assert.AreEqual(3, resultStr)
463+
464+
// empty List
465+
CheckThrowsKeyNotFoundException(fun()-> List.findIndexBack(fun _->true)[]|> ignore)
466+
467+
// not found
468+
CheckThrowsKeyNotFoundException(fun()-> List.findIndexBack(fun _->false)[1..20]|> ignore)
469+
470+
()
471+
421472
[<Test>]
422473
memberthis.TryPick()=
423474
// integer List

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,29 @@ type ListModule02() =
667667

668668
()
669669

670+
[<Test>]
671+
memberthis.TryFindBack()=
672+
// integer List
673+
Assert.AreEqual(Some20,[1..20]|> List.tryFindBack(fun x-> x%5=0))
674+
Assert.AreEqual(Some15,[1..19]|> List.tryFindBack(fun x-> x%5=0))
675+
Assert.AreEqual(Some5,[5..9]|> List.tryFindBack(fun x-> x%5=0))
676+
677+
// string List
678+
letresultStr=["a";"b";"cc";"dd"]|> List.tryFindBack(fun(x:string)-> x.Length>1)
679+
Assert.AreEqual(Some"dd", resultStr)
680+
681+
// empty List
682+
Assert.AreEqual(None,[]|> List.tryFindBack(fun _->true))
683+
684+
// not found
685+
Assert.AreEqual(None,[1..20]|> List.tryFindBack(fun _->false))
686+
687+
// Head satisfy
688+
letresultHead=[7..-1..1]|> List.tryFindBack(fun x-> x%7=0)
689+
Assert.AreEqual(Some7, resultHead)
690+
691+
()
692+
670693
[<Test>]
671694
memberthis.TryFindIndex()=
672695
// integer List
@@ -682,6 +705,25 @@ type ListModule02() =
682705
Assert.AreEqual(None, resultEpt)
683706
()
684707

708+
[<Test>]
709+
memberthis.TryFindIndexBack()=
710+
// integer List
711+
Assert.AreEqual(Some19,[1..20]|> List.tryFindIndexBack(fun x-> x%5=0))
712+
Assert.AreEqual(Some14,[1..19]|> List.tryFindIndexBack(fun x-> x%5=0))
713+
Assert.AreEqual(Some0,[5..9]|> List.tryFindIndexBack(fun x-> x%5=0))
714+
715+
// string List
716+
letresultStr=["a";"b";"cc";"dd"]|> List.tryFindIndexBack(fun(x:string)-> x.Length>1)
717+
Assert.AreEqual(Some3, resultStr)
718+
719+
// empty List
720+
Assert.AreEqual(None,[]|> List.tryFindIndexBack(fun _->true))
721+
722+
// not found
723+
Assert.AreEqual(None,[1..20]|> List.tryFindIndexBack(fun _->false))
724+
725+
()
726+
685727
[<Test>]
686728
memberthis.Unzip()=
687729
// integer List

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,33 @@ type SeqModule() =
609609
CheckThrowsArgumentNullException(fun()-> Seq.find funcInt nullSeq|> ignore)
610610
()
611611

612+
[<Test>]
613+
memberthis.FindBack()=
614+
// integer Seq
615+
letfuncInt x= x%5=0
616+
Assert.AreEqual(20, Seq.findBack funcInt<|seq{1..20})
617+
Assert.AreEqual(15, Seq.findBack funcInt<|seq{1..19})
618+
Assert.AreEqual(5, Seq.findBack funcInt<|seq{5..9})
619+
620+
// string Seq
621+
letfuncStr(s:string)= s.Contains("Expected")
622+
letstrSeq= seq["Not Expected";"Expected Content"]
623+
letfindStr= Seq.findBack funcStr strSeq
624+
Assert.AreEqual("Expected Content", findStr)
625+
626+
// Empty Seq
627+
letemptySeq= Seq.empty
628+
CheckThrowsKeyNotFoundException(fun()-> Seq.findBack funcInt emptySeq|> ignore)
629+
630+
// Not found
631+
letemptySeq= Seq.empty
632+
CheckThrowsKeyNotFoundException(fun()->seq{1..20}|> Seq.findBack(fun _->false)|> ignore)
633+
634+
// null Seq
635+
letnullSeq:seq<'a>=null
636+
CheckThrowsArgumentNullException(fun()-> Seq.findBack funcInt nullSeq|> ignore)
637+
()
638+
612639
[<Test>]
613640
memberthis.FindIndex()=
614641

@@ -648,7 +675,26 @@ type SeqModule() =
648675
// argument exceptions
649676
CheckThrowsArgumentException(fun()-> Seq.permute(fun _->10)[0..9]|> Seq.iter ignore)
650677
CheckThrowsArgumentException(fun()-> Seq.permute(fun _->0)[0..9]|> Seq.iter ignore)
678+
()
679+
680+
[<Test>]
681+
memberthis.FindIndexBack()=
682+
// integer Seq
683+
letdigits=seq{1..100}
684+
letidx= digits|> Seq.findIndexBack(fun i-> i.ToString().Length=1)
685+
Assert.AreEqual(idx,8)
651686

687+
// string Seq
688+
letfuncStr(s:string)= s.Contains("Expected")
689+
letstrSeq= seq["Not Expected";"Expected Content"]
690+
letfindStr= Seq.findIndexBack funcStr strSeq
691+
Assert.AreEqual(1, findStr)
692+
693+
// empty Seq
694+
CheckThrowsKeyNotFoundException(fun()-> Seq.findIndexBack(fun i->true) Seq.empty|> ignore)
695+
696+
// null Seq
697+
CheckThrowsArgumentNullException(fun()-> Seq.findIndexBack(fun i->true)null|> ignore)
652698
()
653699

654700
[<Test>]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp