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

Scala example source code file (BigInteger.scala)

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

biginteger, bigintegerinstances, enum, math, monoid, multiplication, none, order, show

The BigInteger.scala Scala example source code

package scalaz
package std.java.math

import java.math.BigInteger

trait BigIntegerInstances {
  implicit val bigIntegerInstance: Monoid[BigInteger] with Enum[BigInteger] with Show[BigInteger] = new Monoid[BigInteger] with Enum[BigInteger] with Show[BigInteger] {
    override def shows(f: BigInteger) = f.toString

    def append(f1: BigInteger, f2: => BigInteger) = f1 add f2

    def zero = BigInteger.ZERO

    def succ(b: BigInteger) = b add BigInteger.ONE
    def pred(b: BigInteger) = b subtract BigInteger.ONE
    override def succn(a: Int, b: BigInteger) = b add BigInteger.valueOf(a)
    override def predn(a: Int, b: BigInteger) = b subtract BigInteger.valueOf(a)
    override def min = None
    override def max = None

    def order(x: BigInteger, y: BigInteger) = x.compareTo(y) match {
      case x if x < 0   => Ordering.LT
      case x if x == 0 => Ordering.EQ
      case x if x > 0   => Ordering.GT
    }
  }

  import Tags.Multiplication

  implicit val bigIntegerMultiplication: Monoid[BigInteger @@ Multiplication] with Order[BigInteger @@ Multiplication] with Show[BigInteger @@ Multiplication] = new Monoid[BigInteger @@ Multiplication] with Order[BigInteger @@ Multiplication] with Show[BigInteger @@ Multiplication] {
    override def shows(f: scalaz.@@[BigInteger, Multiplication]) = f.toString

    def append(f1: BigInteger @@ Multiplication, f2: => BigInteger @@ Multiplication) = Multiplication(Tag.unwrap(f1) multiply Tag.unwrap(f2))

    def zero: BigInteger @@ Multiplication = Multiplication(BigInteger.ONE)

    def order(x: BigInteger @@ Multiplication, y: BigInteger @@ Multiplication) = Tag.unwrap(x).compareTo(Tag.unwrap(y)) match {
      case x if x < 0   => Ordering.LT
      case x if x == 0 => Ordering.EQ
      case x if x > 0   => Ordering.GT
    }
  }
}

object bigInteger extends BigIntegerInstances {

}

Other Scala examples (source code examples)

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