Posts

Showing posts with the label scala

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...

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...