File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed
src/FSharp.Control.TaskSeq Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 >
Original file line number Diff line number Diff 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 ) =
You can’t perform that action at this time.
0 commit comments