# Deleting/replacing inside sequences

**URL:** <https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726>\
**Category:** Scala Standard Library\
**Created:** [August 13, 2024, 7:37pm UTC](https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726 "2024-08-13T19:37:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![charpov](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/charpov/32/1983_2.png) [@charpov](https://contributors.scala-lang.org/u/charpov)\
**Post date:** [August 13, 2024, 7:37pm UTC](https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726/1 "2024-08-13T19:37:09Z")

</div>

I recently found myself writing these extensions (with help from the Scala Users forum):

```scala
extension [A, CC[_]](seq: SeqOps[A, CC, CC[A]])
   def deleted(i: Int): CC[A] = seq.patch(i, Nil, 1)
   def updatedWith[B >: A](i: Int, f: A => Option[B]): CC[B] = seq.patch(i, f(seq(i)), 1)

```

to be used as:

```scala
List(A, X, C).deleted(1) // List(A, C)
List(A, X, C).updatedWith(1, _ => Some(B)) // List(A, B, C)

```

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 an `updatedWith` method. I understand that sequences are not maps, but would there be much cost to add the methods to `SeqOps` with a default implementation based on `patch`?

---

<div class="post-metadata">

**Author:** ![MateuszKowalewski](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkowalewski/32/77_2.png) [@MateuszKowalewski](https://contributors.scala-lang.org/u/MateuszKowalewski)\
**Post date:** [August 13, 2024, 11:52pm UTC](https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726/2 "2024-08-13T23:52:15Z")

</div>

This looks convenient.

But it has an issue regarding runtime cost: This could be very expensive methods when used on a linear sequence instead of on an indexed one.

But I guess the underlying `patch` has already this issue. So makes not much difference. Just needs to be documented properly I think.

I’ll welcome it.

(Isn’t here some thread about proposals for the next std. lib somewhere? Or was it a issue on GitHub that collects proposals? I think this here belongs on this list.)

---

<div class="post-metadata">

**Author:** ![charpov](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/charpov/32/1983_2.png) [@charpov](https://contributors.scala-lang.org/u/charpov)\
**Post date:** [August 14, 2024, 12:12pm UTC](https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726/3 "2024-08-14T12:12:32Z")

</div>

> [@MateuszKowalewski](#):
>
> But it has an issue regarding runtime cost: This could be very expensive methods when used on a linear sequence instead of on an indexed one.
> 
> But I guess the underlying `patch` has already this issue. So makes not much difference. Just needs to be documented properly I think.

Yes, many of the methods common to all collections have poor performance on some types, something to always be aware of (I keep reminding students). The issue is not so much indexed vs linear but the copying needed (e.g., it will be inefficient even on `immutable.ArraySeq`). Still, it’s no worse than `append`, or `init`, or some of the existing methods.

> [@](#):
>
> (Isn’t here some thread about proposals for the next std. lib somewhere? Or was it a issue on GitHub that collects proposals? I think this here belongs on this list.)

That rings a bell. If someone can post the link, I’ll move my message there.

---

<div class="post-metadata">

**Author:** ![MateuszKowalewski](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkowalewski/32/77_2.png) [@MateuszKowalewski](https://contributors.scala-lang.org/u/MateuszKowalewski)\
**Post date:** [August 14, 2024, 4:23pm UTC](https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726/4 "2024-08-14T16:23:12Z")

</div>

I think it was this here:

> **[GitHub - scala/scala-library-next: backwards-binary-compatible Scala standard...](https://github.com/scala/scala-library-next)**
>
> backwards-binary-compatible Scala standard library additions - scala/scala-library-next

But not sure there is also some other place.

Related:

> [@Re-activate the scala-library-next initiative](https://contributors.scala-lang.org/t/re-activate-the-scala-library-next-initiative/6339):
>
> A couple of years back the [scala-library-next](https://github.com/scala/scala-library-next) repository / project / initiative was started. And, if my memory serves me correctly, during the first 6 months or so, it received a lot of traction and was shaping an interesting roadmap for the next release of the Scala standard library. However, probably due to the imminent release of Scala 3, the pandemic, and other factors, the interest in the project slowly faded away. And, as of today, it is basically a ghost repository: that didn’t even man…

---

<div class="post-metadata">

**Author:** ![charpov](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/charpov/32/1983_2.png) [@charpov](https://contributors.scala-lang.org/u/charpov)\
**Post date:** [August 14, 2024, 5:32pm UTC](https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726/5 "2024-08-14T17:32:47Z")

</div>

I added an issue to the “ghost” repository.

---

<div class="post-metadata">

**Author:** ![MateuszKowalewski](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkowalewski/32/77_2.png) [@MateuszKowalewski](https://contributors.scala-lang.org/u/MateuszKowalewski)\
**Post date:** [August 14, 2024, 5:59pm UTC](https://contributors.scala-lang.org/t/deleting-replacing-inside-sequences/6726/6 "2024-08-14T17:59:52Z")

</div>

Let’s see what happens.

I’m also somehow clueless what’s the current state of the lib-next initiative.
