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

Add NonEmptyList#prependAll #4267

Merged
merged 16 commits into from
Aug 24, 2022
44 changes: 44 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
def prepend[AA >: A](a: AA): NonEmptyList[AA] =
NonEmptyList(a, head :: tail)

/**
* Alias for [[prependList]]
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of(1, 2, 3)
* scala> val list = List(-1, 0)
* scala> list ++: nel
* res0: cats.data.NonEmptyList[Int] = NonEmptyList(-1, 0, 1, 2, 3)
* }}}
*/
def ++:[AA >: A](other: List[AA]): NonEmptyList[AA] =
prependList(other)

/**
* Prepend another `List`
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of(1, 2, 3)
* scala> val list = List(-1, 0)
* scala> nel.prependList(list)
* res0: cats.data.NonEmptyList[Int] = NonEmptyList(-1, 0, 1, 2, 3)
* }}}
*/
def prependList[AA >: A](other: List[AA]): NonEmptyList[AA] =
other match {
case Nil => this
case head :: tail => NonEmptyList(head, tail ::: toList)
}

/**
* Alias for append
*
Expand All @@ -149,6 +180,19 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
def append[AA >: A](a: AA): NonEmptyList[AA] =
NonEmptyList(head, tail :+ a)

/**
* Alias for [[concat]]
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> val nel = NonEmptyList.of(1, 2, 3)
* scala> nel.appendList(List(4, 5))
* res0: cats.data.NonEmptyList[Int] = NonEmptyList(1, 2, 3, 4, 5)
* }}}
*/
def appendList[AA >: A](other: List[AA]): NonEmptyList[AA] =
concat(other)
saeltz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Alias for concatNel
*
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/data/NonEmptySeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ final class NonEmptySeq[+A] private (val toSeq: Seq[A]) extends AnyVal with NonE
*/
def concat[AA >: A](other: Seq[AA]): NonEmptySeq[AA] = new NonEmptySeq(toSeq ++ other)

/**
* Append another `Seq` to this, producing a new `NonEmptySeq`.
*/
def appendSeq[AA >: A](other: Seq[AA]): NonEmptySeq[AA] = concat(other)
saeltz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Append another `NonEmptySeq` to this, producing a new `NonEmptySeq`.
*/
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])
*/
def concat[AA >: A](other: Vector[AA]): NonEmptyVector[AA] = new NonEmptyVector(toVector ++ other)

/**
* Append another `Vector` to this, producing a new `NonEmptyVector`
*
* {{{
* scala> import cats.data.NonEmptyVector
* scala> val nev = NonEmptyVector.of(1, 2, 3)
* scala> nev.appendVector(Vector(4, 5))
* res0: cats.data.NonEmptyVector[Int] = NonEmptyVector(1, 2, 3, 4, 5)
* }}}
*/
def appendVector[AA >: A](other: Vector[AA]): NonEmptyVector[AA] = concat(other)

/**
* Append another `NonEmptyVector` to this, producing a new `NonEmptyVector`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ class NonEmptyListSuite extends NonEmptyCollectionSuite[List, NonEmptyList, NonE
}
}

test("++: consistent with List#:::") {
forAll { (nel: NonEmptyList[Int], i: List[Int]) =>
assert((i ++: nel).toList === (i ::: nel.toList))
assert(nel.prependList(i).toList === (i ::: nel.toList))
}
}

test("NonEmptyList#distinct is consistent with List#distinct") {
forAll { (nel: NonEmptyList[Int]) =>
assert(nel.distinct.toList === (nel.toList.distinct))
Expand Down Expand Up @@ -378,6 +385,7 @@ class NonEmptyListSuite extends NonEmptyCollectionSuite[List, NonEmptyList, NonE
forAll { (nel: NonEmptyList[Int], l: List[Int], n: Int) =>
assert((nel ++ l).toList === (nel.toList ::: l))
assert(nel.concat(l).toList === (nel.toList ::: l))
assert(nel.appendList(l).toList === (nel.toList ::: l))
assert(nel.concatNel(NonEmptyList(n, l)).toList === (nel.toList ::: (n :: l)))
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/shared/src/test/scala/cats/tests/NonEmptySeqSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.tests

import cats.data.NonEmptySeq
import cats.laws.discipline.arbitrary._
import cats.syntax.seq._
import org.scalacheck.Prop._

import scala.collection.immutable.Seq

class NonEmptySeqSuite extends NonEmptyCollectionSuite[Seq, NonEmptySeq, NonEmptySeq] {
protected def toList[A](value: NonEmptySeq[A]): List[A] = value.toSeq.toList
protected def underlyingToList[A](underlying: Seq[A]): List[A] = underlying.toList
protected def toNonEmptyCollection[A](value: NonEmptySeq[A]): NonEmptySeq[A] = value

test("neSeq => Seq => neSeq returns original neSeq")(
forAll { (fa: NonEmptySeq[Int]) =>
assert(fa.toSeq.toNeSeq == Some(fa))
}
)

test("NonEmptySeq#concat/appendSeq is consistent with Seq#++")(
forAll { (fa: NonEmptySeq[Int], fb: Seq[Int]) =>
assert((fa ++ fb).toSeq == fa.toSeq ++ fb)
assert(fa.concat(fb).toSeq == fa.toSeq ++ fb)
assert(fa.appendSeq(fb).toSeq == fa.toSeq ++ fb)
}
)

test("NonEmptySeq#concatNeSeq is consistent with concat")(
forAll { (fa: NonEmptySeq[Int], fb: NonEmptySeq[Int]) =>
assert(fa.concatNeSeq(fb).toSeq == fa.concat(fb.toSeq).toSeq)
}
)

test("toNeSeq on empty Seq returns None") {
assert(Seq.empty[Int].toNeSeq == None)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ class NonEmptyVectorSuite extends NonEmptyCollectionSuite[Vector, NonEmptyVector
}
}

test("appendVector is consistent with concat") {
forAll { (nonEmptyVector: NonEmptyVector[Int], vector: Vector[Int]) =>
assert(nonEmptyVector.appendVector(vector) === nonEmptyVector.concat(vector))
}
}

test(":+ is consistent with concat") {
forAll { (nonEmptyVector: NonEmptyVector[Int], i: Int) =>
assert(nonEmptyVector :+ i === (nonEmptyVector.concat(Vector(i))))
Expand Down
13 changes: 1 addition & 12 deletions tests/shared/src/test/scala/cats/tests/SeqSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package cats.tests

import cats.{Align, Alternative, CoflatMap, Monad, Semigroupal, Traverse, TraverseFilter}
import cats.data.{NonEmptySeq, ZipSeq}
import cats.data.ZipSeq
import cats.laws.discipline.{
AlignTests,
AlternativeTests,
Expand All @@ -37,7 +37,6 @@ import cats.laws.discipline.{
}
import cats.laws.discipline.arbitrary._
import cats.syntax.show._
import cats.syntax.seq._
import cats.syntax.eq._
import org.scalacheck.Prop._
import scala.collection.immutable.Seq
Expand Down Expand Up @@ -75,16 +74,6 @@ class SeqSuite extends CatsSuite {
}
}

test("neSeq => Seq => neSeq returns original neSeq")(
forAll { (fa: NonEmptySeq[Int]) =>
assert(fa.toSeq.toNeSeq == Some(fa))
}
)

test("toNeSeq on empty Seq returns None") {
assert(Seq.empty[Int].toNeSeq == None)
}

test("traverse is stack-safe") {
val seq = (0 until 100000).toSeq
val sumAll = Traverse[Seq]
Expand Down