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

Scala example source code file (BigInt.scala)

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

bigint, bigints, enum, monoid, multiplication, none, ordering, show

The BigInt.scala Scala example source code

package scalaz
package std
package math

trait BigInts {
  implicit val bigIntInstance: Monoid[BigInt] with Enum[BigInt] with Show[BigInt] = new Monoid[BigInt] with Enum[BigInt] with Show[BigInt] {
    override def shows(f: BigInt) = f.toString

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

    def zero: BigInt = 0L

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

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

  import Tags.Multiplication

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

    def append(f1: BigInt @@ Multiplication, f2: => BigInt @@ Multiplication): BigInt @@ Multiplication = Multiplication(Tag.unwrap(f1) * Tag.unwrap(f2))

    def zero: BigInt @@ Multiplication = Multiplication(1)

    def order(x: BigInt @@ Multiplication, y: BigInt @@ Multiplication): Ordering = if (Tag.unwrap(x) < Tag.unwrap(y)) Ordering.LT else if (Tag.unwrap(x) == Tag.unwrap(y)) Ordering.EQ else Ordering.GT
  }
}

object bigInt extends BigInts {

}

Other Scala examples (source code examples)

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