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

Scala example source code file (DListTest.scala)

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

bindrec, dlist, dlisttest, equal, int, isempty, monadplus, monoid, show, some, speclite, traverse, zip

The DListTest.scala Scala example source code

package scalaz

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

object DListTest extends SpecLite {

  checkAll(equal.laws[DList[Int]])
  checkAll(monoid.laws[DList[Int]])
  checkAll(zip.laws[DList])
  checkAll(traverse.laws[DList])
  checkAll(isEmpty.laws[DList])
  checkAll(bindRec.laws[DList])
  checkAll(monadPlus.strongLaws[DList])

  "DList append" ! ((0 to 100000).foldLeft(DList[Int]())(_ :+ _).toList must_== (0 to 100000).toList)

  "headOption, tailOption" ! forAll { (n: Int, d: DList[Int]) =>

    // Defined when appropriate?
    val nonempty = d.uncons(false, (_, _) => true)
    d.headOption.isDefined must_=== nonempty
    d.tailOption.isDefined must_=== nonempty

    // If defined, are values correct?
    val d0 = n +: d
    check(d0.headOption === Some(n)) // no Show instance, can't use must_===
    check(d0.tailOption === Some(d))

  }

  object instances {
    def equal[A: Equal] = Equal[DList[A]]
    def monoid[A] = Monoid[DList[A]]
    def monadPlus = MonadPlus[DList]
    def bindrec = BindRec[DList]
    def traverse = Traverse[DList]
    def zip = Zip[DList]
    def isEmpty = IsEmpty[DList]
  }
}

Other Scala examples (source code examples)

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