Utilities for Scala
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.build-scripts
notes
project
src
.gitignore
.travis.yml
ISSUE_TEMPLATE.md
LICENSE
PULL_REQUEST_TEMPLATE.md
README.md
build.sbt

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.TryWith.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
}

TryWith

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

TryWith returns Try type after apply the given function then closes the AutoCloseable.

val result: Try[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 =>
  /* 
   * NOTE: The inner tryWith is tryWith not TryWith. 
   * Otherwise, it results in Try[Try[T]] instead of Try[T]
   * Try[Try[T]] case will be handled in a future release
   */
  tryWith(new InputStreamReader(inputStream)) { reader =>
    var c = reader.read()
    while (c != -1) {
      print(c.asInstanceOf[Char])
      c = reader.read()
    }
  }
}
val result: Try[String] = tryWith(new SomeResource()) { someSource =>
  someSource.get    // returns String 
}

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.1.0"