Posts

Macros in Scala: moderately complex example

Image
I recently began to discover the power of newly-added macros in Scala. While they are still labeled as "experimental" , they already pack quite a punch. Most tutorials on the web usually use some simple examples - like assert macro , or debugging helper . In this post, I'll try to make a bit deeper dive into the subject, in hope that I will be able to explore darker corners of macro engineering. The use-case for today's macro is actually something I needed in one of my real projects - I had complex hierarchy of case classes, and I wanted a quick way to visualize it (without needing to read mile-long text files). So I created a macro, that takes case class, and creates a frame with JTree containing the hierarchy of case class members. Example use-case for the macro looks as follows: case class Author(name: String, birthYear: Int) case class Book(title: String, authors: List[Author]) val hobbit = Book("Hobbit: There And Back Again", List(Author(...

/dev/null for graphics

Have the following ever happened to you? You are trying to automate some data processing step. The step in question involves some complex, GUI-driven application, for which there is no source available or that source is a little to complex do dive in and extract the required functionality. As a happy coincidence, the application includes batch mode - but it still creates and displays the gui when running in batch mode (even when started with "nogui" option). (in my particular situation, the app in question was Brainstorm , a collection of Matlab scripts. The original author is using Windows exclusively, thus he probably couldn't even imagine this scenario) All of the above could be acceptable, but there is a slight problem - batch processing happens on a mainframe-style Linux server, without keyboard, display, or even X server running. Thus the application starts up, tries to create it's cosy GUI, obviously fails, and crashes hard. When applications...

Code highlighting in Blogger's dynamic templates

I decided to update the style of my blog recently, and immediately fell in love with new dynamic blog templates. Navigation is easy, and the look is quite slick. But, as usual, all shiny things have downsides. Since dynamic templates fetch content via ajax, long after the main page is loaded, "conventional" ways to highlight code snippets on page load don't work (and most code highlighting libs only use this method). And, to make things worse, developers of dynamic templates haven't provided us with hooks to detect the moment of page change. Some brave souls ventured into the depths of template javascript, and some even found the place where user's code can hook to get article change event - but since it's not documented and google developers change the code constantly, this workaround tends to break every several months or so. Currently, I use simple (as in "quick&dirty") workaround - I simply insert small piece of javascript code directl...

Implementing graph to tree conversion using Haskell

I began to become quite enamored with Haskell recently. At the very least, even if you can't use it on your current projects - because of the boss, because of legacy code, or because you just can't understand it well enough - you can always use it as an endless source of brain-teasers and puzzles. Also, since I'm recently switched to XMonad , at least some knowledge of haskell is a must. By the way, I'm extremely happy with XMonad, but that is a theme for a separate blog post :) Currently, I'm exploring various typeclasses ( State , Reader , Arrow , etc) and sometimes try to code some small snippets using them. For example, to practice using State monad, I implemented a method to extract a tree from graph using  DFS . Obviously, in such operation you need to maintain set of visited nodes somewhere, thus State seems to be a good fit. Here's the code in Haskell, and the same code in Scala (written in more "traditional" style): import qualifie...

Scala-tuplicity comes to 2.10

A while back I tried (and succeeded) compiling Scala 2.9.2 with increased limit on tuples. Some time has passed, Scala 2.10 came out, and due to the recent advent of macros there could be some possibility in lifting the limit entirely - there is a thorough discussion on mailing list, but it doesn't seem to be anywhere close. So, for time being, I updated my duct-tape solution for Scala 2.10. The following script is the automated solution for generating new scala distribution with bigger tuples: git clone git://github.com/scala/scala.git scala-tuplicity cd scala-tuplicity git checkout v2.10.0 export ANT_OPTS="-Xmx8192m -Xss25M -Xms4096M -XX:MaxPermSize=512M" VERS="-Dbuild.release=true -Dversion.number=2.10.0-tuplicity -Dmaven.version.number=2.10.0-tuplicity" ant build sed -i 's/\(val MaxTupleArity, .*\) 22/\1 55/' src/reflect/scala/reflect/internal/Definitions.scala ant build sed -i 's/22/55/' src/library/scala/runtime/ScalaRunTime.scala ant...

Scala with bigger tuples

Scala enforces upper limit on number of elements tuple can have. And if you try to go over that limit, you are greeted with friendly compiler message: "Implementation restriction...". At the first sight, that seems quite logical - after all, if in your code you need to write "tup._69", you are surely in a lot of trouble. But it is not so simple! The same restriction exists for number of function arguments, and, more importantly, number of elements in case classes. It just so happens that over time, people created amazing libraries that allow us to do truly spectacular things. Incidentally, sometimes those libraries require case classes with more arguments. One example of such a library is ScalaQuery, a library which enables us to write type-safe database queries. It maps tuples and case classes to database rows, which, combined with 22-limit, effectively restricts your database tables to 22 columns (including id and scaffolding!). I ran into that ...

Cutting down on feed consumption

As many people out there, I read web feeds frequently. Usually, I read them each morning, and then each evening. But some time ago (20 days ago, to be exact), I got flooded by problems, and had to stop reading them. But the feed reader still sat there, accumulating stuff... Over 20 days, it accumulated 3,100 feed items! It took me one full day  to read through them. Just think about it - it is an equivalent of spending 1-2 hours each day just to clean the flood of incoming feed items. Clearly, it had to stop somehow. On the course of my "reading marathon", I noticed that I do not read many feeds (Slashdot, Lifehacker, Habrahabr) "cover-to-cover". Instead, I just look at the headlines - and in most cases (90-95%) dismiss the item without reading, since I'm not interested. So today, I embarked on the quest to clean up my feed list. I deleted the ones, that delivered non-interesting content (0 valuable items in the last 10-15), or were just dead. In the...