In this post I want to give you a short introduction to Google’s new language called Dart.
Continue reading ‘First look at Dart’
Archive for the 'misc' Category
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.
For the upcoming Audiotool 2.0 launch in collaboration with Burn I am developing our new website. The framework choice was quite obvious for me since I love Scala and really like Lift.
Lift is very nice because of its excellent Ajax and Comet support. My main problem getting started with Lift was the lack of documentation and real world examples. E.g. if you want a custom 404 page without a redirect your only source of information is a thread in the mailing list. Lift also comes with its own terminology and a lot of custom DSLs.
Cricitcs aside: I am absolutely happy that Lift exists. You simply have to start using it. The documentation has also improved a lot. Simply Lift and Exploring Lift are available for free and the Wiki contains a lot of useful information too.
I want to give you a simple example of why I think that Lift is simply awesome and what sold me to the framework. You probably know those endless scrolling pages.
Let’s see how to develop something similar with Lift and Scala. First of all we have to prepare a little template.
<div class="lift:surround?with=default;at=content">
<div class="lift:endless">
<ul>
<li>item0</li>
<li>item1</li>
<li>item2</li>
</ul>
</div>
</div>
That is all the HTML we need. In fact we need even less but if you are going for designer friendly templates you could extend it with your usual page chrome.
There are two calls to Scala code in this template. lift:surround will trigger the SurroundSnippet and insert the current template into the default template at the content position.
Our duty is now the implementation of lift:endless. Lift comes with a dynamic convention-over-configuration lookup for the so-called snippets. I am not a fan of COC so I will stick to the manual mechanism of binding endless to a snippet. Lift offers the DispatchSnippet in this case.
You will have to add this line to Boot.scala:
LiftRules.snippetDispatch append Map("endless" -> EndlessSnippet)
It tells Lift to invoke the dispatch method of the EndlessSnippet object whenever a snippet named endless is encountered.
So the only missing piece is the implementationof the EndlessSnippet and this is where Lift really shines.
import net.liftweb.common._
import net.liftweb.util._
import net.liftweb.http._
import net.liftweb.util.Helpers._
import net.liftweb.http.js.jquery.JqJsCmds
import net.liftweb.http.js.JsCmds
import net.liftweb.http.js.JE
import scala.xml.{NodeSeq, Unparsed}
object EndlessSnippet extends DispatchSnippet with Loggable {
def dispatch = {
// If you want to execute render only when "embed.xyz" is called
// you would match on "xyz" here.
case _ => render
}
def render = {
// Generate a random identifier
val containerId = nextFuncName
// The page which we are currently looking at.
var page = 0
// Method to be called by JavaScript.
def process(in: Any) = in match {
case id: String if id == containerId =>
page += 1
logger.debug("Generating page "+page+".")
// Generate new markup and append it to the list.
JqJsCmds.AppendHtml(id, getItems(page))
case _ =>
logger.error("Illegal data has been sent.")
JsCmds.Noop
}
// Here we marry the template with our code.
// "*" is a CSS selector that matches anything and #> binds it to the content
// we specify.
// The most interesting part here is the JavaScript which we bind to the process
// closure. Lift does all the magic for us and calls the server-side method once the
// user scrolls to the bottom of the page.
"*" #> (<ul id={containerId}>{getItems(page)}</ul> ++ JsCmds.Script(JE.JsRaw(
"""$(window).scroll(function(){
if($(window).scrollTop() == $(document).height() - $(window).height()) {"""+
SHtml.jsonCall(containerId, process)._2.toJsCmd+"""
}
})""").cmd))
}
def getItems(offset: Int): NodeSeq = {
val count = 100
val start = offset * count
val end = from + count
// We generate a couple of <li> elements here.
for(i <- start until end) yield {
<li>{"item"+i}</li>
}
}
}
This little amount of code really shows the power of Lift. Simply write a closure and bind it to your JavaScript. Lift does all the plumbing for you. Then you only have to implement some logic on the server. In most cases you do not even have to write any JavaScript since Lift offers Ajax links, forms etc. already. SHtml.a(...) generates a link which will trigger a function on the server for instance.
But please beware: I am not sure if SHtml.jsonCall is actually the way how you should do this. There might be a better way of calling a server-side method via JavaScript.
There is of course more to Lift. Comet is where things get really cool and the record framework seems interesting as well. Hopefully this little excursion into Scala and Lift could quicken your appetite. Since I am usually not a frontend fan this made it interesting and fun again.
I will try to post a complete project in the next days as well. Before starting with the Audiotool website I wrote some test cases that might help other people getting started.
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’
Patrick Le Clec’h is an active committer to the Apparat project and recently we just merged his work into the main branch. With a couple of other changes this is now a good time for another release candidate. Patrick added a good old friend to Apparat: The __asm function. You might remember __asm from my work on the now deprecated AS3C project.
However the Apparat version is much better. First of all we have put a lot of work into Apparat to make such transformations rock-solid. AS3C had its issues and was never a reliable tool. But there are a lot of new great features Patrick implemented. You can mix AS3 in your bytecode as well. __as3 is the best friend of __asm. Because sometimes writing pure bytecode is very verbose and not necessary.
A simple trace('Hello World!') with pure bytecode would look like this. Please note the FindPropStrict and CallPropVoid operations which reference trace.
__asm(
FindPropStrict(AbcQName('trace', AbcNamespace(NamespaceKind.PACKAGE, ''))),
PushString('Hello World!'),
CallPropVoid(AbcQName('trace', AbcNamespace(NamespaceKind.PACKAGE, '')), 1)
);
Finding the object in the correct namespace is often a very cumbersome task. Thanks to __as3 we can also write this in a much more conciese way.
__asm(
FindPropStrict(__as3(trace)),
PushString('Hello World!'),
CallPropVoid(__as3(trace), 1)
);
Note that the ASM compiler will try to guess the required name once it is requested by an operation. You can use __as3 also for other tasks.
var x: int = 1;
__asm(
FindPropStrict(__as3(trace)),
__as3(x < 10),
CallPropVoid(__as3(trace), 1)
);
This would trace "true" for instance. If you are curious about the ASM syntax I can recommend you using the dump tool. It produces code which is nearly __asm-ready. We will probably write another output so you can directly transform existing code to __asm calls.
If you are interested in some more examples the Apparat Math replacements make use of __asm now as well. IntMath is a good example for an inlined class where you are using maybe a simple method like IntMath.abs and the heavy lifting is done behind the scenes using inline assembler. To use the ASM expansion you have to process your SWF file with TDSI. It is by default turned on.

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.
When you have people like Scott Dadich, Ben Fry, Kevin Lynch and Yugo Nakamura gathered at one conference you can be sure to expect nothing less than a stunning event.
Shawn Pucknell is obviously raising the bar. This will be a tough time for an ordinary speaker like me. However I have no other intentions than giving a lecture that proves itself worthy of this event.
We have release a small update of the AudioTool and included André’s ToneMatrix. Some features that have been implemented in the last weeks are online as well. Removing an effect for instance will not destroy the cables. The desktop scales now without interrupting the audio output and various other performance tweaks have been made.
Hooray! The Hobnox AudioTool has been updated. You will find the TR-808 now and also some of our new stompboxes like the Compressor and Crusher. The mixer has also been expanded to cover 16 channels.
There is a lot more stuff in the pipe and I am really excited about it. Developing the timeline and saving arrangements are the next big steps. Until then: have fun playing around and stay tuned!
I am glad that UnitZeroOne.com made a contribution to the imageprocessing library. Now there is a very fast bump mapping and normal map filter (QuickBumpMap and QuickNormalMap).
Theese filters work with a native filter like the QuickSepia and there would be a way to implement them on a pixel-level. So this is why they have the Quick prefix.
Right now I am working (besides of my work) on very nice filters to synthesize textures. As always: Stay tuned!

