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

added Convenience functions for Ior NonEmptyChain #2522

Merged
merged 1 commit into from
Sep 25, 2018
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
7 changes: 6 additions & 1 deletion core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable {
)
}

object Ior extends IorInstances with IorFunctions {
object Ior extends IorInstances with IorFunctions with IorFunctions2 {
final case class Left[+A](a: A) extends (A Ior Nothing)
final case class Right[+B](b: B) extends (Nothing Ior B)
final case class Both[+A, +B](a: A, b: B) extends (A Ior B)
Expand Down Expand Up @@ -354,3 +354,8 @@ private[data] sealed trait IorFunctions {
case Right(b) => right(b)
}
}

private[data] sealed trait IorFunctions2{
def leftNec[A, B](a: A): IorNec[A, B] = Ior.left(NonEmptyChain.one(a))
def bothNec[A, B](a: A, b: B): IorNec[A, B] = Ior.both(NonEmptyChain.one(a), b)
}
14 changes: 13 additions & 1 deletion tests/src/test/scala/cats/tests/IorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests

import cats.kernel.laws.discipline.SemigroupTests
import cats.laws.discipline.{BifunctorTests, BitraverseTests, SemigroupalTests, MonadErrorTests, SerializableTests, TraverseTests}
import cats.data.{Ior, NonEmptyList, EitherT}
import cats.data.{Ior,NonEmptyChain, NonEmptyList, EitherT}
import cats.laws.discipline.arbitrary._
import org.scalacheck.Arbitrary._

Expand Down Expand Up @@ -221,12 +221,24 @@ class IorSuite extends CatsSuite {
}
}

test("leftNec") {
forAll { (x: String) =>
Ior.leftNec(x).left should === (Some(NonEmptyChain.one(x)))
}
}

test("bothNel") {
forAll { (x: Int, y: String) =>
Ior.bothNel(y, x).onlyBoth should === (Some((NonEmptyList.one(y), x)))
}
}

test("bothNec") {
forAll { (x: Int, y: String) =>
Ior.bothNec(y, x).onlyBoth should === (Some((NonEmptyChain.one(y), x)))
}
}

test("getOrElse consistent with Option getOrElse") {
forAll { (x: Int Ior String, default: String) =>
x.getOrElse(default) should === (x.toOption.getOrElse(default))
Expand Down