Posts

DIY screenshot sharing service

I often find it useful to send someone a screenshot - to facilitate some explanation, to point out a bug in some app, or maybe even just brag about my awesome desktop. Obviously, I can open a terminal, start some screenshot app, save screenshot to file, upload a file somewhere, then figure out the link, etc - quite repetitive, I think. Or I could use some of the new-and-popular screenshot uploading services - like  Gyazo  for example. But all those services require you to install some package or application, which looks ridiculous for such a simple task. But we are Linux users, aren't we? We can always do better! Especially when we have some rusty bash skills, barely alive web-facing server somewhere in our closet, and half an hour of free time on our hands. #!/bin/bash f=$(mktemp -u screenshot_XXXXXX.png) import /tmp/$f scp /tmp/$f server:/some/webroot/ rm /tmp/$f url="http://your.server.hostname/$f" echo $url | xclip echo $url | xclip -sel c echo $url The abov...

Quick scala experimenting in emacs

Sometimes it is very handy to do a quick coding session in some scripting language - when experimenting with new language feature, trying out some idea or just throwing together some math. Until recently, there was no way to use Scala for this. While it had script execution feature, it was very slow - could take several seconds on each script restart. This happened because you had to load and initialize scala compiler every time - and it's not a small beast. Somewhere between 2.9 and 2.11, situation changed to better - now, instead of tearing compiler up and down, "scala script.scala" silently starts a compile server in the background, and subsequent scripts are sent to it, cutting down script execution time to less than a second. It would get even better if there was a way to run the script directly from my emacs session, without going to terminal. Turns out, it's easy to do. Just place the following function in your init.el: (defun run-current-file () ...

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