Skip to content
This repository has been archived by the owner on Jan 25, 2018. It is now read-only.

Adds 'sequence' for Map[A, ErrorOr[B]] #43

Merged
merged 1 commit into from
Sep 11, 2017
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
13 changes: 12 additions & 1 deletion src/main/scala/lenthall/validation/ErrorOr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package lenthall.validation
import cats.data.Validated.{Invalid, Valid}
import cats.data.{NonEmptyList, Validated}
import cats.syntax.apply._
import cats.syntax.traverse._
import cats.instances.list._

object ErrorOr {
type ErrorOr[A] = Validated[NonEmptyList[String], A]
Expand All @@ -16,11 +18,20 @@ object ErrorOr {
def flatMap[B](f: A => ErrorOr[B]): ErrorOr[B] = {
fa match {
case Valid(v) => f(v)
case i@Invalid(_) => i
case i @ Invalid(_) => i
}
}
}

implicit class MapErrorOrRhs[A, B](val m: Map[A, ErrorOr[B]]) extends AnyVal {
def sequence: ErrorOr[Map[A, B]] = m.traverseValues(identity)
}

implicit class MapTraversal[A, B](val m: Map[A, B]) extends AnyVal {
def traverse[C,D](f: ((A,B)) => ErrorOr[(C,D)]): ErrorOr[Map[C,D]] = m.toList.traverse(f).map(_.toMap)
def traverseValues[C](f: B => ErrorOr[C]): ErrorOr[Map[A,C]] = m.traverse { case (a, b) => f(b).map(c => (a,c)) }
}

// Note! See the bottom of this file for a generator function for 2 through 22 of these near-identical ShortCircuitingFlatMapTupleNs...

implicit class ShortCircuitingFlatMapTuple1[A](val t1: Tuple1[ErrorOr[A]]) extends AnyVal {
Expand Down
16 changes: 16 additions & 0 deletions src/test/scala/lenthall/validation/ErrorOrSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lenthall.validation

import cats.data.NonEmptyList
import cats.data.Validated.{Invalid, Valid}
import cats.data.Validated.Valid
import cats.syntax.validated._
import lenthall.validation.ErrorOr._
Expand All @@ -25,6 +27,20 @@ class ErrorOrSpec extends FlatSpec with Matchers {
errorMapped should be("hello".invalidNel)
}

it should "sequence a map of valid ErrorOrs" in {
val eo1 = Valid("good1")
val eo2 = Valid("good2")
Map(1 -> eo1, 2 -> eo2).sequence should be(Valid(Map(1 -> "good1", 2 -> "good2")))
}

it should "sequence a map of mixed ErrorOrs" in {
val eo1 = Valid("good1")
val eo2 = "bad2".invalidNel
val eo3 = Valid("good3")
val eo4 = "bad4".invalidNel
Map(1 -> eo1, 2 -> eo2, 3 -> eo3, 4 -> eo4).sequence should be(Invalid(NonEmptyList("bad2", List("bad4"))))
}

val DivBy0Error: String = "Divide by 0!"
def errorOrDiv(v1: Int, v2: Int): ErrorOr[Double] = if (v2 != 0) { Valid(v1.toDouble / v2.toDouble) } else { DivBy0Error.invalidNel }
def errorOrDiv(v1: Double, v2: Int): ErrorOr[Double] = if (v2 != 0) { Valid(v1.toDouble / v2.toDouble) } else { DivBy0Error.invalidNel }
Expand Down