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

Prepend a Vector to a NonEmptyVector #3362

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 prependVec[AA >: A](vector: Vector[AA]): NonEmptyVector[AA] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of naming this prependVector for consistency, on the model of the existing prependLazyList and prependChain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Happy to rename it so it's consistent with similar constructs. 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks much!

NonEmptyVector.fromVector(vector).fold[NonEmptyVector[AA]](this)(_.concatNev(this))

/**
* 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("prependVec with a NonEmptyVector is the same as concatNec") {
forAll { (nonEmptyVector1: NonEmptyVector[Int], nonEmptyVector2: NonEmptyVector[Int]) =>
nonEmptyVector2.prependVec(nonEmptyVector1.toVector) should ===(nonEmptyVector1.concatNev(nonEmptyVector2))
}
}

test("prependVec with an empty Vector is the same as the original NonEmptyVector") {
forAll { (nonEmptyVector: NonEmptyVector[Int]) =>
nonEmptyVector.prependVec(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