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 syntax for validated. #587

Merged
merged 1 commit into from
Oct 27, 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
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 @@ -30,3 +30,4 @@ trait AllSyntax
with StrongSyntax
with TraverseSyntax
with XorSyntax
with ValidatedSyntax
6 changes: 5 additions & 1 deletion core/src/main/scala/cats/syntax/option.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package syntax

import cats.data.Xor
import cats.data.{ Xor, Validated, ValidatedNel }

trait OptionSyntax {
def none[A] = Option.empty[A]
Expand All @@ -16,5 +16,9 @@ class OptionIdOps[A](val a: A) extends AnyVal {
class OptionOps[A](val oa: Option[A]) extends AnyVal {
def toLeftXor[B](b: => B): A Xor B = oa.fold[A Xor B](Xor.Right(b))(Xor.Left(_))
def toRightXor[B](b: => B): B Xor A = oa.fold[B Xor A](Xor.Left(b))(Xor.Right(_))
def toInvalid[B](b: => B): Validated[A, B] = oa.fold[Validated[A, B]](Validated.Valid(b))(Validated.Invalid(_))
def toInvalidNel[B](b: => B): ValidatedNel[A, B] = oa.fold[ValidatedNel[A, B]](Validated.Valid(b))(Validated.invalidNel(_))
def toValid[B](b: => B): Validated[B, A] = oa.fold[Validated[B, A]](Validated.Invalid(b))(Validated.Valid(_))
def toValidNel[B](b: => B): ValidatedNel[B, A] = oa.fold[ValidatedNel[B, A]](Validated.invalidNel(b))(Validated.Valid(_))
def orEmpty(implicit A: Monoid[A]): A = oa.getOrElse(A.empty)
}
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ package object syntax {
object strong extends StrongSyntax
object traverse extends TraverseSyntax
object xor extends XorSyntax
object validated extends ValidatedSyntax
}
15 changes: 15 additions & 0 deletions core/src/main/scala/cats/syntax/validated.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cats
package syntax

import cats.data.{ Validated, ValidatedNel }

trait ValidatedSyntax {
implicit def validatedIdSyntax[A](a: A): ValidatedIdSyntax[A] = new ValidatedIdSyntax(a)
}

class ValidatedIdSyntax[A](val a: A) extends AnyVal {
def valid[B]: Validated[B, A] = Validated.Valid(a)
def validNel[B]: ValidatedNel[B, A] = Validated.Valid(a)
def invalid[B]: Validated[A, B] = Validated.Invalid(a)
def invalidNel[B]: ValidatedNel[A, B] = Validated.invalidNel(a)
}