Skip to content

Commit b641aa6

Browse files
committed
Implement TaskSeq.updateAt
1 parent 391f438 commit b641aa6

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/FSharp.Control.TaskSeq/TaskSeq.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ type TaskSeq private () =
319319
static member insertManyAt index values source = Internal.insertAt index (Many values) source
320320
static member removeAt index source = Internal.removeAt index source
321321
static member removeManyAt index count source = Internal.removeManyAt index count source
322+
static member updateAt index value source = Internal.updateAt index value source
322323

323324
static member except itemsToExclude source = Internal.except itemsToExclude source
324325
static member exceptOfSeq itemsToExclude source = Internal.exceptOfSeq itemsToExclude source

src/FSharp.Control.TaskSeq/TaskSeq.fsi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,3 +1326,15 @@ type TaskSeq =
13261326
/// <exception cref="T:ArgumentNullException">Thrown when the input task sequence is null.</exception>
13271327
/// <exception cref="T:ArgumentException">Thrown when index is below 0 or greater than source length.</exception>
13281328
static member removeManyAt: index: int -> count: int -> source: TaskSeq<'T> -> TaskSeq<'T>
1329+
1330+
/// <summary>
1331+
/// Return a new task sequence with the item at a given index set to the new value.
1332+
/// </summary>
1333+
///
1334+
/// <param name="index">The index of the item to be replaced.</param>
1335+
/// <param name="value">The new value.</param>
1336+
/// <param name="source">The input task sequence.</param>
1337+
/// <returns>The result task sequence.</returns>
1338+
/// <exception cref="T:ArgumentNullException">Thrown when the input task sequence is null.</exception>
1339+
/// <exception cref="T:ArgumentException">Thrown when index is below 0 or greater than source length.</exception>
1340+
static member updateAt: index: int -> value: 'T -> source: TaskSeq<'T> -> TaskSeq<'T>

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,26 @@ module internal TaskSeqInternal =
933933
raiseOutOfBounds (nameof index)
934934
}
935935

936+
let updateAt index value (source: TaskSeq<'T>) =
937+
if index < 0 then
938+
raiseCannotBeNegative (nameof index)
939+
940+
taskSeq {
941+
let mutable i = 0
942+
943+
for item in source do
944+
if i <> index then // most common scenario on top (cpu prediction)
945+
yield item
946+
else
947+
yield value
948+
949+
i <- i + 1
950+
951+
// cannot update past end of sequence
952+
if i <= index then
953+
raiseOutOfBounds (nameof index)
954+
}
955+
936956
// Consider turning using an F# version of this instead?
937957
// https://github.com/i3arnon/ConcurrentHashSet
938958
type ConcurrentHashSet<'T when 'T: equality>(ct) =

0 commit comments

Comments
 (0)