In this post I want to give you a short introduction to Google’s new language called Dart.
Continue reading ‘First look at Dart’
Tag Archive for 'scala'
If Java is a get shit done™ language then Scala is a get shit done fast™ language. The reason is that the signal to noise ratio is very high and that concise code is easier to reason about which makes it less error prone.
When understanding Scala it is sometimes important to have a look at some real-world examples — especially when it comes to functional programming. When I first started using Scala I had a hard time understanding some of the constructs used by other people. In this blog post I want do explain some of them. The first example is a classic one. You want to debug some structure and output all elements which have a condition set.
Printing Elements of a Collection
You will find this in every first-steps guide for Scala. What I like about it is the fact that you have only little amount of code to write if you want to inspect a certain structure.
list filter { _.condition } foreach println
Flattening a Collection
Convert a collection of collections to a flat collection. For instance you have a List<List<String>> in Java and want to get a List<String> instead.
final List<String> newList = new LinkedList<String>();
for(final List<String> innerList : oldList) {
newList.addAll(innerList);
}
A lot of code for a common operation. Probably the reason why the Google Guava library supports Iterables.concat(oldList) to get the same result. So how is this done in Scala?
val newList = oldList.flatten
A simple operation which is very powerful. Let me give you another example. Suppose you have a List[Option[T]] for some T. Before understanding Scala very well and before reading this excellent article about monads I once wrote shameful code like this.
val listOfOptions: List[Option[Int]] =
List(Some(1), None, Some(3), None, Some(5))
// don't do this!
val listOfDefinedValues: List[Int] =
listOfOptions filter { _.isDefined } map { _.get }
println(listOfDefinedValues) //List(1, 3, 5)
When encountering such a construct an alarm should raise in your head, telling you there must be a better way to do this. flatten to the rescue. println(listOfOptions.flatten) would do the job.
val listOfOptions: List[Option[Int]] = List(Some(1), None, None, Some(2)) println(listOfOptions.flatten) //List(1, 2)
Using flatMap for good
Although flatMap might sound esoteric at first it is a very helpful operation. So first of all you know there is the map operation which is extremely useful in a sense like this.
"hello world" map { _.toUpper }
So what is the deal with flatMap? It would not work in this case and the compiler would raise a type error. That is because a collection is expected as the result.
flatMap makes a lot of sense when you have a collection and need to pass every element into a function that would then again return a collection.
val list = List(1,2,3,4) def intToListOfChars(i: Int) = i.toString.toList val listOfListOfChar: List[List[Char]] = list map intToListOfChars
As you can see calling map with intToListOfChars creates a list of lists and we could later flatten that list agin. Or we simply use flatMap which does all of this in one step.
val listOfChars = list flatMap intToListOfChars
Still this example is pretty awkward. But imagine you have a method like trimToOption(value: String): Option[String] which trims a string and returns None if the string is empty or null (for whatever reason).
If you write a webapplication which expects user input you are probably working with a lot of Option[T] values (or Box[T] in case of Lift). For example like this:
val description: Option[String] = extractDescription(request)
Now you also want to trim that description. Here are a couple of ways on how to do it using trimToOption.
// not so nice
val trimmed1 = trimToOption(description getOrElse "")
// ugly as hell
val trimmed2 = description match {
case Some(desc) => trimToOption(desc)
case None => None
}
// kind of ugly
val trimmed3 = (description map trimToOption).flatten
// yey! :)
val trimmed4 = description flatMap trimToOption
So you see that flatMap is actually a pretty usable function.
Passing a Tuple to a Function
Sometimes you have to pass a tuple to a function and instead of the tuple you would like to pass each value of the tuple as a parameter.
val listOfStrings =
List("Hello", "World")
val withIndex = listOfStrings.zipWithIndex
println(withIndex) //List((Hello,0), (World,1)))
So now imagine you have a method like this:
def printStringWithIndex(value: String, index: Int) = println(index+": "+value)
In this case withIndex foreach printStringWithIndex would not work because we pass a tuple to that method. Scala has a nice built-in method called tupled which takes a function and converts it to one accepting a tuple.
withIndex foreach (printStringWithIndex _).tupled
Using the Compiler
Imagine you have a class with a type parameter like CustomList[T] and now you want to have a method which is implemented only for some kind of T. Obviously one way would be to use traits and mix them in on demand. But it gets more and more complex.
Fortunately we can let the compiler proof the evidence of a type at a position in our code.
class Foo[T](bar: T) {
// only allow this method if T is a String
def length()(implicit proof: T =:= String) =
bar.length
}
val foo1 = new Foo(123) //works just fine
foo1.length //does not compile.
val foo2 = new Foo("hello")
foo2.length //5
This is great because it allows us to introduce phantom types and to write less code for certain structures. For instance we have a Connector[A] in Audiotool. This connector accepts connections to other connectors. It is either an input or an output and it transmits an abstract signal. You can only connect an output with an input. A Connector accepts only connections of its own type and virtual connections. This means the following example would be uber-cool code. Unfortunately we did not write Audiotool in Scala ;)
class Connector[A] {
add[B](conn: Conn[A, B])(implicit proof: A =:= B)
{ ... }
add(connection: VirtualConn[_, _])
{ ... }
}
Note: This is in fact not a real example since it would not work. You could only connect an output with another output but I think you might get the idea and it is easier to follow.
Conclusion
Functional programming might seem weird. Constructs like =:= or flatMap are awkward at first but make complete sense. Using Scala and picking some of the functional idioms which you think are right and well understood in your team can increase productivity and lead to less bugs.
Since I had to spend 10h in a train this weekend I was looking at some food for thought. For a long time I had a couple of slides about the Go language on my machine and so I started reading.
Go comes with a different concepts for synchronization. Channels are a construct baked into the language. They allow you to communicate between different go-routines etc. Basically I stopped reading after I came across the Channels section.
A channel allows you to send and receive data from it. Much like a Scala actor. By default a channel is performing blocking send and receive operations. Sounds pretty much like a blocking queue to me. The syntax is something like the following.
// Create a channel of int
var channel = make(chan int)
func send() {
// Send 1 to the channel.
// This operation blocks ...
channel <- 1
}
func receive() {
// Receive from channel
// This operation also blocks!
message := <- channel
fmt.Println(message) //1
}
go send()
go receive()
So ultimately I thought it should be quite easy to achieve something similar using Scala. I think this is an excellent example of the scalable part when it comes to Scala.
Go channels are diveded into reciver and sender channels, with a bidirectional channel type. The same can be achieved using Scala traits very easily. I could also add a couple of nice syntactic sugar like adding a foreach method to a channel which allows you to use it with Scala for comprehensions.
Have a look at this Gist for the complete source code. First of all one part is a little bit unfortunate. You are not able to define <- or <~ as a unary operator in Scala. So I thought I stick with the ! operator which you already know from actors and it is the analog to Go’s <-.
At the end of the file you will find a re-implementation of the Go Producer-Consumer example.
Something really cool is the fact that you can use Scala’s for-comprehensions which automatically exit once a channel is being closed. I think Go allows you do the same by specifying range when iterating over a channel. However I am not sure since I only had a brief look.
So is this useful? I do not know since I would probably stick with Scala actors for most of my multi-threading needs but it is still a great example of Scala’s flexibility as a language.
Many languages make use of implicit conversions. Scala is the only language I know of which makes implicit definitions a feature.
It is very easy to understand an implicit conversion since most programming languages do this already for you. Here are two examples (AS3/Java):
trace("Implicitly converted: "+1)
System.out.println("Implicitly converted: "+1);
Continue reading ‘Understanding Scala Implicits’
About a year ago I started using Scala. A programming language I felt in love with after all. The one and only programming language that started making me smile when I was happy to see how elegant I can express my ideas.
I have been trying to spread the word about Scala during various Flash conferences and always tried convincing my colleagues and friends to learn this great language. A blog post by Alan Shaw about his experience with Haskell inspired me to write about my journey.
Continue reading ‘A Year Of Scala’
The Flash Player is to many of us developers a blackbox. We use it every day in our job but do not really know much about it. Of course we do not have to understand its inner workings but it is sometimes very interesting to know more a little bit about specific implementation details.
Continue reading ‘Opening The Blackbox’

