Posts

Configuration objects in Scallop

It turned out that spreading type annotations throughout all the code in all places where options are requested is not a good idea. And, of course, it is prone to errors - who likes those type annotations? When using such option parsing libraries, users usually end up creating a big object with fields corresponding to each option: object Conf { var apples:Int = 0 var bananas:Int = 0 } // ... parsing options ... Conf.apples = parser.getOption("option name", ...) Conf.bananas = parser.getOption("other option name", ...) Sure, this works, and usually flexible enough - but why would anybody want to create such boilerplate? And by the way, nobody likes var's in their code :) So in Scallop , I needed to cut down that unnecessary repetition. There are several ways: Create a compiler plugin. The most powerful option. And the least enjoyable - even given the ease of installing a compiler plugin using sbt, I doubt that anybody will install compiler p...

Better CLI option parsing in Scala

Command line interfaces have been with us almost as long as computers themselves - first cli's were created  in early 1960s , only two decades later than first general purpose computer was presented ( in 1946 ). From that old times command line and command line interfaces became trusted and loyal friends of the programmer. Fast forward to modern times. Now, half a century later, graphical user interfaces seem to be triumphing over all other interfaces. GUI evangelists love to present pretty charts, which demonstrate that CLI's are dying. But is CLI really dead or planning to die? No way, of course. The whole UNIX philosopy is built around small, orthogonal utilities, each doing its small piece of work perfectly (think wc, cat, find, bash pipes). And, obviously, this effect is almost impossible to achieve using pretty graphical systems with lots of buttons, checkboxes, sliders, knobs and what-not. The "almost" part of that statement is what keeps the m...

Memorizers in Scala

Today I will talk about anonymous functions and memorizers. Here is a simple, self-sufficient anonymous function (I think it is the simplest possible): val fun = (a:Int) => a + 1 What's wrong with it? Nothing, except for the fact, that it would recompute the value every time it is called. In case of adding 1 to argument, it is not a problem, but what if we would be doing something expensive? Wouldn't it be cool to be able to do something like following? val fun = mem( (a:Int) => a + 1 ) And Scala permits us to do exactly this: def mem[A,B](f:A=>B) = new Function[A,B] { import scala.collection.mutable.Map; private var cache:Map[A,B] = Map() def apply(v:A):B = cache getOrElseUpdate(v,f(v)) } val fun = mem( (a:Int) => a + 1 ) Now, we have a function, that stores all the computed results into a Map, and does not recompute them. There are several minor problems with this approach - for example, it is not thread-safe, and sharing this function between thre...

How to set up a simple deb repository: Distilled Instructions

This is tutorial for setting the most simple working deb repository, tested on Linux Mint 10 (Ubuntu 10.10, in other words). I used reprepro  for this task -  an utility to create and manage Debian repositories. You can get it from standard repositories: sudo apt-get install reprepro Now, to actual repo creation. I was creating a repository at /var/www/repo/, you can create it anywhere, as long as Apache is able to reach it. mkdir /var/www/repo/ cd /var/www/repo/ # base directory for reprepro mkdir conf/ # directory for reprepro configuration files echo ' Origin: <project name> Label: <project name> Codename: <osrelease> # maverick, in my case Architectures: i386 amd64 Components: main Description: <description> # if you need to distribute packages for more os versions (natty, for example) # just add another block, separated by empty line ' > conf/distributions reprepro -v export <osrelease> It's done! Now you can add your packages by ...

Distributing your app with sources

Ever wanted a super-easy way for including the sources of your program into your distribution?  SBT  makes it possible with just one line: unmanagedResourceDirectories in Compile += new File("src/main/scala") And now, after you run "package", all your sources would be placed alongside with compiled classes in the .jar.

Algorithm to create realistic forest

Image
I am doing some funny spare coding these days (while getting used to emacs) - and in the picture above you can see one of my projects. I call this "Rangband" - a simple clone of Zangband, actually. But it seems that Zangband (the best crawl rpg I saw) and ToME (the crawl rpg with the best story setting) are out of active development, and also they are buggy as hell. So I set off to write a small clone, just for my fun. As for now, there is only auto-generated forest, moving character (@), and line-of-sight and fog-of-war calculations. First thought for generating forest is just to spray objects all over the map randomly, but that generates true chaos. And I wanted some clearings and thickenings in my forest, so I decided to trim that chaos a bit. It turns out that if we run this chaotic forest through just one iteration of modified  game of life  algorithm, we will get exactly what we want (or I want :) You just need to remove all trees that have less than three...

Rogach meets SBT, or "How to add a remote JavaScript console to your Scala server"

I recently installed sbt, and it was a big breakthrough for my development - before you try things like sbt, you think like "who needs that automatic dependency management", "I can do full recompile on every run - computer is fast enough", "I surely can live without console", etc. But when you install sbt, everything suddenly changes - you find that you can add a full jetty server to my project just by typing in a name and it's version, and do not need to search & download & place in appropriate dir & add to classpath & ... You just type that dependecy line, and forget about it. And you get many-many other goodies as well. (by the way, I had the same thoughts before migrating to Scala from Java - "really, who cares about that boilderplate? I think slower than I type, don't I?" - and the same relevations when I made the move) Now, I feel that sbt increased my speed at least twofold - clever recompilation and sbt-r...