header image
 

Extending SML’s lists

I’ve been busy working on a project in SML/NJ, and in the process I ended up writing a few generic list functions. I’ve extracted these into a separate module, and I’m putting it up here. Warning: These are just quick hacks (but they work (I think!)), and not all of them will be useful outside of my project.

Briefly, here’s what this contains:
A specialized version of foldl, a join function similair to Ruby’s, list generating functions and range functions, map and app functions that also pass in the index of the element, and a special kind of cross product function.

Anyway here goes (sorry for the meaningless syntax highlighting, SML/NJ is of course not supported by the WordPress sourcecode plugin):


structure ListExtension = struct
  exception InvalidArgument of string;

  (*
  * folds left across a list, applying a binary function pairwise to its members.
  * Eg, foldLeft f [a,b,c] = f(f(a,b),c);
  *)
  fun foldLeft f [] = raise InvalidArgument "Empty list to foldLeft"
    |foldLeft f (h::[]) = h
    |foldLeft f (h::t) = foldl (fn (a,b) => f (b,a)) h t

  (*
  * 'joins' a list into a string, converting each member into a string by the
  * given function, and interspersing the given string
  *)
  local
    fun join_aux s f (h::t) res =
        if res = "" then
          join_aux s f t (f h)
        else
          join_aux s f t (res ^ s ^ (f h))
      |join_aux s f [] res = res
  in
    fun join s f h = join_aux s f h ""
  end

  (*
  * Convenience function when the conversion is identity
  * eg joinString "+" ["a", "b", "c"] = "a+b+c"
  *)
  fun joinString s h = join s (fn a => a) h

  (*
  * List generators.
  *)
  local
    fun step_aux n inc lim f l =
      if (inc > 0 andalso n > lim) orelse
         (inc < 0 andalso n < lim) orelse
         (inc = 0) then
        l
      else
        step_aux (n + inc) inc lim f ((f n)::l)
  in
    fun step start fin inc f =
      if start <= fin then
        List.rev (step_aux start inc fin f [])
      else
         List.rev (step_aux start (~inc) fin f [])
  end

  fun downfrom n f = step n 0 1 f
  fun upto n f = step 0 n 1 f
  fun fromto start fin f = step start fin 1 f

  fun range from to = ste from to 1 (fn i => i)
  fun rangeStep from to inc = step from to inc (fn i => i)

  (*
  * Map with index. Same as List.map, but also passes in the index as an
  * argument to the given function
  *)
  local
    fun mapi_aux f [] i res = (List.rev res)
      |mapi_aux f (h::t) i res = mapi_aux f t (i+1) (f (i,h)::res)
  in
    fun mapi f l = mapi_aux f l 0 []
  end

  (*
  * app with index. Same as List.app, but also passes in the index as an
  * argument to the given function
  *)
  local
    fun appi_aux f [] i = ()
      |appi_aux f (h::t) i = (f (i, h); appi_aux f t (i+1))
  in
    fun appi f l = appi_aux f l 0
  end

  (*
  * Applies the function f to each element of the cross product of the two
  * lists.
  * f(a,b) is discarded if it is NONE, if it is SOME(c) then c is added to the result
  *)
  local
    fun crossProduct_aux f (h::t) l result =
      crossProduct_aux f t l ((List.mapPartial (f h) l) @ result)
      |crossProduct_aux f [] l result = result
  in
    fun crossProduct f (a, b) = crossProduct_aux f (List.rev a) b []
  end
end

Suggestions/ additions are welcome of course.

Ubuntu hardy upgrade problems

I got hardy yesterday, and the upgrade wasn’t without problems. I’m posting my solutions here: hopefully they’ll help someone else.

Nvidia

First, Ubuntu wouldn’t let me upgrade, saying “Couldn’t calculate upgrade”. A peek at the logs (at /var/log/dist_upgrade/) revealed that this was because of nvidia-glx. It needed to be removed (because there is now a nvidia-glx-new), but it was in the removal blacklist. So I thought I’d do this manually. I removed nvidia-glx and installed nvidia-glx-new with apt, and then tried upgrading again.
I consider this a bug in the upgrade system. The installer should at least tell me that this might be the issue, and that I should consider changing my drivers, because if I hadn’t known about the existence of nvidia-glx-new, the log alone would have been of no use.

Ok, having done that I tried dist-upgrade’ing again. This time it looked like it was going to work. But then it threw up errors about nvidia-glx-new, and every single xorg package. I tried reinstalling nvidia-glx-new, but dpkg didn’t let me (I kept getting “subprocess post-removal script returned error exit status 2″). Google led me to this:

sudo dpkg-divert –remove /usr/X11R6/lib/libGL.so.1
sudo dpkg-divert –remove /usr/X11R6/lib/libGL.so.1.2
sudo dpkg-divert –remove /usr/lib/xorg/modules/libGLcore.so

However dpkg-divert gave me “No such file or directory” for each of the libgGL’s. After some headscratching, I realized the reason was that there was no ‘lib’ directory in ‘/usr/X11R6′. So a ‘mkdir /usr/X11R6/lib’ and then dpkg-divert –remove fixed the problem.
I still don’t understand how this happened in the first place - I’m just happy I got it to work.

