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

Commit646db39

Browse files
Implement except on Seq, Array and List
1 parent70ed65d commit646db39

File tree

10 files changed

+169
-3
lines changed

10 files changed

+169
-3
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,39 @@ type ArrayModule() =
185185
Assert.AreEqual([|null|], Array.distinctBy id[|null|])
186186
letlist=new System.Collections.Generic.List<int>()
187187
Assert.AreEqual([|null, list|], Array.distinctBy id[|null, list|])
188-
188+
189+
[<Test>]
190+
memberthis.Except()=
191+
// integer array
192+
letintArr1=[|yield!{1..100}
193+
yield!{1..100}|]
194+
letintArr2=[|1..10|]
195+
letexpectedIntArr=[|11..100|]
196+
197+
Assert.AreEqual(expectedIntArr, Array.except intArr1 intArr2)
198+
199+
// string array
200+
letstrArr1=[|"a";"b";"c";"d";"a"|]
201+
letstrArr2=[|"b";"c"|]
202+
letexpectedStrArr=[|"a";"d"|]
203+
204+
Assert.AreEqual(expectedStrArr, Array.except strArr1 strArr2)
205+
206+
// empty array
207+
letemptyIntArr=[||]
208+
Assert.AreEqual([|1..100|], Array.except intArr1 emptyIntArr)
209+
Assert.AreEqual(emptyIntArr, Array.except emptyIntArr intArr1)
210+
Assert.AreEqual(emptyIntArr, Array.except emptyIntArr emptyIntArr)
211+
Assert.AreEqual(emptyIntArr, Array.except intArr1 intArr1)
212+
213+
// null array
214+
letnullArr:int[]=null
215+
CheckThrowsArgumentNullException(fun()-> Array.except nullArr emptyIntArr|> ignore)
216+
CheckThrowsArgumentNullException(fun()-> Array.except emptyIntArr nullArr|> ignore)
217+
CheckThrowsArgumentNullException(fun()-> Array.except nullArr nullArr|> ignore)
218+
219+
()
220+
189221
[<Test>]
190222
memberthis.Take()=
191223
Assert.AreEqual([||],Array.take0[||])

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,32 @@ type ListModule() =
274274
Assert.AreEqual([5,1;2,2;3,2],List.countBy id[5;2;2;3;3])
275275
Assert.AreEqual([3,3;2,2;1,3],List.countBy(fun x->if x<3then xelse3)[5;2;1;2;3;3;1;1])
276276

277+
[<Test>]
278+
memberthis.Except()=
279+
// integer list
280+
letintList1=[yield!{1..100}
281+
yield!{1..100}]
282+
letintList2=[1..10]
283+
letexpectedIntList=[11..100]
284+
285+
Assert.AreEqual(expectedIntList, List.except intList1 intList2)
286+
287+
// string list
288+
letstrList1=["a";"b";"c";"d";"a"]
289+
letstrList2=["b";"c"]
290+
letexpectedStrList=["a";"d"]
291+
292+
Assert.AreEqual(expectedStrList, List.except strList1 strList2)
293+
294+
// empty list
295+
letemptyIntList=[]
296+
Assert.AreEqual([1..100], List.except intList1 emptyIntList)
297+
Assert.AreEqual(emptyIntList, List.except emptyIntList intList1)
298+
Assert.AreEqual(emptyIntList, List.except emptyIntList emptyIntList)
299+
Assert.AreEqual(emptyIntList, List.except intList1 intList1)
300+
301+
()
302+
277303
[<Test>]
278304
memberthis.Exists()=
279305
// integer List

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,39 @@ type SeqModule() =
477477

478478
CheckThrowsArgumentNullException(fun()-> Seq.distinctBy funcInt nullSeq|> ignore)
479479
()
480-
480+
481+
[<Test>]
482+
memberthis.Except()=
483+
// integer Seq
484+
letintSeq1=seq{yield!{1..100}
485+
yield!{1..100}}
486+
letintSeq2={1..10}
487+
letexpectedIntSeq={11..100}
488+
489+
VerifySeqsEqual expectedIntSeq<| Seq.except intSeq1 intSeq2
490+
491+
// string Seq
492+
letstrSeq1= seq["a";"b";"c";"d";"a"]
493+
letstrSeq2= seq["b";"c"]
494+
letexpectedStrSeq= seq["a";"d"]
495+
496+
VerifySeqsEqual expectedStrSeq<| Seq.except strSeq1 strSeq2
497+
498+
// empty Seq
499+
letemptyIntSeq= Seq.empty<int>
500+
VerifySeqsEqual{1..100}<| Seq.except intSeq1 emptyIntSeq
501+
VerifySeqsEqual emptyIntSeq<| Seq.except emptyIntSeq intSeq1
502+
VerifySeqsEqual emptyIntSeq<| Seq.except emptyIntSeq emptyIntSeq
503+
VerifySeqsEqual emptyIntSeq<| Seq.except intSeq1 intSeq1
504+
505+
// null Seq
506+
letnullSeq:seq<int>=null
507+
CheckThrowsArgumentNullException(fun()-> Seq.except nullSeq emptyIntSeq|> ignore)
508+
CheckThrowsArgumentNullException(fun()-> Seq.except emptyIntSeq nullSeq|> ignore)
509+
CheckThrowsArgumentNullException(fun()-> Seq.except nullSeq nullSeq|> ignore)
510+
511+
()
512+
481513
[<Test>]
482514
memberthis.Exists()=
483515

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Create[T](Int32, T)
149149
Microsoft.FSharp.Collections.ArrayModule: T[] DistinctBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
150150
Microsoft.FSharp.Collections.ArrayModule: T[] Distinct[T](T[])
151151
Microsoft.FSharp.Collections.ArrayModule: T[] Empty[T]()
152+
Microsoft.FSharp.Collections.ArrayModule: T[] Except[T](T[], T[])
152153
Microsoft.FSharp.Collections.ArrayModule: T[] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
153154
Microsoft.FSharp.Collections.ArrayModule: T[] GetSubArray[T](T[], Int32, Int32)
154155
Microsoft.FSharp.Collections.ArrayModule: T[] Initialize[T](Int32, Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,T])
@@ -309,6 +310,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
309310
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] DistinctBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
310311
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Distinct[T](Microsoft.FSharp.Collections.FSharpList`1[T])
311312
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Empty[T]()
313+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Except[T](Microsoft.FSharp.Collections.FSharpList`1[T], Microsoft.FSharp.Collections.FSharpList`1[T])
312314
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
313315
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Initialize[T](Int32, Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,T])
314316
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] OfArray[T](T[])
@@ -448,6 +450,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
448450
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] DistinctBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
449451
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Distinct[T](System.Collections.Generic.IEnumerable`1[T])
450452
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Empty[T]()
453+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Except[T](System.Collections.Generic.IEnumerable`1[T], System.Collections.Generic.IEnumerable`1[T])
451454
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
452455
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] InitializeInfinite[T](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,T])
453456
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Initialize[T](Int32, Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,T])

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ namespace Microsoft.FSharp.Collections
200200
Array.Copy(array1,0, res,0, n1)
201201
Array.Copy(array2,0, res, n1, n2)
202202
res
203-
203+
204204
[<CompiledName("Head")>]
205205
lethead(array:'T[])=
206206
checkNonNull"array" array
@@ -478,6 +478,17 @@ namespace Microsoft.FSharp.Collections
478478
[<CompiledName("Where")>]
479479
letwhere f(array:_[])= filter f array
480480

481+
[<CompiledName("Except")>]
482+
letexcept(array1:'T[])(array2:'T[])=
483+
checkNonNull"array1" array1
484+
checkNonNull"array2" array2
485+
486+
if array1.Length=0then
487+
array1
488+
else
489+
letcached= HashSet(array2, HashIdentity.Structural)
490+
filter(fun item-> cached.Add item) array1
491+
481492
[<CompiledName("Partition")>]
482493
letpartition f(array:_[])=
483494
checkNonNull"array" array

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ namespace Microsoft.FSharp.Collections
216216
[<CompiledName("ExactlyOne")>]
217217
val exactlyOne:array:'T[]-> 'T
218218

219+
///<summary>Produces the set difference of two arrays by using generic hash and equality comparisons to compare values.</summary>
220+
///
221+
///<param name="array1">An array whose elements that are not also in second will be returned.</param>
222+
///<param name="array2">A second array whose elements that also occur in the first array will cause those elements to be
223+
///removed from the result.</param>
224+
///
225+
///<returns>An array that contains the set difference of the elements of two arrays.</returns>
226+
///
227+
///<exception cref="System.ArgumentNullException">Thrown when either of the two input arrays is null.</exception>
228+
[<CompiledName("Except")>]
229+
val except:array1:'T[]->array2:'T[]-> 'T[]when 'T:equality
230+
219231
///<summary>Tests if any element of the array satisfies the given predicate.</summary>
220232
///
221233
///<remarks>The predicate is applied to the elements of the input array. If any application

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ namespace Microsoft.FSharp.Collections
419419
[<CompiledName("Filter")>]
420420
letfilter f x= Microsoft.FSharp.Primitives.Basics.List.filter f x
421421

422+
[<CompiledName("Except")>]
423+
letexcept list1(list2:_ list)=
424+
match list1, list2with
425+
|[],_-> list1
426+
|_->
427+
letcached= HashSet(list2, HashIdentity.Structural)
428+
list1|> filter(fun item-> cached.Add item)
429+
422430
[<CompiledName("Where")>]
423431
letwhere f x= Microsoft.FSharp.Primitives.Basics.List.filter f x
424432

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ namespace Microsoft.FSharp.Collections
125125
[<CompiledName("Empty")>]
126126
val empty<'T>: 'T list
127127

128+
///<summary>Produces the set difference of two lists by using generic hash and equality comparisons to compare values.</summary>
129+
///
130+
///<param name="list1">A list whose elements that are not also in second will be returned.</param>
131+
///<param name="list2">A second list whose elements that also occur in the first list will cause those elements to be
132+
///removed from the result.</param>
133+
///
134+
///<returns>A list that contains the set difference of the elements of two lists.</returns>
135+
[<CompiledName("Except")>]
136+
val except:list1:'T list->list2:'T list-> 'T list when 'T:equality
137+
128138
///<summary>Returns the only element of the list.</summary>
129139
///
130140
///<param name="list">The input list.</param>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,3 +1813,18 @@ namespace Microsoft.FSharp.Collections
18131813
letarray= source|> toArray
18141814
letarr,state= Array.mapFoldBack f array acc
18151815
readonly arr, state
1816+
1817+
[<CompiledName("Except")>]
1818+
letexcept(source1:seq<'T>)(source2:seq<'T>)=
1819+
checkNonNull"source1" source1
1820+
checkNonNull"source2" source2
1821+
1822+
seq{
1823+
use e= source1.GetEnumerator()
1824+
if e.MoveNext()then
1825+
letcached= HashSet(source2)
1826+
letnext= e.Current
1827+
if(cached.Add next)thenyield next
1828+
while e.MoveNext()do
1829+
letnext= e.Current
1830+
if(cached.Add next)thenyield next}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,23 @@ namespace Microsoft.FSharp.Collections
243243
[<CompiledName("Empty")>]
244244
val empty<'T>:seq<'T>
245245

246+
///<summary>Produces the set difference of two sequences by using generic hash and equality comparisons to compare values.</summary>
247+
///
248+
///<remarks>Note that this function returns a sequence that digests the whole of the second input sequence as soon as
249+
///the result sequence is iterated. As a result this function should not be used with
250+
///large or infinite sequences. The function makes no assumption on the ordering of the second input
251+
///sequence.</remarks>
252+
///
253+
///<param name="source1">A sequence whose elements that are not also in second will be returned.</param>
254+
///<param name="source2">A second sequence whose elements that also occur in the first sequence will cause those elements to be
255+
///removed from the returned sequence.</param>
256+
///
257+
///<returns>A sequence that contains the set difference of the elements of two sequences.</returns>
258+
///
259+
///<exception cref="System.ArgumentNullException">Thrown when either of the two input sequences is null.</exception>
260+
[<CompiledName("Except")>]
261+
val except:source1:seq<'T>->source2:seq<'T>->seq<'T> when 'T:equality
262+
246263
///<summary>Tests if any element of the sequence satisfies the given predicate.</summary>
247264
///
248265
///<remarks>The predicate is applied to the elements of the input sequence. If any application

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp