Skip to content
This repository

Fixed Validation's Monoid instance. #130

Merged
merged 1 commit into from over 1 year ago

2 participants

George Leontiev Richard Wallace
George Leontiev

Followup to #129.
I've added a check for monoid laws to Validation's tests, and found out, the identity element is wrong. Which resulted in things like:

scala> List(1.success[String], 2.success[String]).suml
res12: scalaz.Validation[String,Int] = Failure()

scala> List(1.success[String], 2.success[String]).concatenate
res13: scalaz.Validation[String,Int] = Failure()

So, I've fixed it. This is how it is now:

scala> List(1.success[String], 2.success[String]).suml
res0: scalaz.Validation[String,Int] = Success(3)

scala> List(1.success[String], 2.success[String]).concatenate
res1: scalaz.Validation[String,Int] = Success(3)
Richard Wallace purefn merged commit a6ae78b into from August 10, 2012
Richard Wallace purefn closed this August 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 10, 2012
George Leontiev Fixed Validation's Monoid instance. 90aed43
This page is out of date. Refresh to see the latest.
4  core/src/main/scala/scalaz/Validation.scala
@@ -346,12 +346,12 @@ trait ValidationInstances0 extends ValidationInstances1 {
346 346
       f1 compare f2
347 347
   }
348 348
 
349  
-  implicit def ValidationMonoid[E: Monoid, A: Semigroup]: Monoid[Validation[E, A]] =
  349
+  implicit def ValidationMonoid[E: Semigroup, A: Monoid]: Monoid[Validation[E, A]] =
350 350
     new Monoid[Validation[E, A]] {
351 351
       def append(a1: Validation[E, A], a2: => Validation[E, A]) =
352 352
         a1 +++ a2
353 353
       def zero =
354  
-        Failure(Monoid[E].zero)
  354
+        Success(Monoid[A].zero)
355 355
     }
356 356
 }
357 357
 
1  tests/src/test/scala/scalaz/ValidationTest.scala
@@ -11,6 +11,7 @@ class ValidationTest extends Spec {
11 11
   type ValidationInt[A] = Validation[Int, A]
12 12
 
13 13
   checkAll("Validation", semigroup.laws[ValidationInt[Int]])
  14
+  checkAll("Validation", monoid.laws[ValidationInt[Int]])
14 15
   checkAll("Validation", plus.laws[ValidationInt])
15 16
   checkAll("Validation", applicative.laws[ValidationInt])
16 17
   checkAll("Validation", traverse.laws[ValidationInt])
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.