Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Useful Ammonite snippets

Useful Ammonite snippets

All of these can be figured out by looking at ammonite.io and at libraries pages, but to make things faster for myself I written it down (I don't always want to search through Ammonite history nor pollute my ~/.ammonite/predef.sc).

For convenience, next to library there is badge with the newest version and next to one liners for Ammonite for specific version is link to releases - it let you update to newest version without goolging or checking project's site.

(ToC generated using Markdown TOC generate)

Ammonite for specific Scala version

Newest releases.

2.10

Last release of 2.10 (1.0.3)

[ -f /tmp/ammonite-2.10/amm ] || (mkdir /tmp/ammonite-2.10 -p && curl -L https://github.com/lihaoyi/Ammonite/releases/download/1.0.3/2.10-1.0.3 > /tmp/ammonite-2.10/amm && chmod +x /tmp/ammonite-2.10/amm) && /tmp/ammonite-2.10/amm

2.11

Last release of 2.11 (1.6.7)

[ -f /tmp/ammonite-2.11/amm ] || (mkdir /tmp/ammonite-2.11 -p && curl -L https://github.com/lihaoyi/Ammonite/releases/download/1.6.7/2.11-1.6.7 > /tmp/ammonite-2.11/amm && chmod +x /tmp/ammonite-2.11/amm) && /tmp/ammonite-2.11/amm

2.12

[ -f /tmp/ammonite-2.12/amm ] || (mkdir /tmp/ammonite-2.12 -p && curl -L https://github.com/lihaoyi/Ammonite/releases/download/2.1.4/2.12-2.1.4 > /tmp/ammonite-2.12/amm && chmod +x /tmp/ammonite-2.12/amm) && /tmp/ammonite-2.12/amm

2.13

[ -f /tmp/ammonite-2.13/amm ] || (mkdir /tmp/ammonite-2.13 -p && curl -L https://github.com/lihaoyi/Ammonite/releases/download/2.1.4/2.13-2.1.4 > /tmp/ammonite-2.13/amm && chmod +x /tmp/ammonite-2.13/amm) && /tmp/ammonite-2.13/amm

Generate Ammonite import format from sbt/coursier import format

implicit class PrintImport(deps: coursierapi.Dependency) {
  def forAmmonite(mock: Any): Unit = println(s"import $$ivy.`${deps.getModule.getOrganization}:${deps.getModule.getName}:${deps.getVersion}`")
  def fetch(mock: Any): Unit = interp.load.ivy(deps)
}

Copy-paste sbt dependency into Ammonite, add forAmmonite () and run it

@ "org.typelevel" %% "cats-core" % "2.1.1" forAmmonite ()
import $ivy.`org.typelevel:cats-core_2.13:2.1.1`

then copy-paste output of forAmmnite () in order to import it or share with others or

@ "org.typelevel" %% "cats-core" % "2.1.1" fetch ()

to fetch dependencies and make them available in REPL.

Standard library utils

import scala.util._, scala.concurrent._, scala.concurrent.duration._

Not use together with monix.execution.Scheduler

import scala.concurrent.ExecutionContext.Implicits.global

2.13-only

import scala.util.chaining._

Set compiler flags

Enable macro annotations

2.13-only, 2.12 needs Macro paradise

interp.configureCompiler(_.settings.YmacroAnnotations.value = true)

Log implicits

interp.configureCompiler(_.settings.XlogImplicits.value = true)

Partial unification

Build-in in 2.13, only needed in 2.12

interp.configureCompiler(_.settings.YpartialUnification.value = true)

Load compiler plugins

Kind projector

Maven Central

import $plugin.$ivy.`org.typelevel:kind-projector_2.13.2:0.11.0`

Better monadic for

Maven Central

import $plugin.$ivy.`com.olegpy::better-monadic-for:0.3.1`

Macro paradise

2.13 replaced it with Ymacro-annotation compiler flag

Maven Central

import $plugin.$ivy.`org.scalamacros:paradise_2.12.11:2.1.1`

Splain

Maven Central

import $plugin.$ivy.`io.tryp:splain_2.13.2:0.5.6`

Import libraries

Cats

Maven Central

import $ivy.`org.typelevel::cats-core:2.1.0`, cats._, cats.data._, cats.implicits._

Maven Central

import $ivy.`org.typelevel::cats-effect:2.1.3`, cats.effect._

Maven Central

import `org.typelevel::kittens:2.1.0`, cats.derived.auto._

Circe

Maven Central

import $ivy.`io.circe::circe-generic:0.13.0`, io.circe._, io.circe.generic.auto._
import $ivy.`io.circe::circe-parser:0.13.0`, io.circe.parser._
import $ivy.`io.circe::circe-optics:0.13.0`, io.circe.optics.JsonPath._

Enumeratum

Maven Central

import $ivy.`com.beachape::enumeratum:1.6.0`, enumeratum._
import $ivy.`com.beachape::enumeratum-circe:1.6.0`, enumeratum._

FS2

Maven Central

import $ivy.`co.fs2::fs2-core:2.3.0`, fs2._
import $ivy.`co.fs2::fs2-io:2.3.0`, fs2._

Monix

Maven Central

import $ivy.`io.monix::monix:3.2.1`, monix.eval._, monix.execution._
import monix.execution.Scheduler.Implicits.global

Pureconfig

Maven Central

import $ivy.`com.github.pureconfig::pureconfig:0.12.3`, pureconfig._

Shapeless

Maven Central

import $ivy.`com.chuusai::shapeless:2.3.3`, shapeless._

Chimney

Maven Central

import $ivy.`io.scalaland::chimney:0.5.2`, io.scalaland.chimney.dsl._
import $ivy.`io.scalaland::chimney-cats:0.5.2`, io.scalaland.chimney.cats._

Catnip

Maven Central

interp.configureCompiler(_.settings.YmacroAnnotations.value = true)
import $ivy.`io.scalaland::catnip:1.0.0`, io.scalaland.catnip._, cats._, cats.implicits._, alleycats.std.all._

Enumz

Maven Central

import $ivy.`io.scalaland::enumz:1.0.0`, io.scalaland.enumz.Enum
@MateuszKubuszok

This comment has been minimized.

Copy link
Owner Author

@MateuszKubuszok MateuszKubuszok commented May 25, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment