(Runtime) Cost-free abstractions in clay

My own-time programming habits have been way down recently, which is a bit sad. Much of what I have been doing has been in clay though – partly a project of my own, partly contributing to the standard library (there’s been a cycle of “own project, ooh, this bit could possibly be in the standard library, let’s flesh it out…” resulting in probably more time spent on the latter than on my own).

One of my recent contributions has to been to custom sorting and ordering. My starting point was simple: I wanted to be able to sort by things which were not the natural order (in my particular use case I had a scoring function and wanted to sort things from “best” to “worst”).

I started out in the obvious way, and just let it take an argument that defined a custom < for the type - essentially just a function comparator argument. This was fine, and works perfectly naturally in clay (yes, clay is a systems language, but it's got a perfectly reasonable lambda system. The capturing rules are a little odd, but for things where you don't have to capture state it Just Works). I then took a leaf out of Haskell's book and defined a function comparing. In Haskell speak, comparing has the signature comparing :: (Ord b) => (a -> b) -> (a -> a -> Bool). (I’m not 100% sure this is the signature in Haskell, but it’s the equivalent signature to what I defined).

This all worked fine. But I found it a little annoyingly specific. What if I wanted to use <= in the sort? Ok, sure, you can do x <= y as not(x > y), but that’s a little awkward. And then what if I wanted to do ==? Again, you can define it in terms of <=, but that's a bit rubbish. Thus was born the comparators API. It’s very straightforward. Essentially the idea is that all of the standard functions that correspond to the comparison operators get overloaded to take a three-argument version where the first argument is a “Comparator” type (in clay this currently just means that Comparator?(T) returns true. There’s no true type-class system, only compile-time predicates which test types).

Having the comparator types, it was natural to define combinators for them. So far we’ve only got the basic ones (comparing, natural, and reversed), but it would be highly reasonable to define more.

But, I asked, you really want these operations to be fast. e.g. a sort lives or dies on how fast its comparisons are. How much overhead does using the comparators API introduce?

Err, none, it turns out. At least if you use the standard comparators with procedures for arguments (and most of the time when you use lambdas too). I’m afraid I’ve lost the examples, but I did some experimenting, and it turns out that with things like reversed(comparing(size)) and similar, clay actually just gives this a unique type which requires no data to represent, and type specialisation and inlining take over and the comparator literally completely disappears at runtime: I compared assembly output from using the comparator and writing the same code by hand, and they were literally byte identical.

Most languages (yes guys, I know you can do this in C++, or with a whole program optimising compiler like mlton) when you introduce an abstraction like this, the result would have a performance hit. Not a lot in many cases, but maybe you’re passing a dictionary at runtime, or introduce boxing overhead, or even just have to pass a token around which makes function calls slightly more expensive. Here you can introduce the abstraction, which can clean your code up a lot, without any runtime difference from the specialised code you would have written by hand. That’s pretty cool.

This entry was posted in programming on by .

New Comment Policy

Hi,

So you remember a while back I turned off comments on my blog? Some of you have probably noticed I re-enabled them recently.

I’m really glad I did. The quality of comments on my recent political stuff has been extremely high. There’s a lot that I’ve disagreed with, but it was largely for sensible reasons on which intelligent, rational, civilized people can disagree on – often one of us was wrong, sometimes we merely disagreed on some fundamental point. That’s fine. Disagreement is healthy, and lead to good discussions.

But I just want to emphasise that: Apparently we, the internet, are capable of having reasonable and mostly polite conversations about politics, but we can’t manage to do that about programming. That’s a bit sad, don’t you think?

Anyway, I’ve decided to leave the comments on for now, but I will be adopting a stricter moderation policy. If I think your comment isn’t worth responding to – either because you clearly haven’t read the article, because your comment is unnecessarily rude or inflammatory, or even just because I think you’re clearly an idiot, I will delete it rather than responding to it.

And remember: I’ve received nearly 100 comments in the last couple of weeks, none of which I have had to moderate. If they can manage, you can too.

David

PS. I know you’re just itching to write an idiotic inflammatory comment which has misread the article in response to this. Go ahead. Get it out of your system. Last chance.

This entry was posted in Admin on by .

What books have made you a better person?

This post is largely an aggregation of some conversation on twitter that I want to preserve for posterity and share with a wider audience.

Yesterday I asked:

Question: What books have you read that you’d say improved you as a human being? (Ethically, rationally, creatively, whatever),
I’m aware that’s a bloody hard question. I’m not sure I can answer it.

Here are the responses, in roughly chronological order.

  • @alexjs: 1984 taught me at an early age to question misinformation. HHGTG taught me that ignorance can be even better…
  • @mdreid: Singer’s How Are We To Live. DFW’s Infinite Jest. Feynman’s QED. Mandelbrot’s Fractal Geometry of Nature. Coetzee’s Youth.
  • @etorreborre: I think it was this one: http://amzn.to/kl9Mjv (psychology)
  • @davidpeto: American Psycho.
  • @riffraff: excluding others already suggested, “King Solomon’s Ring” by konrad lorenz, taught me “human” behaviour is not so human after all
  • @wgren: Stephen J Gould – “The Mismeasure of Man”. Charles Stross – “Accelerando”. Carl Sagan – “The Demon Haunted World”.
  • @newsmary: In that case, Faulkner’s Absalom, Absalom! tends to be the first one I recommend to storytellers.
  • @illyrica: I Fought the Law, Dan Kieran. Chinese Whispers, Hsiao-Hung Pai. Contingency Irony & Solidarity, Richard Rorty
  • @cemenzel: 1984; The Demon-Haunted World; The God Delusion. None really changed me much, but made me more aware.
  • @lordcope: Full Catastrophe Living; Getting Things Done; The Little Schemer; Crucial Confrontations; 7 Habits; Getting to Yes.
  • @reyhan: Neuromancer and The Adventures of Endill Swift
  • @channingwalton: zen and the art of motorcycle maintenance, Tao te Ching,Timeless Way of Building (look at things differently)
  • @jarhart: Working backward – Learn You A Haskell; Sex, Ecology Spirituality; No Limit Hold ‘Em Theory And Practice; Rich Dad, Poor Dadu
  • @wgren: Also- Diamond: “Collapse”. Wright: “Remembering Satan”. And ofc 1984, Brave New World, Postman: “Amusing ourselves to Death”
  • @toluju: Not sure if it’s been mentioned, but Sophie’s World has a pretty big effect on me.
  • @kssreeram: “If you want to write” by Brenda Ueland. More than just writing, that book is about self-expression and creativity.
  • @DanielJMaxwell: Domain Driven Design by @ericevans0. It looks technical but really it’s about communication. Every human being should read it.
  • @dylanbeattie: ‘Chess for Young Beginners’ – the book that taught me that you can learn things from books. I can remember every page vividly.
  • @dylanbeattie: Chaos (Gleick). Every Rough Guide I ever read. Andrew Martin’s “How To Get Things Really Flat”, the first Dirk Gently novel…
  • @obs3sd: To Kill A Mockingbird has stuck with me for over 30 years
  • @dylanbeattie: …and Leonard da Vries Book of Experiments. Neuromancer. Neal Stephenson’s Baroque Cycle. Does the Jargon File count?
  • @paraseba: If “Les Misérables” doesn’t improve you, I bet you’re a statue
  • @runarorama: Ethically: Getting Things Done. The Virtue of Selfishness. Hávamál. And of course The Nicomachean Ethics.
  • @palfrey: Godel, Escher and Bach
  • @dcsobral: The Players of Null-A, Hellspark

Feel free to add more answers in the comments.

Myself, I still don’t have a good answer. But maybe when I’ve got through the above list I will.

This entry was posted in Books, Uncategorized on by .

A plan for not failing

After a number of posts about something I don’t usually talk about (politics and voting systems) I decided it was time to get the blog back on theme (although to be honest there are going to be more posts along those lines. The theme of this blog is, and will remain, whatever I happen to find interesting at the time of writing).

Err. By which I mean this is a post about cooking. Sorry if you were hoping for some programming goodness. There will be some eventually, I promise. Probably about Clay. Or maybe about why Ruby sucks and should be set on fire. We’ll see. But for the moment, on to the plan!

As some of you might know I live with my brother Jamie – I was looking for a flatmate, he was looking for a job, and decided that London would be a better place to find one, so he moved in with me. It’s worked out well.

We do a lot of cooking together, but we noticed that we were being quite rubbish – we were often falling back to really lame meals and a lot of food was getting thrown away. We felt bad about this, and enacted a plan to fix this. It’s extremely simple, and many of you probably already follow it, but has worked very well:

Before we do our weekly shop, we come up with a list of meals for the week. These don’t have to be haute cuisine – they’re stuff which we’re reasonably going to make. We usually try to make sure it contains something interesting (this week we’re experimenting with how to do a vegan pie. We’re not vegan, but occasionally need to feed them, and the recipe concept sounded tasty), but it also contains easy things like pizza or macaroni and cheese.

We don’t plan things out for specific days – it’s just a list. This means that we can pick something off the list appropriate to our energy levels.

We’re also not obligated to actually follow the list: If we can come up with something better on the night (with the ingredients we have to hand) we’re totally welcome to do that. This doesn’t mean we’re allowed to just go “I can’t be fucked to cook properly tonight. Let’s just do pasta”, but it does mean we’re allowed to go “You know, we have all sorts of vegetables that would go well in a stir fry. Let’s do that instead”.

So really the list only has two purposes: It guides us in our shopping, and it gives us a plan in advance so that if we’re lacking in inspiration we can just pick something off the list.

For all of that, it’s worked incredibly well. In particular:

  • Previously we were probably failing to do anything sensible and eating pasta, or bread and hummous or similar for dinner at least once a week if not more. We still do occasionally, but the frequency is probably down to under once a month
  • Previously we were throwing a lot of food away because we didn’t use it in time. This still occasionally happens, but it’s now a rare occurrence.
  • Our weekly shops have become much smaller. Previously we were spending probably somewhere slightly under £70 / week, and now we’re typically somewhere around £40 – £45. This isn’t through any conscious attempt to save money, it’s just something that’s emerged automatically from this plan.

Maybe this is all obvious and you’re all already doing it. It’s certainly not the most ground-breaking idea in the world, but I must admit to being regularly blown away by just how well it’s worked as a strategy.

This entry was posted in Food on by .

I’m mentioned (and misrepresented) in the New Scientist

So my slightly crazy article about random ballot was mentioned in the new scientist.

This is, as you might expect, quite exciting. Unfortunately the article gets one really crucial thing about me wrong, and my comment clarifying this on the site has yet to be approved, so I just want to make this clear:

I am not actually advocating random ballot

Observe the following quote from the original article:

I think it has desirable and undesirable features (in particular there’s one really major problem with it), but even with the above list I’m not sure I actually endorse it. Consider this a thought experiment more than a proposal.

“perfect” was in scare quotes for a reason. It has some very nice properties, but it also has a lot of problems (both theoretical and practical). I have no interest in promoting it as the system we should actually be using, it’s just an interesting system to think about it. The reality is that (while it’s not necessarily the system I would have most preferred) I am extremely strongly in favour of the move to AV and will be voting yes on May 5th.

Update: Jacob has now fixed this, mentioning I am “highlighting” it rather than “pushing” it.

This entry was posted in voting on by .