Member-only story

A tiny guide to quickly test driving a library in Scala

Brian Schlining
Feb 25, 2020

--

Java has about a googol available libraries for your software enjoyment. Most of them deployed to Maven repositories. If want a fast way to try them out, you can quickly load them from a remote repository and take them for a test drive using Ammonite. So go install Ammonite and fire it up.

Simple Example:

// Import a library from a Maven repository
import $ivy.`org.apache.commons:commons-text:1.2`
import org.apache.commons.text.RandomStringGenerator
// Use classes from the library
val generator = new RandomStringGenerator.Builder()
.withinRange('a', 'z')
.build()
println(generator.generate(20))

Advance Example:

Here we add a custom repository:

// Add a repository
interp.repositories() ++= Seq(
coursierapi
.MavenRepository
.of("https://dl.bintray.com/org-mbari/maven"))
// This ampersand denotes a separate compilation block.
@
import $ivy.`org.mbari:mbarix4j:2.0.5.jre11`
import mbarix4j.math.Matlib
Matlib.diff(Array(2, 4, 6))

--

--

Brian Schlining
Brian Schlining

Written by Brian Schlining

Polyglot coder. Deep-sea Researcher. Zazen aficionado. I think squids are pretty cool.

No responses yet