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

Scala example source code file (PartialFunctionTest.scala)

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

arrow, int, list, partialfunctiontest, speclite

The PartialFunctionTest.scala Scala example source code

package scalaz
package std

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

object PartialFunctionTest extends SpecLite {
  private type PF[-A, +B] = PartialFunction[A, B]

  private val A = Arrow[PartialFunction]

  private val pfnegonly: PF[Int, Int] = {case a if a < 0 => 0 - a}
  private val pfposonly: PF[Int, Int] = {case a if a >= 0 => 0 - a}

  "arrow equivalence" ! forAll {
    (num: Int, num2: Int) =>
      val nums = (num, num2)
      A.split(A.id[Int], A.id[Int]).isDefinedAt(nums) must_===(true)
      A.split(A.id[Int], A.id[Int])(nums) must_===(nums)
      val definedness = for {
        l <- List(pfnegonly, pfposonly)
        r <- List(pfnegonly, pfposonly)
      } yield (A.split(l, r), A.split(l, r) isDefinedAt (nums))
      (definedness.find(_._2).get._1(nums)
       must_===((0 - num, 0 - num2)))
  }
}

Other Scala examples (source code examples)

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