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

Scala example source code file (OptionTTest.scala)

This example Scala source code file (OptionTTest.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

apply, bindrec, foldable, functor, int, monad, monaderror, optiont, optionteither, optiontlist, speclite, traverse

The OptionTTest.scala Scala example source code

package scalaz

import scalaz.scalacheck.ScalazProperties._
import scalaz.scalacheck.ScalazArbitrary._
import std.AllInstances._
import org.scalacheck.Prop.forAll

object OptionTTest extends SpecLite {

  type OptionTList[A] = OptionT[List, A]
  type OptionTOption[A] = OptionT[Option, A]
  type IntOr[A] = Int \/ A
  type OptionTEither[A] = OptionT[IntOr, A]

  checkAll(equal.laws[OptionTList[Int]])
  checkAll(bindRec.laws[OptionTList])
  checkAll(monadPlus.laws[OptionTList])
  checkAll(traverse.laws[OptionTList])
  checkAll(monadError.laws[OptionTEither, Int])

  "show" ! forAll { a: OptionTList[Int] =>
    Show[OptionTList[Int]].show(a) must_=== Show[List[Option[Int]]].show(a.run)
  }

  "optionT" ! forAll { ass: List[Option[Int]] =>
      OptionT.optionT(ass).run == ass
  }

  "listT" ! forAll { a: OptionTList[Int] => a.toListT.run must_=== a.run.map(_.toList)}

  object instances {
    def functor[F[_] : Functor] = Functor[OptionT[F, ?]]
    def bindRec[F[_] : Monad: BindRec] = BindRec[OptionT[F, ?]]
    def monad[F[_] : Monad] = MonadPlus[OptionT[F, ?]]
    def monadError[F[_], E](implicit F: MonadError[F, E]) = MonadError[OptionT[F, ?], E]
    def foldable[F[_] : Foldable] = Foldable[OptionT[F, ?]]
    def traverse[F[_] : Traverse] = Traverse[OptionT[F, ?]]

    // checking absence of ambiguity
    def functor[F[_] : Monad] = Functor[OptionT[F, ?]]
    def functor[F[_] : Monad: BindRec] = Functor[OptionT[F, ?]]
    def functor[F[_] : Monad : Traverse] = Functor[OptionT[F, ?]]
    def functor[F[_], E](implicit F1: MonadError[F, E], F2: Traverse[F]) = Functor[OptionT[F, ?]]
    def bind[F[_] : Monad: BindRec] = Bind[OptionT[F, ?]]
    def apply[F[_] : Monad] = Apply[OptionT[F, ?]]
    def foldable[F[_] : Traverse] = Foldable[OptionT[F, ?]]
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.