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 Seq.splitAround #187

Open
erikvanoosten opened this issue Sep 6, 2024 · 2 comments
Open

Add Seq.splitAround #187

erikvanoosten opened this issue Sep 6, 2024 · 2 comments

Comments

@erikvanoosten
Copy link

erikvanoosten commented Sep 6, 2024

This proposes to add Seq.splitAround(separator): (Seq, Seq) which splits a sequence in all items before and after the first item that is equal to the given separator.

SplitAround is useful for a very common string operation for which no handy operator exists. For example:

"bucket/path/prefix".splitAround('/') === ("bucket", "path/prefix")
"key=value".splitAround('=') === ("key", "value")

A simple implementation would be:

trait Seq[A] {
  def splitAround(separator: A): (Seq[A], Seq[A]) = {
    val (before, after) = this.splitAt(this.indexOf(separator))
    (before, after.drop(1))
  }
}

Ideally, the result uses the same concrete type of sequence as the given sequence. E.g. String.splitAround returns a pair of Strings and Vector.splitAround returns a pair of Vectors.

In addition, it would be nice to have a similar splitAroundLast which splits around the last separator value.

(Updated after @ritschwumm 's comment.)

@erikvanoosten
Copy link
Author

This might be a duplicate of #30, but there is no clear definition there so I can not be certain.

@ritschwumm
Copy link

both the first and the last occurance are useful as a split point here in my experience. and it maybe nice to stay with the concrete collection so e.g. Vector.splitAround returns a pair of Vectors.

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

2 participants