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

append and prepend on NonEmptyVectors #1356

Merged
merged 1 commit into from
Sep 5, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ final class NonEmptyVector[A] private (val toVector: Vector[A]) extends AnyVal {
*/
def concatNev(other: NonEmptyVector[A]): NonEmptyVector[A] = new NonEmptyVector(toVector ++ other.toVector)

/**
* Append an item to this, producing a new `NonEmptyVector`.
*/
def append(a: A): NonEmptyVector[A] = new NonEmptyVector(toVector :+ a)

/**
* Alias for [[append]]
*/
def :+(a: A): NonEmptyVector[A] = append(a)

/**
* Prepend an item to this, producing a new `NonEmptyVector`.
*/
def prepend(a: A): NonEmptyVector[A] = new NonEmptyVector(a +: toVector)

/**
* Alias for [[prepend]]
*/
def +:(a: A): NonEmptyVector[A] = prepend(a)

/**
* Find the first element matching the predicate, if one exists
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyVectorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@ class NonEmptyVectorTests extends CatsSuite {
}
}

test(":+ is consistent with concat") {
forAll { (nonEmptyVector: NonEmptyVector[Int], i: Int) =>
nonEmptyVector :+ i should === (nonEmptyVector.concat(Vector(i)))
}
}
test("append is consistent with :+") {
forAll { (nonEmptyVector: NonEmptyVector[Int], i: Int) =>
nonEmptyVector append i should === (nonEmptyVector :+ i)
}
}

test("+: is consistent with concatNev") {
forAll { (nonEmptyVector: NonEmptyVector[Int], i: Int) =>
i +: nonEmptyVector should === (NonEmptyVector.of(i).concatNev(nonEmptyVector))
}
}
test("prepend is consistent with +:") {
forAll { (nonEmptyVector: NonEmptyVector[Int], i: Int) =>
nonEmptyVector prepend i should === (i +: 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