Skip to content

Commit

Permalink
added Convenience functions for Ior NonEmptyChain (#2522)
Browse files Browse the repository at this point in the history
  • Loading branch information
binkabir authored and kailuowang committed Sep 25, 2018
1 parent c5181ca commit e093bbd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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

0 comments on commit e093bbd

Please sign in to comment.