Terminal Fonts

After hardy was installed I noticed that my Terminal had stopped antialiasing its fonts. Here’s the fix:

cd /etc/fonts/conf.d
sudo rm 10-hinting-medium.conf
sudo rm 10-no-sub-pixel.conf
sudo ln -s ../conf.avail/10-hinting-full.conf
sudo ln -s ../conf.avail/10-sub-pixel-rgb.conf

Firefox

Now my one remaining gripe is with Firefox. ff3b5 is highly unstable for me. I used Beta 4 up until now, and that worked just great. beta 5 has crashed on me 4 times just while writing this blog post! (although that’s an extreme case.. it isn’t that bad normally).
However, I may have found a solution. I’m beginning to think its firebug that’s causing the crashes, but I’m not sure yet. Any other ideas?

Haskell ? : The ternary operator


? :: Bool -> (t, t) -> t
a ? (b, c) = if a then b else c

And there you have the Haskell ternary operator. Nice syntax, and lazy evaluation means that only one of b or c is ever evaluated!

Simple Javascript Progress Bar

Here’s a quick and obvious-in-hindsight progress bar that I created while working on a project:


<div id="progress" style="border: 1px solid black;">
  <div style="background-color:green; width:50%">&nbsp;</div>
</div>

Now just use Javascript to change the width of the inner div!

Of course there can be any number of variations on this theme. For example, a repeating background-image can give a nice effect.

Or how about a discrete bar instead with say, five stars? Just use five image tags, then if you have these image elements in the array ’stars’:


progress = Math.round (progress/20); //progress was a percentage
for (i =0; i < progress; i++) stars[i].src = "filled_star.gif";
for (i = progress; i < 5; i++) stars[i].src = "empty_star.gif";

A look at Gobo Linux

I came across an interesting Linux distro today, called GoboLinux. Gobo brings about a radical change in the way your filesystem is organized. Instead of having your programs scattered all over, Gobo organizes the filesystem in a much more sane manner (because lets face it, there may be a method to the madness that is the Linux filesystem, but for most purposes, it’s still madness). In Gobo, doing an  ls /  will give you:

Programs
Users
System
Files
Mount
Depot

You can easily tell whats going to be where. For example the /Programs directory has… well all your Programs! But the best part is, if you had two versions of Foo, they’d be in /Programs/Foo/1.0/ and /Programs/Foo/2.0/. In effect, the filesystem is now your package manager. Thats a really cool way of looking at package management. You install a program into /Programs (Gobo comes with a nice path-agnostic compilation system called “Compile”), and then if you want to uninstall, just delete its directory. Want to know which program a file “belongs” to? That’s natural as well, with this hierarchy.

Of course everything cannot possibly be so simple, and Gobo has an uglier side. First of course, the filesystem-as-package-manager cannot by itself deal with dependencies. Compile handles dependencies, but if you’re going to slap a package manager on top anyway, then you’ve lost some of the elegance you gained with this new filesystem hierarchy.

What’s worse is that Gobo needs to litter all the directories with symlinks to handle shared files, etc. What’s even worse is that Gobo still maintains the the traditional hierarchy alongside the new one! Without it, they would go straight to compatibility hell. So they still have /usr and friends, but these are now symlinks to their Gobo counterparts. They’ve even made a kernel extension (called GoboHide) to hide this ugly underbelly from your eyes (GoboHide is not strictly required, it’s only for hiding the traditional hierarchy).

I can’t help get the feeling that I’ve been sent back to square one :). Of course, that’s not really true, and my gripes are aesthetic more than anything else. Gobo really has done something pretty innovative. But wouldn’t it have been easier to just use fuse to achieve the same goal? Don’t touch the filesystem hierarchy, don’t make a new distro, just make a filesystem with fuse, that acts as a different “view” of all your files.

I admit I haven’t thought too much about it, but it seems possible to extend this to a package manager that would masquerade as a filesystem. You would certainly be able to get the /Programs coolness, and an rm -rf /Programs/Foo/1.0, would uninstall version 1.0 of Foo. You might even be able to manage dependencies within the confines of the filesystem-as-package-manager paradigm. On the other hand, it could just be a hook into an existing package manager, and still provide many of the advantages of Gobo. Food for thought, and perhaps for an interesting project at some point in the future!

As far as Gobo itself goes, I think its great that they’ve done something so innovative, and I wish them luck. Only problem is, a nicer filesystem hierarchy is not by itself a strong enough motivation to switch distros (especially since I’m perfectly happy with my package manager). Maybe as Gobo matures it’ll be worth looking at again.

Mumble your tests!

mumbles screenshot
I’ve been working on a rails project, and in some screen cast, I saw autotest+growl and fell in love. I googled a bit for a Linux version of growl, and I found Mumbles. There was no built in autotest support, so I hacked around, extended the Mumbles dbus interface just a little, and then made a simple autotest hook that uses Mumbles.

Until the author commits my patch to his repository, you can get my patched version as a tarball here: Mumbles patched (its a bziped tarball, not a jpeg file - wordpress limitations, so right-click and save as)

And here’s the autotest plugin:


require 'dbus'

def send_message(title, message, icon)
  begin
    bus = DBus::SessionBus.instance
    mumbles_service = bus.service("org.mumblesproject.Mumbles&quot ;)
    mumbles = mumbles_service.object("/org/mumblesproject/Mumbles&quot ;)
    mumbles.introspect
    mumbles_iface = mumbles["org.mumblesproject.Mumbles"]
    sig = mumbles_iface.signals["Notify"]
    bus.emit(mumbles_service, mumbles, mumbles_iface, sig, title, message, icon)
  rescue Exception => e
  end
end

Autotest.add_hook :ran_command do |at|
   begin
     output = at.results.last.slice(/(\d+) examples?, (\d+) failures?(, \d+ pending)?/)
     if output =~ /.*[1-9] failure.*/ then
       send_message("FAIL", "#{output}", "fail.png&quot ;)
     else
       send_message("PASS", "#{output}", "pass.png&quot ;)
     end
  rescue Exception => e
  end
end

You’ll need the ruby-dbus library which you can get here

*Update:* surrounded the hook with begin rescue. Without this, autotest would crash if I had a syntax error in my spec, because at.result.last would be nil

chwnd!

I did something incredibly stupid.
I wanted to do this:


sudo chown <me> somedir/*

Instead, I did this:


sudo chown <me> /*

Oops! I realised what was happening, and Ctrl+C’d. When I tried to put things back to normal, I realised i couldn’t sudo. sudo gave me some error about /etc/sudoers having the wrong uid. It turns out this was because /etc/sudoers was one of the things that got chowned before I stopped chowning things.

So if you’re ever in such a situation, here’s what you do:
In ubuntu, simply boot into recovery mode, with other distros, you may have to manually log in as root. Then chown your sudoers, and anything else that you messed up and reboot.

What I did was simply this:


chown -R root:root /bin
chown -R root:root /boot
chown -R root:root /etc

Those being the only three dirs affected. However, there may have been files that should belong to me and shouldn’t be chowned to root (there weren’t in this case, but if I hadn’t Ctrl+C’d fast enough, there would have been such files). So really, I should be checking for files with me as owner, but root as the group, and then only chowning those. In which case the following might be a better way (might because its untested)


ls -Rl / | grep "<yourusername>[[:space:]]root"  | awk '{print $8}' | xargs chown -R root:root

Of course this wont work if you have spaces in your filenames. Just change the awk part appropriately (it’ll end up uglier!)

bash fun: alarm clock

Over time I’ve become immune to most kinds of alarm clocks. The only thing loud enough to wake me is music coming from my 5.1 speakers. But I’m really particular about what I want to wake up listening to, and it can’t be the same song every day. So, I came up with this thing:


#!/bin/bash
function createAlarm
{
  command="gst-launch-0.10 playbin uri=\"file://"$1"\""
  echo ${2}" "${3}" * * * "${command}|crontab -
}
if [ -z "$1" ]; then
  echo Usage: $0 " "
  exit
fi

if [ -z "$2" ]; then
  min=00
else
  min=$2
fi
if [ -z "$3" ]; then
  hr=07
else
  hr=$3
fi

pattern=$(echo "$1" |sed -r -e 's/\s/*/g' -e 's/.*/*&*/' ;) 

IFS=$'\n'
declare -a results
results=($(find -H ~/Music/ -iname "$pattern" -printf "%p\n%f\n"))

if [ ${#results} -eq 0 ]; then
  echo "No results"
  exit;
elif [${#results} = 2]; then
  #dont show menu
  createAlarm ${results[0]} $min $hr
else
  for i in `seq 1 2 ${#results[*]}`
  do
    echo $((($i+1)/2))&quot ;) "${results[i]}$'\n'
  done
  echo -n "select# "
  read i
  createAlarm ${results[$(($i*2-2))]} $min $hr
fi

It searches through my music based on the keywords I give, then it creates a crontab which uses gstreamer to play the song. Incredibly useful, and much faster than navigating a GUI to find a song, set a time, etc like I used to do before. Try it!

Installing Ubuntu

As I clap my hands,
with the echoes, it begins to dawn–
the summer moon.
-Basho

I finally made the decision to install Ubuntu. I had been running it off VMWare for really long, but I always knew I had to switch eventually. After several long hourse of defragmentation (with 3 different defragmenters!) I finally managed to get the Vista disk manager to shrink my partition enough to get myself a measly 8 Gb of space.

Anyway installation was no problem, what I really wanted to do was restore my system as it used to be on VMWare. Remastersys seemed perfect, but sadly didn’t work for me. The author was really helpful, but given the amount of time I had spent defragging, I was way too impatient to wait for a fix. There are other ways to duplicate your system, but they involve re-downloading all your packages anyway, so I decided to do it manually.

By the time everything was ready it was really late and I was exhausted. Still, I feel an irresistible urge to tweak things. Also I need to launch a hunt for alternatives to some of the Windows only programs I had gotten used to. There goes the rest of the week!