Posts in the “scala” category

Logging brainwave data with ShadajL's NeuroSky MindWave Scala library

Yesterday I shared a simple first example of using ShadajL’s NeuroSky MindWave Scala library. Today I’m taking that a little further to share some code for a simple brainwave data recorder.

The premise is this: You put on a MindWave headset, start the recorder, and then do whatever you want to do -- work, meditate, sleep, whatever -- and the recorder records your brainwaves while you do those things. It writes the data in a CSV format so you can graph it, or do anything else you want with it.

Upgrading a Play Framework application (to 2.2.2)

When you upgrade a Play Framework application from one version to another, such as from 2.1.x to 2.2.x, you have to update a few files. I’m currently upgrading an app from 2.1.x to 2.2.x, and had to change my project/build.properties file to this:

sbt.version=0.13.1

I also had to upgrade my project/plugins.sbt file to this:

Scala 2.10 and Akka 2.0 and 2.1 import problems (Await, Future, Duration)

As a quick note, import statements that used to be like this with Akka 2.0:

import akka.dispatch.Await
import akka.dispatch.Future
import akka.util.duration._

are now like this with Akka 2.1 and Scala 2.10.x:

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._

The Duration object in Akka 2.0 has also been moved, and it’s now included with the scala.concurrent.duration._ import statement.

Generating dynamic XML from Scala source code (like a template)

Scala FAQ: How can I dynamically generate XML from my Scala source code, such as creating output for a SOAP web service?

Solution

A great feature of Scala’s XML support is that you can interweave XML and regular Scala source code together. This lets you dynamically generate XML from your Scala code.

To create XML with dynamic, embedded data, just put your Scala code in curly braces inside the XML tags, as shown in the following example:

Play Framework async controller method examples

If you want to write “async” Play Framework controller methods, I hope the following example code will help you get started. I’ll offer it here without much description today, other than to say that the list and add methods are written to work asynchronously by invoking Action.async and using a Scala Future:

Scala Days in Berlin, 2016

I’m sorry that I haven’t written publicly about Scala recently, but Scala Days in Berlin launched today with 1,000 developers in attendance. Follow the Scala Days Twitter account for information, including tweets like “The first version of the Dotty compiler is about 2x faster than the current version,” and “There’s no agreement on what's ‘best’ so all we can do is to find a local optimum,” which I assume is a statement Martin Odersky made during his keynote address.

An example of using the Geolocation web service at hostip.info using Scala

This is a quick example of how to use the geolocation web service at http://www.hostip.info/use.html using Scala:

/**
 * an example of how to use the geolocation web service at
 * http://www.hostip.info/use.html
 * using scala
 */
object GetRestContent extends App {

  val url = "http://api.hostip.info/get_json.php?ip=12.215.42.19"
  val result = scala.io.Source.fromURL(url).mkString
  println(result)

}

I don't show how to handle the JSON string in this example, but if you search this website you'll find other Scala JSON examples.

Scala - Be careful using mutable fields (var) in equals methods

The classic book, Programming in Scala, states that you need to be careful about using var fields when defining equals methods. They specifically state:

Pitfall #3: Defining equals in terms of mutable fields.

Personally, I think I've always defined equals methods with mutable fields, at least in Java. They share the following source code example that demonstrates a problem when defining an equals method in a Scala class when used in a collection.

How to use Scala Swing with SBT

As a quick note, to use the Scala Swing library (scala.swing.*) with an SBT project, all I had to do today was add this line to my SBT build.sbt file:

libraryDependencies += "org.scala-lang" % "scala-swing" % "2.10+"

Once I did that, I was able to compile the Scala Swing project normally with the sbt compile command.

Scala code to print the byte values of text files

This is some test code I wrote. It shows how to read a text file in Scala.

This first program shows how to read the entire file into memory and print out the “byte values” of each byte/character in the file:

How to assign Scala tuple fields to variable/value names

As a quick note, I was just reminded that when you have a Scala tuple instance, you can assign the tuple fields to Scala values. This tends to be more readable than accessing the tuple elements using the underscore syntax.

For example, if you have a Scala function like this that returns a tuple:

def getUserInfo = {
    // do some stuff here, then return a tuple (a Tuple3 in this case)
    ("Al", 42, 200.0)
}

you can assign the tuple fields to Scala val fields like this:

What does '???' (three question marks) mean in Scala?

Scala FAQ: What does the use of three questions marks (???) in Scala mean?

The syntax of using three question marks in Scala lets you write a not-yet implemented method, like this:

def createWorldPeace = ???

The methods you define can also take input parameters and specify a return type, like this: