Skip to content

Commit

Permalink
Merge pull request #1356 from benhutchison/topic/nevappend
Browse files Browse the repository at this point in the history
append and prepend on NonEmptyVectors
  • Loading branch information
non authored Sep 5, 2016
2 parents ae51ce5 + d832c40 commit 10b80df
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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

0 comments on commit 10b80df

Please sign in to comment.