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

Commit0fd67db

Browse files
forkilatkin
authored andcommitted
Implement Array.where, List.where
Commits: Implementing "where" for Array and List Alphabetical order in .fsi files Show possible System.ArgumentNullException in docs for Array.where Make XML docs more specific for Array.where and List.where Make it more obvious that Array.where = Array.filter Adding surface area fir Array.where and List.where Array.filter already does a null check. No need to do this in Array.where
1 parente8ac90a commit0fd67db

File tree

8 files changed

+89
-0
lines changed

8 files changed

+89
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,38 @@ type ArrayModule() =
451451

452452
()
453453

454+
455+
[<Test>]
456+
memberthis.Where()=
457+
// integer array
458+
letintArr=[|1..20|]
459+
letfuncInt x=if(x%5=0)thentrueelsefalse
460+
letresultInt= Array.where funcInt intArr
461+
if resultInt<>[|5;10;15;20|]then Assert.Fail()
462+
463+
// string array
464+
letstrArr=[|"Lists";"are";"a";"commonly";"data";"structor"|]
465+
letfuncStr(x:string)=if(x.Length>4)thentrueelsefalse
466+
letresultStr= Array.where funcStr strArr
467+
if resultStr<>[|"Lists";"commonly";"structor"|]then Assert.Fail()
468+
469+
// empty array
470+
letemptyArr:int[]=[||]
471+
letresultEpt= Array.where funcInt emptyArr
472+
if resultEpt<>[||]then Assert.Fail()
473+
474+
// null array
475+
letnullArr=null:string[]
476+
CheckThrowsArgumentNullException(fun()-> Array.where funcStr nullArr|> ignore)
477+
478+
()
479+
480+
[<Test>]
481+
memberthis.``where should work like filter``()=
482+
Assert.AreEqual([||], Array.where(fun x-> x%2=0)[||])
483+
Assert.AreEqual([|0;2;4;6;8|], Array.where(fun x-> x%2=0)[|0..9|])
484+
Assert.AreEqual([|"a";"b";"c"|], Array.where(fun _->true)[|"a";"b";"c"|])
485+
454486
[<Test>]
455487
memberthis.Find()=
456488
// integer array

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,35 @@ type ListModule() =
228228

229229
()
230230

231+
[<Test>]
232+
memberthis.Where()=
233+
// integer List
234+
letintArr=[1..20]
235+
letfuncInt x=if(x%5=0)thentrueelsefalse
236+
letresultInt= List.where funcInt intArr
237+
Assert.AreEqual([5;10;15;20], resultInt)
238+
239+
// string List
240+
letstrArr=[".";"..";"...";"...."]
241+
letfuncStr(x:string)=if(x.Length>2)thentrueelsefalse
242+
letresultStr= List.where funcStr strArr
243+
Assert.AreEqual(["...";"...."], resultStr)
244+
245+
// empty List
246+
letemptyArr:int list=[]
247+
letresultEpt= List.where funcInt emptyArr
248+
Assert.AreEqual(emptyArr, resultEpt)
249+
250+
()
251+
252+
[<Test>]
253+
memberthis.``where should work like filter``()=
254+
Assert.AreEqual([], List.where(fun x-> x%2=0)[])
255+
Assert.AreEqual([0;2;4;6;8], List.where(fun x-> x%2=0)[0..9])
256+
Assert.AreEqual(["a";"b";"c"], List.where(fun _->true)["a";"b";"c"])
257+
258+
()
259+
231260
[<Test>]
232261
memberthis.Find()=
233262
// integer List

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Reverse[T](T[])
156156
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
157157
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
158158
Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
159+
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
159160
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
160161
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
161162
Microsoft.FSharp.Collections.ArrayModule: Void Fill[T](T[], Int32, Int32, T)
@@ -292,6 +293,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
292293
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], Microsoft.FSharp.Collections.FSharpList`1[T])
293294
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Sort[T](Microsoft.FSharp.Collections.FSharpList`1[T])
294295
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Tail[T](Microsoft.FSharp.Collections.FSharpList`1[T])
296+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
295297
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])
296298
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])
297299
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])

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Reverse[T](T[])
150150
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
151151
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
152152
Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
153+
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
153154
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
154155
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
155156
Microsoft.FSharp.Collections.ArrayModule: Void Fill[T](T[], Int32, Int32, T)
@@ -286,6 +287,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
286287
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], Microsoft.FSharp.Collections.FSharpList`1[T])
287288
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Sort[T](Microsoft.FSharp.Collections.FSharpList`1[T])
288289
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Tail[T](Microsoft.FSharp.Collections.FSharpList`1[T])
290+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
289291
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])
290292
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])
291293
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])

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ namespace Microsoft.FSharp.Collections
296296
if f xthen res.Add(x)
297297
res.ToArray()
298298

299+
[<CompiledName("Where")>]
300+
letwhere f(array:_[])= filter f array
301+
299302
[<CompiledName("Partition")>]
300303
letpartition f(array:_[])=
301304
checkNonNull"array" array

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,16 @@ namespace Microsoft.FSharp.Collections
600600
[<CompiledName("Unzip3")>]
601601
val unzip3:array:('T1* 'T2* 'T3)[]->('T1[]* 'T2[]* 'T3[])
602602

603+
///<summary>Returns a new array containing only the elements of the array
604+
///for which the given predicate returns"true".</summary>
605+
///<param name="predicate">The function to test the input elements.</param>
606+
///<param name="array">The input array.</param>
607+
///<returns>An array containing the elements for which the given predicate returns true.</returns>
608+
///
609+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
610+
[<CompiledName("Where")>]
611+
val where:predicate:('T->bool)->array:'T[]-> 'T[]
612+
603613
///<summary>Combines the two arrays into an array of pairs. The two arrays must have equal lengths,otherwise an <c>ArgumentException</c> is
604614
///raised.</summary>
605615
///<param name="array1">The first input array.</param>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ namespace Microsoft.FSharp.Collections
319319
[<CompiledName("Filter")>]
320320
letfilter f x= Microsoft.FSharp.Primitives.Basics.List.filter f x
321321

322+
[<CompiledName("Where")>]
323+
letwhere f x= Microsoft.FSharp.Primitives.Basics.List.filter f x
324+
322325
[<CompiledName("Partition")>]
323326
letpartition p x= Microsoft.FSharp.Primitives.Basics.List.partition p x
324327

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,14 @@ namespace Microsoft.FSharp.Collections
553553
///<returns>Three lists of split elements.</returns>
554554
[<CompiledName("Unzip3")>]
555555
val unzip3:list:('T1* 'T2* 'T3)list->('T1 list* 'T2 list* 'T3 list)
556+
557+
///<summary>Returns a new list containing only the elements of the list
558+
///for which the given predicate returns"true"</summary>
559+
///<param name="predicate">The function to test the input elements.</param>
560+
///<param name="list">The input list.</param>
561+
///<returns>A list containing only the elements that satisfy the predicate.</returns>
562+
[<CompiledName("Where")>]
563+
val where:predicate:('T->bool)->list:'T list-> 'T list
556564

557565
///<summary>Combines the two lists into a list of pairs. The two lists must have equal lengths.</summary>
558566
///<param name="list1">The first input list.</param>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp