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

Commite8ac90a

Browse files
mexxlatkin
authored andcommitted
Implement Array.contains, List.contains, Seq.contains
add Seq.contains function add List.contains function add Array.contains function adujst assertion as requested by code review remove empty comment lines refactor Seq.contains towards better performance as suggested by code review refactor List.contains towards better performance as suggested by code review refactor Array.contains towards better performance as suggested by code review update surface area tests with added contains functions define contains functions as inline Resetting all of the whitespace changes, so that overall diff stays clean
1 parentbea1a9e commite8ac90a

File tree

11 files changed

+121
-1
lines changed

11 files changed

+121
-1
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,3 +1124,24 @@ type ArrayModule() =
11241124
memberthis.``Parallel.Partition``()=
11251125
this.PartitionTester Array.Parallel.partition Array.Parallel.partition
11261126
#endif
1127+
1128+
[<Test>]
1129+
memberthis.Contains()=
1130+
// integer array
1131+
letintArr=[|2;4;6;8|]
1132+
letresultInt= Array.contains6 intArr
1133+
Assert.IsTrue(resultInt)
1134+
1135+
// string array
1136+
letstrArr=[|"Lists";"are";"commonly"|]
1137+
letresultStr= Array.contains"not" strArr
1138+
Assert.IsFalse(resultStr)
1139+
1140+
// empty array
1141+
letemptyArr:int[]=[||]
1142+
letresultEpt= Array.contains4 emptyArr
1143+
Assert.IsFalse(resultEpt)
1144+
1145+
// null array
1146+
letnullArr=null:string[]
1147+
CheckThrowsArgumentNullException(fun()-> Array.contains"empty" nullArr|> ignore)

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,21 @@ type ListModule() =
606606
List.iteri2 funInt emptyArr emptyArr
607607
Assert.AreEqual(0,!resultEpt)
608608

609-
()
609+
()
610+
611+
[<Test>]
612+
memberthis.Contains()=
613+
// integer List
614+
letintList=[2;4;6;8]
615+
letresultInt= List.contains4 intList
616+
Assert.IsTrue(resultInt)
617+
618+
// string List
619+
letstrList=[".";"..";"...";"...."]
620+
letresultStr= List.contains"....." strList
621+
Assert.IsFalse(resultStr)
622+
623+
// empty List
624+
letemptyList:int list=[]
625+
letresultEpt= List.contains4 emptyList
626+
Assert.IsFalse(resultEpt)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,30 @@ type SeqModule() =
797797
letl= f3|> Seq.toList
798798
Assert.AreEqual([6;7;8], l)
799799

800+
[<Test>]
801+
memberthis.Contains()=
802+
803+
// Integer Seq
804+
letintSeq=seq{0..9}
805+
806+
letifContainsInt= Seq.contains5 intSeq
807+
808+
Assert.IsTrue(ifContainsInt)
809+
810+
// String Seq
811+
letstrSeq= seq["key";"blank key"]
812+
813+
letifContainsStr= Seq.contains"key" strSeq
814+
815+
Assert.IsTrue(ifContainsStr)
816+
817+
// Empty Seq
818+
letemptySeq= Seq.empty
819+
letifContainsEmpty= Seq.contains5 emptySeq
820+
821+
Assert.IsFalse(ifContainsEmpty)
822+
823+
// null Seq
824+
letnullSeq:seq<'a>=null
825+
826+
CheckThrowsArgumentNullException(fun()-> Seq.contains5 nullSeq|> ignore)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Microsoft.FSharp.Collections.ArrayModule+Parallel: TResult[] Map[T,TResult](Micr
9393
Microsoft.FSharp.Collections.ArrayModule+Parallel: T[] Initialize[T](Int32, Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,T])
9494
Microsoft.FSharp.Collections.ArrayModule+Parallel: Void IterateIndexed[T](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit]], T[])
9595
Microsoft.FSharp.Collections.ArrayModule+Parallel: Void Iterate[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit], T[])
96+
Microsoft.FSharp.Collections.ArrayModule: Boolean Contains[T](T, T[])
9697
Microsoft.FSharp.Collections.ArrayModule: Boolean Equals(System.Object)
9798
Microsoft.FSharp.Collections.ArrayModule: Boolean Exists2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,System.Boolean]], T1[], T2[])
9899
Microsoft.FSharp.Collections.ArrayModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
@@ -256,6 +257,7 @@ Microsoft.FSharp.Collections.HashIdentity: System.Collections.Generic.IEqualityC
256257
Microsoft.FSharp.Collections.HashIdentity: System.Collections.Generic.IEqualityComparer`1[T] Structural[T]()
257258
Microsoft.FSharp.Collections.HashIdentity: System.String ToString()
258259
Microsoft.FSharp.Collections.HashIdentity: System.Type GetType()
260+
Microsoft.FSharp.Collections.ListModule: Boolean Contains[T](T, Microsoft.FSharp.Collections.FSharpList`1[T])
259261
Microsoft.FSharp.Collections.ListModule: Boolean Equals(System.Object)
260262
Microsoft.FSharp.Collections.ListModule: Boolean Exists2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,System.Boolean]], Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
261263
Microsoft.FSharp.Collections.ListModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
@@ -351,6 +353,7 @@ Microsoft.FSharp.Collections.MapModule: TResult Pick[TKey,T,TResult](Microsoft.F
351353
Microsoft.FSharp.Collections.MapModule: TState FoldBack[TKey,T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TKey,Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]]], Microsoft.FSharp.Collections.FSharpMap`2[TKey,T], TState)
352354
Microsoft.FSharp.Collections.MapModule: TState Fold[TKey,T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[TKey,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]]], TState, Microsoft.FSharp.Collections.FSharpMap`2[TKey,T])
353355
Microsoft.FSharp.Collections.MapModule: Void Iterate[TKey,T](Microsoft.FSharp.Core.FSharpFunc`2[TKey,Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit]], Microsoft.FSharp.Collections.FSharpMap`2[TKey,T])
356+
Microsoft.FSharp.Collections.SeqModule: Boolean Contains[T](T, System.Collections.Generic.IEnumerable`1[T])
354357
Microsoft.FSharp.Collections.SeqModule: Boolean Equals(System.Object)
355358
Microsoft.FSharp.Collections.SeqModule: Boolean Exists2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,System.Boolean]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
356359
Microsoft.FSharp.Collections.SeqModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], 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
@@ -88,6 +88,7 @@ Microsoft.FSharp.Collections.Array4DModule: T[,,,] Create[T](Int32, Int32, Int32
8888
Microsoft.FSharp.Collections.Array4DModule: T[,,,] Initialize[T](Int32, Int32, Int32, Int32, Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,T]]]])
8989
Microsoft.FSharp.Collections.Array4DModule: T[,,,] ZeroCreate[T](Int32, Int32, Int32, Int32)
9090
Microsoft.FSharp.Collections.Array4DModule: Void Set[T](T[,,,], Int32, Int32, Int32, Int32, T)
91+
Microsoft.FSharp.Collections.ArrayModule: Boolean Contains[T](T, T[])
9192
Microsoft.FSharp.Collections.ArrayModule: Boolean Equals(System.Object)
9293
Microsoft.FSharp.Collections.ArrayModule: Boolean Exists2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,System.Boolean]], T1[], T2[])
9394
Microsoft.FSharp.Collections.ArrayModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
@@ -250,6 +251,7 @@ Microsoft.FSharp.Collections.HashIdentity: System.Collections.Generic.IEqualityC
250251
Microsoft.FSharp.Collections.HashIdentity: System.Collections.Generic.IEqualityComparer`1[T] Structural[T]()
251252
Microsoft.FSharp.Collections.HashIdentity: System.String ToString()
252253
Microsoft.FSharp.Collections.HashIdentity: System.Type GetType()
254+
Microsoft.FSharp.Collections.ListModule: Boolean Contains[T](T, Microsoft.FSharp.Collections.FSharpList`1[T])
253255
Microsoft.FSharp.Collections.ListModule: Boolean Equals(System.Object)
254256
Microsoft.FSharp.Collections.ListModule: Boolean Exists2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,System.Boolean]], Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
255257
Microsoft.FSharp.Collections.ListModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
@@ -345,6 +347,7 @@ Microsoft.FSharp.Collections.MapModule: TResult Pick[TKey,T,TResult](Microsoft.F
345347
Microsoft.FSharp.Collections.MapModule: TState FoldBack[TKey,T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TKey,Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]]], Microsoft.FSharp.Collections.FSharpMap`2[TKey,T], TState)
346348
Microsoft.FSharp.Collections.MapModule: TState Fold[TKey,T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[TKey,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]]], TState, Microsoft.FSharp.Collections.FSharpMap`2[TKey,T])
347349
Microsoft.FSharp.Collections.MapModule: Void Iterate[TKey,T](Microsoft.FSharp.Core.FSharpFunc`2[TKey,Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit]], Microsoft.FSharp.Collections.FSharpMap`2[TKey,T])
350+
Microsoft.FSharp.Collections.SeqModule: Boolean Contains[T](T, System.Collections.Generic.IEnumerable`1[T])
348351
Microsoft.FSharp.Collections.SeqModule: Boolean Equals(System.Object)
349352
Microsoft.FSharp.Collections.SeqModule: Boolean Exists2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,System.Boolean]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
350353
Microsoft.FSharp.Collections.SeqModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ namespace Microsoft.FSharp.Collections
218218
let recloop i= i< len&&(f array.[i]|| loop(i+1))
219219
loop0
220220

221+
[<CompiledName("Contains")>]
222+
let inlinecontains e(array:'T[])=
223+
checkNonNull"array" array
224+
let mutablestate=false
225+
let mutablei=0
226+
while(not state&& i< array.Length)do
227+
state<- e= array.[i]
228+
i<- i+1
229+
state
230+
221231
[<CompiledName("Exists2")>]
222232
letexists2 f(array1:_[])(array2:_[])=
223233
checkNonNull"array1" array1

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ namespace Microsoft.FSharp.Collections
6262
///<returns>The concatenation of the sequence of input arrays.</returns>
6363
[<CompiledName("Concat")>]
6464
val concat:arrays:seq<'T[]>-> 'T[]
65+
66+
///<summary>Tests if the array contains the specified element.</summary>
67+
///<param name="value">The value to locate in the input array.</param>
68+
///<param name="array">The input array.</param>
69+
///<returns>True if the input array contains the specified element;false otherwise.</returns>
70+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
71+
[<CompiledName("Contains")>]
72+
val inline contains:value:'T->array:'T[]->bool when 'T:equality
6573

6674
///<summary>Builds a new array that contains the elements of the given array.</summary>
6775
///<param name="array">The input array.</param>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ namespace Microsoft.FSharp.Collections
269269

270270
[<CompiledName("Exists")>]
271271
letexists f list1= Microsoft.FSharp.Primitives.Basics.List.exists f list1
272+
273+
[<CompiledName("Contains")>]
274+
let inlinecontains e list1=
275+
let reccontains e xs1=
276+
match xs1with
277+
|[]->false
278+
|(h1::t1)-> e= h1|| contains e t1
279+
contains e list1
272280

273281
let recexists2aux(f:OptimizedClosures.FSharpFunc<_,_,_>)list1 list2=
274282
match list1,list2with

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ namespace Microsoft.FSharp.Collections
6666
///<returns>The resulting concatenated list.</returns>
6767
[<CompiledName("Concat")>]
6868
val concat:lists:seq<'T list>-> 'T list
69+
70+
///<summary>Tests if the list contains the specified element.</summary>
71+
///<param name="value">The value to locate in the input list.</param>
72+
///<param name="source">The input list.</param>
73+
///<returns>True if the input list contains the specified element;false otherwise.</returns>
74+
[<CompiledName("Contains")>]
75+
val inline contains:value:'T->source:'T list->bool when 'T:equality
6976

7077
///<summary>Returns an empty list of the given type.</summary>
7178
[<GeneralizableValue>]

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,15 @@ namespace Microsoft.FSharp.Collections
881881
state<- f e.Current
882882
state
883883

884+
[<CompiledName("Contains")>]
885+
let inlinecontains element(source:seq<'T>)=
886+
checkNonNull"source" source
887+
use e= source.GetEnumerator()
888+
let mutablestate=false
889+
while(not state&& e.MoveNext())do
890+
state<- element= e.Current
891+
state
892+
884893
[<CompiledName("ForAll")>]
885894
letforall f(source:seq<'T>)=
886895
checkNonNull"source" source

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp