Skip to content

Commit

Permalink
Merge pull request #520 from ceedubs/toNel
Browse files Browse the repository at this point in the history
Add toNel syntax to List
  • Loading branch information
non committed Sep 10, 2015
2 parents 271b7be + 2a0f012 commit eb5f431
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ package object data {
F.reduceRightTo(fa)(a => NonEmptyList(a, Nil)) { (a, lnel) =>
lnel.map { case OneAnd(h, t) => OneAnd(a, h :: t) }
}

def fromList[A](la: List[A]): Option[NonEmptyList[A]] =
la match {
case (h :: t) => Some(OneAnd(h, t))
case Nil => None
}
}

type ReaderT[F[_], A, B] = Kleisli[F, A, B]
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ trait AllSyntax
with FunctorSyntax
with GroupSyntax
with InvariantSyntax
with ListSyntax
with MonadCombineSyntax
with MonadFilterSyntax
with OptionSyntax
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/scala/cats/syntax/list.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cats
package syntax

import cats.data.NonEmptyList

trait ListSyntax {
implicit def listSyntax[A](la: List[A]): ListOps[A] = new ListOps(la)
}

final class ListOps[A](val la: List[A]) extends AnyVal {
def toNel: Option[NonEmptyList[A]] = NonEmptyList.fromList(la)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object arbitrary {
implicit def constArbitrary[A, B](implicit A: Arbitrary[A]): Arbitrary[Const[A, B]] =
Arbitrary(A.arbitrary.map(Const[A, B]))

implicit def oneAndArbitrary[F[_], A](implicit A: Arbitrary[A], F: ArbitraryK[F]): Arbitrary[OneAnd[A, F]] =
Arbitrary(F.synthesize[A].arbitrary.flatMap(fa => A.arbitrary.map(a => OneAnd(a, fa))))
implicit def oneAndArbitrary[F[_], A](implicit A: Arbitrary[A], F: Arbitrary[F[A]]): Arbitrary[OneAnd[A, F]] =
Arbitrary(F.arbitrary.flatMap(fa => A.arbitrary.map(a => OneAnd(a, fa))))

implicit def xorArbitrary[A, B](implicit A: Arbitrary[A], B: Arbitrary[B]): Arbitrary[A Xor B] =
Arbitrary(Gen.oneOf(A.arbitrary.map(Xor.left), B.arbitrary.map(Xor.right)))
Expand Down
15 changes: 14 additions & 1 deletion tests/shared/src/test/scala/cats/tests/ListTests.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cats
package tests

import cats.data.NonEmptyList
import cats.laws.discipline.{TraverseTests, CoflatMapTests, MonadCombineTests, SerializableTests}
import cats.laws.discipline.arbitrary._
import org.scalatest.prop.GeneratorDrivenPropertyChecks

class ListTests extends CatsSuite {
class ListTests extends CatsSuite with GeneratorDrivenPropertyChecks {
checkAll("List[Int]", CoflatMapTests[List].coflatMap[Int, Int, Int])
checkAll("CoflatMap[List]", SerializableTests.serializable(CoflatMap[List]))

Expand All @@ -12,4 +15,14 @@ class ListTests extends CatsSuite {

checkAll("List[Int] with Option", TraverseTests[List].traverse[Int, Int, Int, Int, Option, Option])
checkAll("Traverse[List]", SerializableTests.serializable(Traverse[List]))

test("nel => list => nel returns original nel")(
forAll { fa: NonEmptyList[Int] =>
assert(fa.unwrap.toNel == Some(fa))
}
)

test("toNel on empty list returns None"){
assert(List.empty[Int].toNel == None)
}
}

0 comments on commit eb5f431

Please sign in to comment.