Posts in the “scala” category

Scala - How to find the unique items in a List, Array, Vector (sequence)

Scala FAQ: How do I find the unique items in a List, Array, Vector, or other Scala sequence?

Solution: Use the distinct method.

Here's a simple example using a List of integers:

scala> val x = List(1,1,1,2,2,3,3)
x: List[Int] = List(1, 1, 1, 2, 2, 3, 3)

scala> x.distinct
res0: List[Int] = List(1, 2, 3)

As you can see, res0 now contains only the unique elements in the list.

How to create a mutable Set in Scala

Scala Set FAQ: How do I create a mutable Set in Scala?

To create a mutable set in Scala, first determine the type of set you want. You do this because the mutable Set is actually a trait, and you need to determine which concrete implementation you want.

For instance, if you want a SortedSet of String, define it like this:

val names = scala.collection.mutable.SortedSet[String]()

Then you can add elements to the set later like this:

Scala: Immutable collections of mutable data

Normally I just write about solutions, but I thought I'd take a moment today to write about something else. In this case I just wanted to note that it's possible to create an immutable List of mutable data in Scala. This scenario made me wonder, "What does 'immutable' mean?" Let's take a look.

As a first example, we'll create a Person class that has two fields, and the first field (firstName) can change:

A Scala XML EntityRef example (unicode character)

This page shows a good Scala XML EntityRef example:

import scala.xml._
val xml = <body>Hello{EntityRef("#8198")}World</body>

That page states that 8198 is the unicode value for a tiny space character.

That code isn't too helpful unless you can see it in the REPL, so here it is:

Scala - Thinking of functions as transformers

When you write Scala code like this:

val x = List.range(1, 10)
val evens = x.filter((i: Int) => i % 2 == 0)

the following portion of the code is a function literal (also known as an anonymous function):

(i: Int) => i % 2 == 0

When reading this code, it helps to think of this symbol:

Connecting to the NeuroSky ThinkGear API (in Scala)

If you want to connect to the NeuroSky ThinkGear API -- what they call the ThinkGear Socket Protocol -- this code shows you how to do it, at least in Scala. It consists of a few pieces of code I pulled from this GitHub repo. I wrote this code to help debug a problem I was seeing with the data.

The RawReader class does all the work. It opens a socket on the right port, then sends the necessary JSON to that socket to get things started. After that I just read the raw data that the API writes to the socket.

Scala String differences, intersections, and distinct characters

Scala String problem: You need to find the difference between two strings, the common characters between two strings, or the unique characters in a string.

Solution

You can perform all of these operations with built-in methods. Use the diff method to find the differences between two strings:

Another Lift-JSON array example

As a quick little bit of code sharing, I gave my Sarah stocks plugin the ability to allow multiple spoken phrases for one command. For instance, a user can now say “check stock prices”, “get stock prices”, or “get stocks” to check their stocks. To do this, I added an array of phrases to the JSON file that describes the Stocks plugin.

Getting to the point of this post, the following Scala source code shows how to read an array of JSON string values using Lift-JSON:

Software brevity, and "Hello, World" in Java, Ruby, and Scala

I was just reading Treating Code as an Essay in the book Beautiful Code, and saw this paragraph:

After that, I saw two versions of the famous, “Hello, world” program, one written in Java:

public class Hello {

  public static void main(String[] args) {
    System.out.println("Hello, world");
  }

}

and a second program written in Ruby:

A Scala Twitter client example

Just a quick note here today that if you want to create a Twitter client in Scala, the Java Twitter4J library looks like a good path to take.

I've shown an example below, where you can see that besides the eight lines of code it takes to create a Scala twitter object, the actual code you need to get information from the Twitter developer API is pretty short.

Lift Framework form tutorials and examples

There currently isn't much in the way of good Scala Lift Framework documentation, particularly for creating web forms, but I thought I'd share whatever good links I can find.

Currently there are only two good links about creating Lift forms, and both of those are on the official liftweb.net website: