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

Scala example source code file (LazyOptionTest.scala)

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

align, bindrec, cobind, equal, int, isempty, lazyoption, lazyoptiontest, monadplus, monoid, semigroup, speclite, traverse, zip

The LazyOptionTest.scala Scala example source code

package scalaz

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

object LazyOptionTest extends SpecLite {
  checkAll(equal.laws[LazyOption[Int]])
  checkAll(bindRec.laws[LazyOption])
  checkAll(monadPlus.strongLaws[LazyOption])
  checkAll(cobind.laws[LazyOption])
  checkAll(traverse.laws[LazyOption])
  checkAll(zip.laws[LazyOption])
  checkAll(align.laws[LazyOption])
  checkAll(isEmpty.laws[LazyOption])
  checkAll(monoid.laws[LazyOption[Int]])

  "monoid" ! forAll { (a: LazyOption[Int], b: LazyOption[Int]) =>
    Monoid[LazyOption[Int]].append(a, b).toOption must_=== Monoid[Option[Int]].append(a.toOption, b.toOption)
  }

  "tail recursive tailrecM" in {
    val times = 10000

    val result =
      BindRec[LazyOption].tailrecM[Int, Int] {
        i => LazyOption.lazySome(if (i < 10000) \/.left(i + 1) else \/.right(i))
      }(0)
    result.getOrElse(0) must_=== times
  }

  object instances {
    def equal[A: Equal] = Equal[LazyOption[A]]
    def monadPlus = MonadPlus[LazyOption]
    def bindrec = BindRec[LazyOption]
    def cobind = Cobind[LazyOption]
    def traverse = Traverse[LazyOption]
    def zip = Zip[LazyOption]
    def align = Align[LazyOption]
    def isEmpty = IsEmpty[LazyOption]
    def monoid[A: Semigroup] = Monoid[LazyOption[A]]
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala LazyOptionTest.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.