Closed
Description
Similar to Insert operator (same signature) but the position where second
will be inserted at first
is count from end to start. That is,
- 0 means last position,
- 1 means second-last position,
- 2 means third-last position
- etc
Signature:
IEnumerable<T> InsertEnd<T>(this IEnumerable<T> first, IEnumerable<T> second, int index)
Example:
[1,2].InsertEnd([3,4], -1); // ArgumentOutOfRangeException when index < 0
[1,2].InsertEnd([3,4], 0); // [1,2,3,4]
[1,2].InsertEnd([3,4], 1); // [1,3,4,2]
[1,2].InsertEnd([3,4], 2); // [3,4,1,2]
[1,2].InsertEnd([3,4], 3); // ArgumentOutOfRangeException when index > first.Count()
workaround
first.SkipLast(index).Concat(second).Concat(first.TakeLast(index))
produces the same as
first.InsertEnd(second, index)
except for exception cases with index
parameter
Motivation
InsertEnd
iteratesfirst
once,- performance, since workaround iterates
first
2 times and uses 2 buffers (SkipLast
+TakeLast
) Insert
operator symmetry- more readable, workaround does not capture the intention as clearly as
InsertEnd