Skip to content

Commit 42269e5

Browse files
committed
Implement TaskSeq.removeAt and TaskSeq.removeManyAt
1 parent 077c355 commit 42269e5

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/FSharp.Control.TaskSeq/TaskSeq.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ type TaskSeq private () =
317317

318318
static member insertAt index value source = Internal.insertAt index (One value) source
319319
static member insertManyAt index values source = Internal.insertAt index (Many values) source
320+
static member removeAt index source = Internal.removeAt index source
321+
static member removeManyAt index count source = Internal.removeManyAt index count source
320322

321323
static member except itemsToExclude source = Internal.except itemsToExclude source
322324
static member exceptOfSeq itemsToExclude source = Internal.exceptOfSeq itemsToExclude source

src/FSharp.Control.TaskSeq/TaskSeq.fsi

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,3 +1300,29 @@ type TaskSeq =
13001300
/// <exception cref="T:ArgumentNullException">Thrown when the input task sequence is null.</exception>
13011301
/// <exception cref="T:ArgumentException">Thrown when index is below 0 or greater than source length.</exception>
13021302
static member insertManyAt: index: int -> values: TaskSeq<'T> -> source: TaskSeq<'T> -> TaskSeq<'T>
1303+
1304+
/// <summary>
1305+
/// Return a new task sequence with the item at the given index removed.
1306+
/// </summary>
1307+
///
1308+
/// <param name="index">The index where the item should be removed.</param>
1309+
/// <param name="source">The input task sequence.</param>
1310+
/// <returns>The result task sequence.</returns>
1311+
/// <exception cref="T:ArgumentNullException">Thrown when the input task sequence is null.</exception>
1312+
/// <exception cref="T:ArgumentException">Thrown when index is below 0 or greater than source length.</exception>
1313+
static member removeAt: index: int -> source: TaskSeq<'T> -> TaskSeq<'T>
1314+
1315+
/// <summary>
1316+
/// Return a new task sequence with the number of items starting at a given index removed.
1317+
/// If <paramref name="count" /> is negative or zero, no items are removed. If <paramref name="index" />
1318+
/// + <paramref name="count" /> is greater than source length, but <paramref name="index" /> is not, then
1319+
/// all items until end of sequence are removed.
1320+
/// </summary>
1321+
///
1322+
/// <param name="index">The index where the items should be removed.</param>
1323+
/// <param name="count">The number of items to remove.</param>
1324+
/// <param name="source">The input task sequence.</param>
1325+
/// <returns>The result task sequence.</returns>
1326+
/// <exception cref="T:ArgumentNullException">Thrown when the input task sequence is null.</exception>
1327+
/// <exception cref="T:ArgumentException">Thrown when index is below 0 or greater than source length.</exception>
1328+
static member removeManyAt: index: int -> count: int -> source: TaskSeq<'T> -> TaskSeq<'T>

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,43 @@ module internal TaskSeqInternal =
896896
raiseOutOfBounds (nameof index)
897897
}
898898

899+
let removeAt index (source: TaskSeq<'T>) =
900+
if index < 0 then
901+
raiseCannotBeNegative (nameof index)
902+
903+
taskSeq {
904+
let mutable i = 0
905+
906+
for item in source do
907+
if i <> index then
908+
yield item
909+
910+
i <- i + 1
911+
912+
// cannot remove past end of sequence
913+
if i <= index then
914+
raiseOutOfBounds (nameof index)
915+
}
916+
917+
let removeManyAt index count (source: TaskSeq<'T>) =
918+
if index < 0 then
919+
raiseCannotBeNegative (nameof index)
920+
921+
taskSeq {
922+
let mutable i = 0
923+
let indexEnd = index + count
924+
925+
for item in source do
926+
if i < index || i >= indexEnd then
927+
yield item
928+
929+
i <- i + 1
930+
931+
// cannot remove past end of sequence
932+
if i <= index then
933+
raiseOutOfBounds (nameof index)
934+
}
935+
899936
// Consider turning using an F# version of this instead?
900937
// https://github.com/i3arnon/ConcurrentHashSet
901938
type ConcurrentHashSet<'T when 'T: equality>(ct) =

0 commit comments

Comments
 (0)