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 toNel syntax to List #520

Merged
merged 1 commit into from
Sep 10, 2015
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
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)
}
}