Skip to content

Commit

Permalink
Merge pull request #3423 from saraiva132/add-combinators-to-non-empty…
Browse files Browse the repository at this point in the history
…-list

Add toNev to NonEmptyList variants
  • Loading branch information
djspiewak authored May 21, 2020
2 parents 2bb358d + cf92734 commit de8cc76
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
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] =
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

0 comments on commit de8cc76

Please sign in to comment.