“From Microliths To Microsystems,” by Jonas Bonér
The slides from Jonas Bonér’s talk, “From Microliths To Microsystems,” are now online at speakerdeck.com.
The slides from Jonas Bonér’s talk, “From Microliths To Microsystems,” are now online at speakerdeck.com.
Scala REPL FAQ: How do I add a Jar file to the Scala REPL classpath? (The Scala REPL is the interactive command line you get if you just type scala
at your command line.)
To add a new jar file to the Scala REPL classpath (interactive command line classpath), use the :require
command at the command line, like this:
scala> :require myjar.jar
Li Haoyi has a nice blog post titled, Strategic Scala Style: Principle of Least Power.
If you ever need to print 25 blank spaces in Scala, this works:
println(" " * 25)
That’s pretty cool (and convenient) that you can repeat X number of characters with such a concise syntax like that. Of course you don’t have to just print the characters out, you can also assign them to a value and then use them however you want to:
val x = "a" * 10
Summary: How to properly use the Scala Option/Some/None idiom to initialize empty var fields -- and specifically how not to use null values for the same purpose.
When you get started in the Scala world, you quickly learn that null
values are a bad thing. Scala makes it easy to replace null
values with something better, and that something better is what I call the Option/Some/None pattern (or idiom).
Akka actor FAQ: How do you stop an Akka actor?
I don't have time this morning to write my usual tutorial, so in short, if you want to stop an Akka actor, use code like this from inside your actor's receive method:
context.stop(self)
Or, if you want to shut down the Akka system, use the following code, again from inside the receive method of one of your actors:
While working with a Scala Actor last night, I came across a situation where I wanted to be able to manually tell the Actor to quit/die/terminate.
It looks like the proper way to exit a Scala Actor is pretty simple:
A cool thing about Scala for
loops — what I’ll more-accurately call for
expressions in this article — is that you can have multiple generators. What’s also very cool about them is how they work.
For example, imagine that you have these two values:
val nums = Seq(1,2,3) val letters = Seq('a', 'b', 'c')
An interesting question then becomes, “What is the type of res
in this expression?”:
As I was researching who might be using an “IO Monad” in Scala, I found this quote from Martin Odersky in the Google Group titled “scala-debate”:
“The IO monad was a neat trick for combining side effects with lazy evaluation ... there is only one lazily evaluated language in wide usage today and even its creators have said that laziness was probably a mistake. Strict languages don’t need the IO monad, and generally don’t have it, even though they could. Bob Harper’s posts in his ‘existential type’ series are a good explanation on why not.”
Here’s a link to Bob Harper’s The Point of Laziness article.
Sadly, I had to get away from Scala for a while, but now I can get back to it again. Just as I started getting back into it I happened upon the following code, and thought, “Well, surely title
in this anonymous class is a var
field. How strange that the Programming in Scala guys would use a var
like this.”:
This image comes from a reactivesystems.eu article titled, Things I wish I knew when I started building reactive systems.
Just a quick note today that if you want to create a mutable Scala array -- particularly an array that can grow in size after you first declare it -- you need to use the Scala ArrayBuffer class instead of the Array class, which can't grow.
Here's a short example of how to instantiate an ArrayBuffer object, then add elements to the array:
Here’s a link to some Effective Scala slides, as presented by Mirco Dotta in 2013.
I use the Java StringTemplate library in my Android applications, and Brian Clapper has created a Scala wrapper around it that he calls Scalasti. His intro: “Scalasti is a Scala interface to the StringTemplate Java template library. It provides a subset of the features of StringTemplate, using a more Scala-friendly syntax.”
If you ever need to generate a list of subdirectories in a directory in Scala, here's one way to do it:
def getListOfSubDirectories(directoryName: String): Array[String] = { return (new File(directoryName)).listFiles.filter(_.isDirectory).map(_.getName) }
I intentionally wrote that function in a short, "Scala like" style, but you can expand it to multiple lines, if you prefer.
The following links are a collection of Scala XML tutorials I've written. Most of them come from the Scala Cookbook, while the others were written before I wrote the Cookbook.
Without any further ado, here are the links:
A really terrific feature about Scala is that XML handling is built into the language. This means you don't have to deal with XML as String objects, you deal with it as XML objects.
Here are just a few examples of using XML in Scala. First, you can create an XML literal like this:
scala> val hello = <p>Hello, world</p> hello: scala.xml.Elem = <p>Hello, world</p>
Again, note that this is not a String, there are no double quotes; we've just defined an XML literal in Scala.
Problem: In a Scala application, you want to extract information from XML you receive, so you can use the data in your application.
Use the methods of the Scala Elem
and NodeSeq
classes to extract the data. The most commonly used methods of the Elem
class are shown here:
When it’s finished, this series of articles by José Manuel may be a good read on functional programming.
Lightbend has a good article by Jonas Boner and Viktor Klang titled Reactive Programming versus Reactive Systems. This quote describes the article: “looking at the differences between writing code in a Reactive Programming style, and the design of Reactive Systems as a cohesive whole.”