I have released Apparat RC5 at GoogleCode. It contains a really cool feature which is called LZMA compression.
Reducer has advanced a lot during the last couple of weeks. It is now also a strong SWF compression tool even if you do not have any PNG files it can compress. You may ask: “What is that Matryoshka doing there? And why the hell the top hat?” The top hat: I do not know. The Matryoshka: I can explain.
The Flash Player does not understand LZMA. SWF files are compressed using good old DEFLATE. So what happens? Apparat extracts your original SWF. It compresses it again using the LZMA algorithm. The compressed SWF is injected into another SWF that contains an LZMA decoder. Size, background color, frame rate, etc. get adjusted. Finally you get a new SWF that contains your old SWF and a decoder to extract it at runtime. The overhead of the decoder is currently at around 5kb and I hope I can get it even smaller.
When you open that SWF with the Flash Player it will extract your original file and load it. Another nice feature is that I created different versions of the runtime decoder. One is using a classic preloader which is great if your SWF is a little bit bigger. And hey: it is a preloader for free so you do not have to deal with the [Frame] hassle. But here is the catch. We at audiotool.com always write our SWF files in the same style and I can just hope you do the same or use the great InitInjector by Valentin Simonov.
Your main SWF class or the so called DocumentClass must make sure that a stage is available before accessing it. This is really easy:
public function Main() {
addEventListener(Event.ADDED_TO_STAGE, init)
if(null != stage) init()
}
private function init(event: Event = null): void {
removeEventListener(Event.ADDED_TO_STAGE, init)
// your original constructor code goes here ...
}
Stick to that rule or InitInjector can automate it for you. Otherwise you will get a runtime exception that the stage is null. So this is one new feature. The other one is actually pretty standard. If you compile and link against a SWC file all classes will come in their own ABC file. Each ABC file has a constant pool. So if you link against 1000 classes you get 1000 constant pools. Reducer can merge all those files into a single one with some minor exceptions. It can also sort the constant pool so that constants which are used frequently consume less bytes when accessed. The funk-as3 test runner which links against FlexUnit and the Flex framework is only loosing 3.01% of its weight with the old Reducer. The new version reduces it by 17.81% already thanks to the ABC merging. Combine that with some LZMA love and we get 35.34%.
Besides you can call the LZMA compression as often as you want for some basic obfuscation. The LZMA compression alone is already a (weak) obfuscation of your bytecode.
Last but not least: Good news everyone! Scala 2.8 arrived so this is the last time you will have to update it for a while.
You can download the latest Apparat version at GoogleCode. Scala 2.8.0 is required.
A couple of weeks ago I started learning Scala. I can highly recommend it. The language has a lot of great approaches to multithreading and scalability. The reason why I like Scala is because it is so simple yet powerful.
Of course doing something at home influences work. I decided to write a build tool for the Hobnox AudioTool which is about 200 lines of Scala code. Cool thing is that this tool replaces a manually maintained Ant build and all project dependencies are always correct. Plus analyzing the dependencies in a more powerful way allows me to spawn the compiler in parallel. Building the AudioTool is now twice as fast and much more comfortable.
When learning a new programming language you also learn about new concepts. Functional languages in general have a different approach to nullable types. I know Scala is not the only one but let me introduce the concept in terms of ActionScript.
When you have a method that returns either a result or nothing: what do you do? Imagine you have some kind of service and a Dictionary of users. Requesting a user works by his unique id. The Dictionary is private to the class since you want to keep it read only.
function getUser(id: String): User {
return hasUser(id) ? users[id] : null;
}
If I would now simply ask the service for an unknown user and do something like if(getUser('xyz').isLoggedIn) { trace('Hooray'); } I could probably and up with a null-reference error. No one checks for me if the user exists. So what else could we do? Write a lot of boilerplate code and prepend a check if the user is null or not each time we request one from the service. A much better approach in my opinion is to throw an error as early as possible. In this case we would rewrite the method to something like this:
function getUser(id: String): User {
if(hasUser(id)) return users[id];
throw new NoSuchUserError(id);
}
In this scenario we get informed about the error as early as possible. But we are stuck again. First of all ActionScript does not enforce you to catch possible exceptions. This means if you do not read the documentation of a method carefully or look into the source code of a method before calling it somewhere you will never know that it throws an exception. And what if we are actually in a scenario where we do not expect errors for non-existing objects? Think of the Dictionary object throwing each time an error when you access it and the result is null. How could I even check if an object exists in a Dictionary?
try {
dictionary[key];
return true;
}
catch(noSuchElementError: NoSuchElementError) {
return false;
}
I guess you see that this can not be the solution to our problem. In a real world example you may deal with your own collection of objects instead of a Dictionary of course. So we have to get rid of exceptions and null for the optimal solution. Scala’s approach to this problem is the Option type. We always abuse null as a placeholder when we want to express that an element does not exist. The Option means that either Some or None result exists. Rewriting our getUser function using this approach would yield the following ActionScript code.
function getUser(id: String): Option {
return hasUser(id) ? new Some(user[id]) : new None();
}
Why is this much better than the old approach? When calling the method you will always know that the method has only an optional result value. We get rid of the exception and null values. Our only problem at the moment is ActionScript. The result is now untyped. In an ideal world this method would be written as:
function getUser(id: String): Option.<User> {
return hasUser(id) ? new Some.<User>(user[id]) : new None.<User>();
}
However we can still tackle this issue by implementing null-representations of our objects. Imagine the User class. You could rewrite the code to something like this.
function getUser(id: String): IUser {
return hasUser(id) ? user[id] : new NullUser();
}
final class NullUser implements IUser {
public function get isLoggedIn(): Boolean { return false; }
public function get name(): String { return 'null'; }
}
And even if you are interested in null-reference errors you could rewrite your code to something like this:
final class NullUser implements IUser {
public function get isLoggedIn(): Boolean {
CONFIG::ThrowNullReferenceErrors { throw new NullReferenceError(); }
return false;
}
public function get name(): String {
CONFIG:: ThrowNullReferenceErrors { throw new NullReferenceError(); }
return 'null';
}
}
It is definitely a very different approach. A functional language like Scala allows you to deal much better with Options. But it makes sense to diferentiate between an uninitialized variable which is null and an optional result of a function. Unfortunately this is at the moment very painful with the lack of generics in ActionScript.