Skip to content

Commit

Permalink
Prepend a Vector to a NonEmptyVector (#3362)
Browse files Browse the repository at this point in the history
* Add prependVec

I needed to implement this recently for some work
I was doing and was surprised it didn't already exist.

* Run +prePR

* Fix according to PR recommendations
- Use idiomatic implementation
- Use more consistent naming
  • Loading branch information
ssanj authored Mar 23, 2020
1 parent eba3fcb commit 0bcb6fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])
*/
def prepend[AA >: A](a: AA): NonEmptyVector[AA] = new NonEmptyVector(a +: toVector)

/**
* Prepend a `Vector` to this, producing a new `NonEmptyVector`.
*/
def prependVector[AA >: A](vector: Vector[AA]): NonEmptyVector[AA] =
new NonEmptyVector(vector ++ this.toVector)

/**
* Alias for [[prepend]]
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyVectorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ class NonEmptyVectorSuite extends NonEmptyCollectionSuite[Vector, NonEmptyVector
}
}

test("prependVector with a NonEmptyVector is the same as concatNec") {
forAll { (nonEmptyVector1: NonEmptyVector[Int], nonEmptyVector2: NonEmptyVector[Int]) =>
nonEmptyVector2.prependVector(nonEmptyVector1.toVector) should ===(nonEmptyVector1.concatNev(nonEmptyVector2))
}
}

test("prependVector with an empty Vector is the same as the original NonEmptyVector") {
forAll { (nonEmptyVector: NonEmptyVector[Int]) =>
nonEmptyVector.prependVector(Vector.empty) should ===(nonEmptyVector)
}
}

test("NonEmptyVector#of on varargs is consistent with NonEmptyVector#apply on Vector") {
forAll { (head: Int, tail: Vector[Int]) =>
NonEmptyVector.of(head, tail: _*) should ===(NonEmptyVector(head, tail))
Expand Down

0 comments on commit 0bcb6fb

Please sign in to comment.