Posts in the “scala” category

Calculating a cotangent equation in Scala

One of my nieces had a homework problem where she had to graph the x and y values of this cotangent equation:

y = 3 * cotangent(4 * x)

I couldn’t remember how to graph things like that just by looking at the equation, so I wrote this Scala “cotangent” program:

Converting PDF content to plain text with Scala (or Java)

I recently wrote a little application to convert pages from a PDF to plain text. The GUI portion of the application looks like this:

As you can see, the application just needs the name of a PDF file to convert, along with the page you want to start at and the page you want to end at. There are several ways I could make the application more convenient to use, but since I don't plan to use it that often, I can deal with its limitations.

A note about Scala/Java startup time

I was reading this post by Martin Odersky (Make the Scala runtime independent of the standard library) and came across this comment by Li Haoyi: “This would also make it more feasible to use Scala for tiny bootstrap scripts; current Mill’s launcher is written in Java because the added classloading needed to use scala.Predef (even just println) easily adds a 200-400ms of initialization overhead.” I haven’t written anything where the startup time of a Scala application was a huge problem, but that was interesting to read.

(Though I should say that I wish all Scala/Java command-line apps started faster. It’s one reason I occasionally think about using Haskell for small scripts, so I can compile them to an executable.)

Scala SBT scalacOptions syntax/examples

As a quick note to self, here’s an example of how to set scalacOptions in an SBT build.sbt file:

scalacOptions ++= Seq(
    "-Xfatal-warnings",
    "-deprecation",
    "-feature",
    "-unchecked",
    "-language:implicitConversions",
    "-language:higherKinds",
    "-language:existentials",
    "-language:postfixOps"
)

As shown, scalacOptions lets you set Scala compiler options in your SBT project build.

I got that example from my earlier very large Scala/SBT build.sbt example.

Also, I just saw that tpolecat has put together a nice list of scalaOptions flags at this URL.

Using GitHub projects as Scala library dependencies with sbt and sbteclipse

Okay, this is pretty cool. With sbt, you can magically refer to dependencies that are set up as GitHub projects, and those projects don't need to have a jar file, they can just be a source code library. This means you can save your Scala libraries as source code to GitHub (like you normally would), then pull them into your other Scala projects simply by referencing them in your build.sbt file.

Assuming you're comfortable with sbt, here's a quick six-step example that shows how to pull a GitHub library into a simple Scala project:

Processing HTTP response headers with a ScalaJ-HTTP web client

If for some reason you ever want to print out some HTTP response headers from a HEAD request when using ScalaJ-HTTP as an HTTP client, this example may help point you in the right direction:

import scalaj.http._

object TestHead extends App
{
    val response: HttpResponse[String] = Http("http://www.google.com")
        .method("HEAD")
        .timeout(connTimeoutMs = 2000, readTimeoutMs = 5000)
        .asString
    for ((k,v) <- response.headers) println(s"key:   $k\nvalue: $v\n")
}

I may write more about ScalaJ-HTTP in the future, but for today that’s a quick example of processing the response headers/parameters when making a HEAD request.

One million Scala developers

Yesterday I just churned the numbers from the surveys, but last night I started thinking how cool it is that there are one million Scala developers in the world.

I remember when I was wandering around Alaska in 2011 and first stumbled upon Programming in Scala, I found that very few people knew about Scala, maybe numbering in the thousands or tens of thousands at most. I hope Martin Odersky & Company are having a little celebration this year for their success. (And on to two million!)

Scala version of Collective Intelligence Euclidean distance algorithm

While reading the excellent book, Programming Collective Intelligence recently, I decided to code up the first algorithm in the book using Scala instead of Python (which the book uses). This is a Euclidean distance algorithm, and it provides one way to compare two sets of data to each other, and attempts to score the similarity between the data sets.

Without any further introduction (and assuming you have the Collective Intelligence book), here's the Scala source code for the Euclidean distance algorithm as described in the book:

Scala cookbook recipes

Wow, I began by writing a few Scala programming tutorials just because I like the language, and as I look here a couple of months later I now have more than sixty tutorials. As a result, I thought I'd start organizing them here in the form a Scala Programming Cookbook.

Here's my current collection of Scala FAQs and recipes, in a "cookbook" format.