Skip to content

Add Seq.splitAround #187

Open
Open
@erikvanoosten

Description

@erikvanoosten

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions