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

Scala example source code file (MonoidTest.scala)

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

cord, endo, foldable, int, kleisli, list, monoid, monoidtest, none, num, some, speclite, vector

The MonoidTest.scala Scala example source code

package scalaz

import std.AllInstances._
import org.scalacheck.Prop.forAll

object MonoidTest extends SpecLite {
  "multiply" ! forAll{ (a: Int, b: Int) =>
    if(b <= 0) {
      Monoid[Int].multiply(a, b) must_=== 0
    } else {
      Monoid[Int].multiply(a, b) must_=== (a * b)
    }
  }

  "endo multiply" in {
    import syntax.monoid._

    def inc(i: Int) = i + 1

    val incTimesThree: Endo[Int] = Endo(inc).multiply(3)
    incTimesThree(0) must_===(3)
  }

  "endo kleisli multiply" in {
    import syntax.monoid._

    val k = Kleisli { i: Int => if (i % 2 == 0) Some(i * 2) else None }

    val kTimes3 = k.endo.multiply(3)
    kTimes3.run(2) must_=== (Some(16))
  }

  "unfold" in {
    val ss = std.stream.unfold(1) {
      case x if x < 10 => Some((x.toString, x * 2))
      case _           => None
    }
    ss.toList must_===(List("1", "2", "4", "8"))
  }

  "intercalate empty" in (
    Foldable[List].intercalate(List[String](), "oops")
    must_===("")
  )

  "intercalate" in {
    val xs = List(Vector(Cord("this"), Cord("has")), Vector(),
		  Vector(Cord("elements")), Vector(Cord("beneath")), Vector())
    ((Foldable[List] compose Foldable[Vector]).intercalate(xs, Cord("!!")).toString
     must_===(Cord("this!!has!!elements!!beneath").toString))
  }

  "invariant functor" in {
    import InvariantFunctorTest._
    import syntax.invariantFunctor._

    val sg: Monoid[Num] = Monoid[Int].xmap[Num](Num.apply _, _.x)
    sg.append(Num(1), Num(2)) must_===(Num(3))
    sg.zero must_===(Num(0))
  }
}

Other Scala examples (source code examples)

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