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 toNev to NonEmptyList variants #3423

Merged
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
15 changes: 15 additions & 0 deletions core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,21 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
final def toNes[B >: A](implicit order: Order[B]): NonEmptySet[B] =
NonEmptySet.of(head, tail: _*)

/**
* Creates new `NonEmptyVector`, similarly to List#toVector from scala standard library.
*{{{
* scala> import cats.data._
* scala> import cats.instances.int._
* scala> val nel = NonEmptyLazyList.fromLazyListPrepend(1, LazyList(2,3,4))
* scala> val expectedResult = NonEmptyVector.fromVectorUnsafe(Vector(1,2,3,4))
* scala> val result = nel.toNev
* scala> result === expectedResult
* res0: Boolean = true
*}}}
*/
final def toNev[B >: A]: NonEmptyVector[B] =
NonEmptyVector.fromVectorUnsafe(toLazyList.toVector)

final def show[AA >: A](implicit AA: Show[AA]): String = s"NonEmpty${Show[LazyList[AA]].show(toLazyList)}"
}

Expand Down
15 changes: 15 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,21 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
*/
def toNes[B >: A](implicit order: Order[B]): NonEmptySet[B] =
NonEmptySet.of(head, tail: _*)

/**
* Creates new `NonEmptyVector`, similarly to List#toVector from scala standard library.
*{{{
* scala> import cats.data._
* scala> import cats.instances.int._
* scala> val nel = NonEmptyList(1, List(2,3,4))
* scala> val expectedResult = NonEmptyVector.fromVectorUnsafe(Vector(1,2,3,4))
* scala> val result = nel.toNev
* scala> result === expectedResult
* res0: Boolean = true
*}}}
*/
def toNev[B >: A]: NonEmptyVector[B] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason this one isn't final but the one above is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. NonEmptyList is a final class. LazyNonEmptyList is not.

NonEmptyVector.fromVectorUnsafe(toList.toVector)
}

object NonEmptyList extends NonEmptyListInstances {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats.tests

import cats.{Align, Bimonad, SemigroupK, Show, Traverse}
import cats.data.{NonEmptyLazyList, NonEmptyLazyListOps}
import cats.data.{NonEmptyLazyList, NonEmptyLazyListOps, NonEmptyVector}
import cats.kernel.{Eq, Hash, Order, PartialOrder, Semigroup}
import cats.kernel.laws.discipline.{EqTests, HashTests, OrderTests, PartialOrderTests, SemigroupTests}
import cats.laws.discipline.{AlignTests, BimonadTests, NonEmptyTraverseTests, SemigroupKTests, SerializableTests}
Expand Down Expand Up @@ -142,6 +142,12 @@ class NonEmptyLazyListSuite extends NonEmptyCollectionSuite[LazyList, NonEmptyLa
ci.distinct.toList should ===(ci.toList.distinct)
}
}

test("NonEmptyLazyList#toNev is consistent with List#toVector and creating NonEmptyVector from it") {
forAll { (ci: NonEmptyLazyList[Int]) =>
ci.toNev should ===(NonEmptyVector.fromVectorUnsafe(Vector.empty[Int] ++ ci.toList.toVector))
}
}
}

class ReducibleNonEmptyLazyListSuite extends ReducibleSuite[NonEmptyLazyList]("NonEmptyLazyList") {
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ class NonEmptyListSuite extends NonEmptyCollectionSuite[List, NonEmptyList, NonE
nel.toNes should ===(NonEmptySet.fromSetUnsafe(SortedSet.empty[Int] ++ nel.toList.toSet))
}
}

test("NonEmptyList#toNev is consistent with List#toVector and creating NonEmptyVector from it") {
forAll { (nel: NonEmptyList[Int]) =>
nel.toNev should ===(NonEmptyVector.fromVectorUnsafe(Vector.empty[Int] ++ nel.toList.toVector))
}
}
}

@deprecated("to be able to test deprecated methods", since = "1.0.0-RC1")
Expand Down