Open charpov opened 3 months ago
For my own usage, I ended up with this implementation:
private val none = (_: Any) => None
extension [A, CC[_]](seq: SeqOps[A, CC, CC[A]])
def deleted(i: Int): CC[A] = updatedWith(i)(none)
def updatedWith[B >: A](i: Int)(f: A => Option[B]): CC[B] =
seq.iterableFactory.from:
if i < 0 then seq
else
val (left, right) = seq.view.splitAt(i)
if right.isEmpty then seq
else if f eq none then left ++ right.tail
else left ++ f(seq(i)) ++ right.tail
This has the benefit over patch
that seq.deleted(i) eq seq
when i >= seq.length
(for common Seq
types).
I recently found myself writing these extensions (with help from the Scala Users forum):
to be used as:
Someone there suggested this might make sense to be part of the
Seq
operations in a future version of the standard library. I was actually surprised to find them missing, especially since immutable maps do have anupdatedWith
method. I understand that sequences are not maps, but would there be much cost to add the methods toSeqOps
with a default implementation based onpatch
?