alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (BitraverseTest.scala)

This example Scala source code file (BitraverseTest.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

bitraverse, boolean, either, int, l_e_lo, left\-biased, list, option, r_e_ro, right\-biased, string, success, validation, vector

The BitraverseTest.scala Scala example source code

package scalaz

import scalaz.std.AllInstances.{tuple2Instance => _, _}
import scalaz.scalacheck.ScalazProperties._
import scalaz.scalacheck.ScalazArbitrary._

object BitraverseTest extends SpecLite {

  implicit val LE = Bitraverse[\/].leftTraverse[Int]
  implicit val RE = Bitraverse[\/].rightTraverse[Int]

  checkAll("Left-biased Bitraverse for Either",  traverse.laws[? \/ Int])
  checkAll("Right-biased Bitraverse for Either", traverse.laws[Int \/ ?])


  implicit val LT = Bitraverse[Tuple2].leftTraverse[Int]
  implicit val RT = Bitraverse[Tuple2].rightTraverse[Int]

  checkAll("Left-biased Bitraverse for (,)",  traverse.laws[(?, Int)])
  checkAll("Right-biased Bitraverse for (,)", traverse.laws[(Int, ?)])

  "bitraverseU" in {
    import syntax.bitraverse._
    val a: Validation[Int \/ String, Int \/ Boolean] = Success(\/-(true))
    val b = a.bitraverseU(identity, identity)
    val _ = b: (Int \/ Validation[String, Boolean])
    b must_=== \/-(Success(true))
  }

  "bisequenceU" in {
    import syntax.bitraverse._
    val a: Validation[Int \/ String, Int \/ Boolean] = Success(\/-(true))
    val b = a.bisequenceU
    val _ = b: (Int \/ Validation[String, Boolean])
    b must_=== \/-(Success(true))
  }

  "left/right bias" in {
    import scalaz.syntax.either._

    Bitraverse[\/].rightTraverse.traverse(42.left[Int])(x => Vector(x + 3)) must_===(Vector(-\/(42)))
    Bitraverse[\/].leftTraverse.traverse(42.left[Int])(x => Vector(x + 3))  must_===(Vector(-\/(45)))

    Bifoldable[\/].leftFoldable.foldMap(42.left[Int])(identity)  must_===(42)
    Bifoldable[\/].rightFoldable.foldMap(42.left[Int])(identity) must_===(0)
  }

  "both sides, left and right embedding" in {
    implicit val L_E_LO_RL: Traverse[λ[α => Option[α] \/ List[Int]]] = Bitraverse[\/].embed[Option,List].leftTraverse[Int]
    implicit val R_E_LO_RL: Traverse[λ[α => Option[Int] \/ List[α]]] = Bitraverse[\/].embed[Option,List].rightTraverse[Int]
    checkAll("Left-biased Bitraverse for Either[Option,List[_]]", traverse.laws[λ[α => Option[α] \/ List[Int]]])
    checkAll("Right-biased Bitraverse for Either[Option[_],List]", traverse.laws[λ[α => Option[Int] \/ List[α]]])

    implicit val L_E_LO = Bitraverse[\/].embedLeft[Option].leftTraverse[Int]
    implicit val R_E_RO = Bitraverse[\/].embedRight[Option].rightTraverse[Int]
    checkAll("Left-biased Bitraverse for Either[Option,_]", traverse.laws[λ[α => Option[α] \/ Int]])
    checkAll("Right-biased Bitraverse for Either[_,Option]", traverse.laws[λ[α => Int \/ Option[α]]])
  }

}

// vim: expandtab:ts=2:sw=2

Other Scala examples (source code examples)

Here is a short list of links related to this Scala BitraverseTest.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.