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

Scala example source code file (BigDecimal.scala)

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

bigdecimal, bigdecimalinstances, bigdecimalmultiplicationnewtype, enum, int, monoid, multiplication, none, show

The BigDecimal.scala Scala example source code

package scalaz
package std
package math

trait BigDecimalInstances {
  implicit val bigDecimalInstance: Monoid[BigDecimal] with Enum[BigDecimal] with Show[BigDecimal] = new Monoid[BigDecimal] with Enum[BigDecimal] with Show[BigDecimal] {
    override def shows(f: BigDecimal) = f.toString

    def append(f1: BigDecimal, f2: => BigDecimal) = f1 + f2

    def zero = 0L

    def order(x: BigDecimal, y: BigDecimal) = if (x < y) Ordering.LT else if (x == y) Ordering.EQ else Ordering.GT

    def succ(b: BigDecimal) = b + 1
    def pred(b: BigDecimal) = b - 1
    override def succn(a: Int, b: BigDecimal) = b + a
    override def predn(a: Int, b: BigDecimal) = b - a
    override def min = None
    override def max = None
  }

  import Tags.Multiplication

  implicit val BigDecimalMultiplicationNewType: Monoid[BigDecimal @@ Multiplication] = new Monoid[BigDecimal @@ Multiplication] {
    def append(f1: BigDecimal @@ Multiplication, f2: => BigDecimal @@ Multiplication) = Multiplication(Tag.unwrap(f1) * Tag.unwrap(f2))

    def zero = Multiplication(1)
  }
}

object bigDecimal extends BigDecimalInstances

Other Scala examples (source code examples)

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