Skip to content
main
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Skala

Build Status Build Status Download

Codacy Badge Coverage Status

Build Status

Utilities for Scala

skala.math

skala.math.BigInts

sqrt()

The square root of a BigInt value

import io.kevinlee.skala.math.BigInts._

sqrt(BigInt(9))
// Result: BigDecimal(3)

sqrt(BigInt(10))
// Result: BigDecimal(3.162277660168379331998893544432718)


sqrt(BigInt(-1))
// Result: IllegalArgumentException

findSqrt()

It returns Option may or may not have a square root of a BigInt value. This method doesn't throw any exception when a negative number is passed as a parameter. Instead, it returns None.

import io.kevinlee.skala.math.BigInts._

findSqrt(BigInt(9))
// Result: Some(BigDecimal(3))

findSqrt(BigInt(10))
// Result: Some(BigDecimal(3.162277660168379331998893544432718))


findSqrt(BigInt(-1))
// Result: None

mean()

It calculates a mean of TraversableLike[BigInt, TraversableLike[BigInt, _]].

import io.kevinlee.skala.math.BigInts._

val numbers = List[BigInt](1, 2, 3)
mean(numbers)  // returns BigDecimal(2)

median()

It finds a median of Seq[BigInt].

import io.kevinlee.skala.math.BigInts._

val numbers = List[BigInt](1, 2, 3, 4)
median(numbers)  // return BigDecimal(2.5)
import io.kevinlee.skala.math.BigInts._

val numbers = List[BigInt](2, 4, 3, 1)
median(numbers)  // return BigDecimal(2.5)
import io.kevinlee.skala.math.BigInts._

val numbers = List[BigInt](1, 2, 3, 4, 5)
median(numbers)  // return BigDecimal(3)
import io.kevinlee.skala.math.BigInts._

val numbers = List[BigInt](2, 3, 5, 4, 1)
median(numbers)  // return BigDecimal(3)

skala.math.BigDecimals

sqrt()

The square root of a BigDecimal value

import io.kevinlee.skala.math.BigDecimals._

sqrt(BigDecimal(9))
// Result: BigDecimal(3)

sqrt(BigDecimal(10))
// Result: BigDecimal(3.162277660168379331998893544432718)

sqrt(BigDecimal(-1))
// Result: IllegalArgumentException

findSqrt()

It returns Option may or may not have a square root of a BigDecimal value. This method doesn't throw any exception when a negative number is passed as a parameter. Instead, it returns None.

import io.kevinlee.skala.math.BigDecimals._

findSqrt(BigDecimal(9))
// Result: Some(BigDecimal(3))

findSqrt(BigDecimal(10))
// Result: Some(BigDecimal(3.162277660168379331998893544432718))


findSqrt(BigDecimal(-1))
// Result: None

mean()

It calculates a mean of TraversableLike[BigDecimal, TraversableLike[BigDecimal, _]].

import io.kevinlee.skala.math.BigDecimals._

val numbers = List[BigDecimal](1, 2, 3)
mean(numbers)  // returns BigDecimal(2)

median()

It finds a median of Seq[BigDecimal].

import io.kevinlee.skala.math.BigDecimals._

val numbers = List[BigDecimal](1, 2, 3, 4)
median(numbers)  // return BigDecimal(2.5)
import io.kevinlee.skala.math.BigDecimals._

val numbers = List[BigDecimal](2, 4, 3, 1)
median(numbers)  // return BigDecimal(2.5)
import io.kevinlee.skala.math.BigDecimals._

val numbers = List[BigDecimal](1, 2, 3, 4, 5)
median(numbers)  // return BigDecimal(3)
import io.kevinlee.skala.math.BigDecimals._

val numbers = List[BigDecimal](2, 3, 5, 4, 1)
median(numbers)  // return BigDecimal(3)

Try with Resource

tryWith

import io.kevinlee.skala.util.SideEffect.tryWith
tryWith(AutoCloseable) { autoCloseable =>
  // run block
}

tryWith is similar to Java's Try with Resource so it may throw an exception if the block or AutoCloseable throws any exception.

val result: WhatEverTheRunBlockReturns = tryWith(AutoCloseable) { autoCloseable =>
  // run block
}
tryWith(new FileInputStream("/path/to/file.txt")) { inputStream =>
  var c = inputStream.read()
  while (c != -1) {
    print(c.asInstanceOf[Char])
    c = inputStream.read()
  }
}
tryWith(new FileInputStream("/path/to/file.txt")) { inputStream =>
  tryWith(new InputStreamReader(inputStream)) { reader =>
    var c = reader.read()
    while (c != -1) {
      print(c.asInstanceOf[Char])
      c = reader.read()
    }
  }
}
val result = tryWith(new SomeResource()) { someSource =>
  someSource.get
}

Get Skala

In your build.sbt, add the following repo and dependency.

Note: It supports Scala 2.11 and 2.12.

resolvers += "3rd Party Repo" at "http://dl.bintray.com/kevinlee/maven"

libraryDependencies += "io.kevinlee" %% "skala" % "0.2.0"