Archive for September, 2008

Help me write idiomatically correct Python

Monday, September 29th, 2008

Why can’t the features I like from every programming language be available all the time? It really bugs me that, for instance, I can’t get lazy lists and patter matching in Python and I can’t get, well, there’s got to be something that Python has that’s sort of a pain in Haskell. I/O, for example.

Anyway, I want code that does this:

import Prelude hiding (Left, Right)

data EitherBoth a = Left a | Right a | Both a a deriving Show

cmpseq :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a] -> [EitherBoth a]
cmpseq _ [] rs = map Right rs
cmpseq _ ls [] = map Left  ls
cmpseq cmp (l:ls) (r:rs) = case (cmp l r) of
                             LT -> Left   l : cmpseq cmp ls  (r:rs)
                             GT -> Right  r : cmpseq cmp (l:ls) rs
                             EQ -> Both l r : cmpseq cmp ls     rs

So given two ordered lists and a comparison function, I want to know (efficiently, and preserving order) which elements are just in one list and which are in both. The Haskell version is pretty clear and pretty concise. (Bonus points: if you have a better way to do this in Haskell — and I’m sure there is one — I’d love to hear about it.)

So far, the best I can do in Python is this rather ugly mess:

def cmpseq(f, xs, ys):
    nx, ny = len(xs), len(ys)
    ix, iy = 0, 0
    while ix < nx and iy < ny:
        r = f(xs[ix], ys[iy])
        if -1 == r:
            yield (xs[ix], None)
            ix = 1 + ix
        elif 1 == r:
            yield (None, ys[iy])
            iy = 1 + iy
        else:
            yield (xs[ix], ys[iy])
            ix, iy = 1 + ix, 1 + iy
    while ix < nx:
        yield (xs[ix], None)
        ix = 1 + ix
    while iy < ny:
        yield (None, ys[iy])
        iy = 1 + iy

Yuck. I’m going to go out on a limb and say that five yields is too many. What’s the idiomatically correct way to do this? Perhaps something to do with feeding values back in to generators? Python language lawyers: please show me the error of my ways!

Greetings from Ghent

Monday, September 8th, 2008

As it turns out, there’s no internets in rural France. So I’ve been completely disconnected since last Wednesday. I have half a post with events from Paris, but it isn’t done and I’m tired. Long story short: Paris was amazing. We did everything we wanted to do. Charente was beautiful and the wedding was amazing as were all other associated parties. (There were very many.) We’re in Belgium now and I have a cold, so I’m going to sleep.

Until I can get some more notes from the trip up (and photos — my camera decided to dissappear in Paris, so I’m hooked up to Stephanie’s fire hose of photos), here are the names of a few cities we’ve been in or near:

  • Villejesus, France
  • Viellemorte, France
  • Germeville, France
  • Fouqueure, France
  • Asse, Belgium

Greetings from Detroit (Rock City)

Monday, September 1st, 2008

And a good day to you! We started things very, very early today. Well, for us at least. After I was done showering and getting dressed and what not (and I am by no means quick in this regard), it was still… well:

Early. Very, very early. We made it to the airport without complication and had time to have some breakfast and do some lounging before the flight.

Of course you can all guess which airline we took:

Why fly anything else?

Five hours later and we’re in Detroit, Rock City. Our layover is about five hours. Plenty of time for a bite to eat, some Flickr uploading and a little exploring. Fletcher was quite taken by one of the fountains in the airport.

The terminal we’re in is crazy long. There are 78 gates in the terminal. Both sides have gates, and each is a little more than an airplane or so wide. After we ate we took a stroll down to gate 1 on the other end of the terminal.

While we were down there we had some spare time, so Stephanie busted out the yoga mat and did some of her travel workout. In some ways, having the huge layover is cool as it keeps us from being stuck on a plane for 14 or 15 hours. Braggable.

We took the NWA tram back up to our gate, as we only had three and a half hours before our flight left. I can’t say that it’s completely necessary, but it certainly is a cute little train. The “you have to wait this long” signs were particularly helpful.

Now we have some pretty serious time killing to do before we head off for Paris. More news as it happens. <sfx: Curried Soul>