dtm: (Default)
So here's a bit of a geeky post, but not so terribly geeky that I'm hiding the bulk behind a cut link.

"true" and "false" are simple concepts, right? Different programming languages seem to have hugely different ideas of what they are:

In C, 0 is false, any other integer is true, and the coercion rules make a null pointer false, and any other pointer true. Anything else is a compile error.

C++ follows (inherits?) the C rule, and adds true and false as things that are true and false.

In shell, 0 is a true exit value; any other exit value is false. Inside [ ], empty strings are false and other strings (such as "0" or "1") are true.

In Python, 0, 0.0, -0.0, False, None, the empty string, empty tuples, lists, and dicts are all false. Anything else (such as the string '0') is true. (A note on 0.0 vs -0.0: they're both == according to python, and both false, but print differently. One can be turned into the other by multiplying by -1)

In Perl, 0, the empty string, and undef are false, and since evaluation as a boolean imposes scalar context, an empty array or hash variable is also false. Anything else (including arrayref or hashref values to empty structures) is true. Note that because perl helpfully autoconverts like a $SOME_NOUN_HERE, the string "0" and 0.0 are to perl identical to 0, and therefore false. (hence the Perl idiom "0 but true") A full discussion of the quirks of perl around various representations of zero could probably fill a small book, but I'll note that other consequences are that the expression -0 is true, but -0.0 is false.

In Ruby, false and nil are false, anything else (including 0, the empty string, and an empty Array or Hash) is true.

In Javascript, 0, null, undefined, the empty string, false and NaN are false. Anything else (such as an empty Array, or empty Object) is true.

In Java, (since the autoboxing introduced in Java 1.5) true and Boolean.TRUE are true, false and Boolean.FALSE are false, ((Boolean) null) is a runtime exception, and anything else is a compile error.

In Haskell, True is true, False is false, and anything else is a compile error.

Google Go behaves like Java 1.4 or Haskell - true is true, false is false and anything else is a compile error.

Scala appears to behave the same way - true is true and false is false. Anything else (including an instance of RichBoolean, or of java.lang.Boolean) is a compile error.

In Common Lisp, nil, aka the empty list, is false. Anything else - including 0, the empty string or empty vectors of other types - is true.
dtm: (Default)
Because I've recently read an article on how Microsoft has egg on its face because of this, and because searching on Google for "sort a list randomly in javascript" currently shows the wrong thing to do in the top several hits, here's how to take a list in javascript and shuffle it, or sort it in a truly random order.

Also, yeah, it's been over two months since I last posted, etc.

However, this is just going to be code, and anyone who still has me in the old friends list probably doesn't need their livejournal covered with code, so it goes behind a cut...
Read more... )

Magic spot

Jun. 24th, 2004 10:41 am
dtm: (Default)
For years, whenever my father has been learning a new programming environment (being a programmer since the late 60's, he's gone through a few), especially a new GUI environment, one of the things he'd implement as a test is a game he invented in the early 80s called "magic spot". It's a stupid little game, but it works as something to implement and get ones feet wet in a new environment.

Anyway, he's somehow avoided learning any HTML or related technologies until now, when he suddenly has to rewrite a huge bunch of asp forms with embedded client-side scripting.

So he's been picking my brain on and off and the other day sent me an initial implementation of magic spot with client-side VBScript. Of course, I can't view that in Mozilla, so I responded with my own version which will take advantage of client-side javascript if available, but will work just fine if javascript is turned off. (and works in both mozilla and ie, but may not work too well in old netscape 4)

An easier (and faster-loading) version

The source code

If you look at it in a browser that supports the css attribute :hover on arbitrary elements (like mozilla), you get the added benefit of being able to easily see which spot you're clicking.

Edge cases

Mar. 2nd, 2004 01:02 pm
dtm: (Default)
Edge cases are hard. Off-by-one errors are painful. (see also page 25 of this pdf)

Why, then, do programmers seem to delight in creating edge cases for themselves?

I remember as an undergrad. helping out in the CS lab at Carleton. One of the first very painful assignments for people in the second CS course in our CS sequence involved making and using singly linked lists. Thorny edge cases all over the place, and with pointers too.

Now, there are some edge cases here, but not nearly as many as people think, if they simply use slightly different variables.

C singly-linked-list digression )

Maybe special cases are the kind of thing that creeps up on you - it's easy to do just one, but soon you end up with a dozen tricky edge cases because adding each one was easier than re-thinking your algorithm. However, I often find that special cases are added in preference to using a few extra variables, or allocating a little bit more memory.

I'm thinking about this because I recently dealt with someone at work being burried under special cases. The specific problem has been simplified; I'll include it in an lj-cut below. When he called me, he'd just gotten through trying to work out how to handle all his different test cases, which were ending up in at least four tricky edge cases, and several flag variables that he was having trouble keeping separate. In fact, I was able to simplify the main loop to have just three if statements (none nested), and two of those if statements were identical.
Fun with text manipulation in perl )
dtm: (undefined)

What I've been programming on like mad these past few days is SMML - St. Mary's Music Library. At least search now works, even if I don't have the log-in-so-you-can-comment bit working yet.

Jennifer's been cataloging the assorted sheet music that has accumulated in the chior room at our church on a scrounged computer (built mostly out of bits of machine I've cast off, plus a donated old vga monitor from work and a yard-sale dot matrix printer) using an old package called dbman and lynx.

Well, that has a few problems. First off, dbman isn't a very functional program to begin with; secondly, we need to advertize for them if we put it up somewhere so that other people are able to search with it.

So I'm a programmer, right? So it should be a simple matter to put something like this together. I mean, it's just throwing up a database based on DBD::CSV, and writing a few pages with Mason. These choices were made because dbman stores things in a file format that DBD::CSV can use, or at least I thought it did. It turns out that DBD::CSV is a bit picky about some things.  I chose Mason because I thought it would be easy to set it up in an environment where all I can set up is a cgi script; actually, that was true, modulo having to work around the fact that the webserver's chroot contained almost no perl modules.  Plus, if it becomes possible to move to a mod_perl setup, Mason will let me do that.

So two weekends and several long nights later, the searching that shouldn't take me "more than a few hours" is up and functional. (Lots of stupid stuff I ran into along the way - including an interesting bug that vanished when I looked at it with the debugger, because the condition I set on a breakpoint caused an integer comparison to happen. perl's string<->number equivalence can get whacky) Now I need to add everything necessary to let Jennifer make changes, and then I can work on allowing other people to add annotations (maybe; we're still not certain that there's any need for this feature).  The changes bit is interesting since I have to keep three copies of the database in sync - one on cush (my home machine), one on snowplow, and one on phoenix (the machine sitting in the chior room basement).  Fortunately only one person is going to be making any changes.

The search language parser is a neat hack, if I do say so myself. I should document it sometime as an example of leveraging perl's parser to do your work for you.

While taking a break I ran across some old pictures I had taken while in Baltimore, and my lj picture needed changing.

May 2017

212223242526 27


RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 24th, 2017 12:50 pm
Powered by Dreamwidth Studios