Scala/Ammonite: How to load/import a jar file into the REPL
TIL that you can import/load a JAR file into the Scala Ammonite REPL using this import
command:
import $cp.foo.`simpletest_3.0.0-0.2.0.jar`
TIL that you can import/load a JAR file into the Scala Ammonite REPL using this import
command:
import $cp.foo.`simpletest_3.0.0-0.2.0.jar`
As a note to self, I had a problem with the ZIO HTTP library, where it was throwing Netty errors/exceptions like this:
io.netty.channel.AbstractChannel$AnnotatedNoRouteToHostException: null: jsonplaceholder.typicode.com.
The solution to this was to make a couple of changes to my SBT build.sbt file, specifically adding the javaOptions
setting below, and forking the running application from SBT:
As a little note today, if you want to create a little startup file for the Scala Ammonite REPL, the way you do that on Mac and Unix/Linux systems is to create or edit this file:
~/.Ammonite/predef.sc
If you ever need to convert HTML to plain text using Scala or Java, I hope these Jsoup examples are helpful:
import org.jsoup.Jsoup import org.jsoup.nodes.{Document, Element} object JsoupHtmlToPlainTextTest extends App { val html = """ |<html> | <head><title>Hello, world</title></head> | <body> | <h1>Hello, world</h1> | <p>Hello, world.</p> | <p>This is a test.</p> | </body> |</html> """.stripMargin // Example 1: this works, but all output is on one line val doc: Document = Jsoup.parse(html) //val s: String = doc.text() //include <head> and <body> text val s: String = doc.body.text() //<body> text only //println(s) // Example 2: this works, output is on multiple lines val formatter = new JsoupFormatter val plainText = formatter.getPlainText(doc) //println(plainText) // Example 3: this works as a way to select the <body> only val body: String = doc.select("body").first.text() //println(body) // Example 4: works: gets text from paragraphs only // https://jsoup.org/cookbook/input/parse-body-fragment val doc4 = Jsoup.parseBodyFragment(html) val body4 = doc4.body() val paragraphs = body4.getElementsByTag("p") import scala.collection.JavaConverters._ val scalaParagraphs = asScalaBuffer(paragraphs) for (paragraph <- scalaParagraphs) { println(paragraph.text) } }
While this is just some test code that I’m currently working on to understand Jsoup, the code shows four different ways to convert the given HTML into plain text. Hopefully the comments explain how the HTML to plain text conversion processes work, so I won’t write more about them. I just wanted to share this code snippet here today a) so I can find it again, and b) in hopes it might help others that need to convert HTML to text using Jsoup.
My 100% FREE “Introduction to Functional Programming” online video training course is now finished! To make things a little easier, here are links to all of the free videos in the training course:
March 24, 2024: I just released my free “Advanced Scala 3” online video training course. This free video course gets into different Scala programming topics such as functions, types, generics with variance and bounds, multiversal equality, modular programming, extension methods, and much more.
As always I want to thank Ziverge’s software consulting services for sponsoring these videos! These video courses take many weeks and even months to create, and they would not exist without Ziverge.
<<Click here to start my free Advanced Scala 3 video training course.>>
April, 2024: As a brief note today, the PDF version of my book, Learn Functional Programming The Fast Way!, is now FREE. I wrote this functional programming book for Scala, Java, and Kotlin developers, and you can now download it for free here:
If you’re interested in functional programming, or just want to learn more about data types, generics, pure functions, expression-oriented programming, and functional error handling, I hope this book is helpful.
Scala FAQ: When compiling a Scala application with SBT, I get warning messages like these:
$ sbt compile [warn] there were 6 deprecation warnings; re-run with -deprecation for details [warn] there were 4 feature warnings; re-run with -feature for details
How do I ’re-run with -deprecation’ or ’re-run with -feature’?
Summary: This post is a discussion of the “Option
/Some
/None
Pattern” in Scala, specifically how to use the Either
/Left
/Right
data types instead of Option when you need to know the reason some code failed. As you may know, the None data type does not return failure/exception information, but if you use the Either
/Left
/Right
types, you can access that failure information through the Left
type.
The post is sponsored by my book, the 2nd Edition of the Scala Cookbook.
Scala List FAQ: Can you share some Scala List
class examples?
The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.
In this tutorial, I'll share examples of the most common List
operations (methods).
This is a “cheat sheet” for the Scala 3 programming language. I initially wrote it for my book Learn Functional Programming The Fast Way, but I thought I’d share it here as well.
If you want to see many more details about Scala 3, see:
If you ever need some good ScalaJ-HTTP examples, see the test files in the project, including this HttpBinTest.scala file. That file currently shows a number of good ScalaJ-HTTP examples, including GET, POST, and redirect examples with Scala.
See that page for a full list of examples, but for my own use, here are a few of them.
Scala FAQ: Can you show an example of the Scala 3 if-then-else-if syntax?
Here’s an example of the Scala 3 if-then/else-if/else syntax, as used as the body of a method:
I ran into a strange problem this weekend where I noticed a huge difference between the way a Scala Future
and a Thread
worked for the exact same code. I think I’m pretty aware of the obvious differences between futures and threads, so this really caught me by surprise. If/when I figure out why there was such a difference in behavior for the given problem I’ll post that information here.
While that problem will haunt me for a while, what I really want to post here today is a simple Scala Thread
example:
Scala collections FAQ: How can I convert a Scala array to a String? (Or, more, accurately, how do I convert any Scala sequence to a String.)
A simple way to convert a Scala array to a String is with the mkString method of the Array class. (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.)
Scala FAQ: Can you share some examples of the Scala if/then/else syntax? Also, can you show a function that returns a value from an if/then/else statement?
In its most basic use, the Scala if/then/else syntax is very similar to Java: