Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updatedWith (and deleted) to sequences #186

Open
charpov opened this issue Aug 14, 2024 · 1 comment
Open

Add updatedWith (and deleted) to sequences #186

charpov opened this issue Aug 14, 2024 · 1 comment

Comments

@charpov
Copy link

charpov commented Aug 14, 2024

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

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:

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?

@charpov
Copy link
Author

charpov commented Nov 2, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant