Category Archives: programming

Popular Science

Some times I recall an invention I read about in Popular Science when I was a wee lad and wonder what ever became of that? For example I recall an air conditioner who’s design was based on blowing a stream of air, at an angle, onto a surface. The magazine assured me that the air that then went one way was cooler than the air that went the other.

These days I read Science Blog for my science innovation porn. Today we have room temperature combustion using catalysts.

… “nano-catalytic reaction” with nothing but nanometer-sized particles of platinum stuck to fibers of glass wool in a small jar with methanol and air — with no source of external ignition. … “Since the caveman days, we have burned things to utilize their energy, and the high temperatures and the entire process have created a lot of problems that we’re then forced to deal with,” … solve the energy crisis is to replace our existing fuel consuming method with one that has much higher efficiency … the reactions can reach high temperatures of greater than 600 degrees Celsius and low temperatures of just a few tenths of a degree above room temperature …

Meanwhile Stefano points out some programming language porn. A wonderful hack. A cool trick. At the end of the little movie the inventor says “I hope I made you think.” Indeed!

Original Closures

I didn’t learn the term ‘closure’ until maybe 1978. I’m having trouble remembering when the first time was I used a language with them. Simula maybe? It predates Algol 68 I assume (update Algol 68 had only limited closures; see comments).

The language implementor discovers them the moment he: 1) allows a function to be defined nested inside another function; 2) allows the inner function to access the locals of the outer function, and 3) let’s the programmer get a handle to the function, i.e. treat it like data. You can actually get most of the fun of closures without a garbage collector if your clever.

They had been an issue in Lisp for years, but I didn’t have any Lisp experiance until the mid 70s.

It’s interesting how many of these ideas are as old dirt.

Where the eye falls

Via Gideon Rosenblatt a cool visual of where user’s eyes fall over google search results. This also answers a question I had. On a pull down menu the sweet spot is the second, not the first, item on the list. I’d wondered if that might be the case on a google search listing. This suggests not; though eye and click aren’t necessarily the same.

slashdot

Hm, suddenly popular


 RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
 RewriteCond %{QUERY_STRING} !^coral-no-serve
 RewriteRule ^archives/2005/03/yahoo-fickr/ https://enthusiasm.cozy.org.nyud.net:8090/archives/2005/03/yahoo-fickr/  [redirect,last]

 RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
 RewriteCond %{QUERY_STRING} !^coral-no-serve
 RewriteRule ^images/flickr.Jpg https://enthusiasm.cozy.org.nyud.net:8090/images/flickr.Jpg [redirect,last]

My thanks, to the kind folks at Coral content distribution network and Planet Lab, and ah Jeff.

Whitespace elimination

I solved my PDF whitespace problem using ghostscript and psutils. Ghostscript provides pdf2ps, and ps2pdf tools. psutil includes psnup which will paste p N pages per page. The power tool in the psutils family is pstops which will do a number of page layout transformations. If you then tinker you can find a combination of scaling, offset, which eliminates all the whitespace for the document in question. So, I have the technology, even if I don’t have an easy to use solutions … yet.

Negative lines of code per day.

Lines-of-code/day is a common bogus metric in primitive performance review systems. I have never seen anybody get a bonus for achieving a large negative number. That can’t be right! What whould a guy be worth to you if he could reduce the size of your system by 10-20%? more.

R scoping, assignment, confusion

In R lexical variables are implicitly introduced by assignment. It also appears that lexical variables default to the value they had in the enclosing scope.

> b = 2
> g <- function () { print(b); b <- 3; print(b) }
> b
[1] 2
> g()
[1] 2
[1] 3
> b
[1] 2
>

It’s unclear from that example if the first print of b in the function is referring to outer or the inner b.

But this example makes my brain hurt. Here we have x, who’s value is a matrix. When we assign to it even a single element of the matrix in the function it appears that R copies the entire matrix.

> x <- matrix(1:4,c(2,2))
> x
[,1] [,2]
[1,] 1 3
[2,] 2 4
> x[2,2] <- 22
> x
[,1] [,2]
[1,] 1 3
[2,] 2 22
> f <- function (z) { x[1,1] <- z; c(x[1,1],x[2,2]) }
> f(11)
[1] 11 22
> x
[,1] [,2]
[1,] 1 3
[2,] 2 22
>

It this point it unclear on how to write functions that have side effects on data structures. That certainly is a strange state to get into. The language reference manual talks about scopes, but it spends all its calories on explaining closures. If you read the help for the assignment operator help(“<-“) you discover there are actually five operators with two semantics.

> f <- function (z) { x[1,1] <<- z; c(x[1,1],x[2,2]) }
> f(11)
[1] 11 22
> x
[,1] [,2]
[1,] 11 3
[2,] 2 22

Notice the “< <-" operator. It preempts creating something in the inner most scope of the function. Instead it will run outward looking for an existing data structure to modify, finally creating a fresh variable in the global scope. The five operators are <-, ->, <<-, ->>, and =. The = looks like a vestigial organ; it’s identical to <- except for some severe limits on what contexts you can us it in.

I don’t think I quite have this all figured out yet. In this example we make a list of two elements, but we also give names to those
elements.

> guys = list(who = c("tom", "john"), age=c(12, 14))
> guys
$who
[1] "tom" "john"

$age
[1] 12 14

> names(guys)
[1] "who" "age"

We can then “attach” to that data structure so we can use the name directly.

> attach(guys)
> who
[1] "tom" "john"
> age
[1] 12 14
> mean(age)
[1] 13

Something similar to the automatic copy of a lexical variable is going on here as well.

> age[1] <- 13
> age
[1] 13 14
> guys$age[1]
[1] 12
> detach(guys)
> guys
$who
[1] "tom" "john"

$age
[1] 12 14

That’s kind of neat. The attach creates a scratch workspace where you can tinker with a data structure without doing damage to your master copy.

All this suggests to me that there might be a lot of lazy evaluation going on here. That’s plausible since we saw promises in the basic type suite.

None of this surprises me, this is the kind of stuff that happens in a very domain specific special purpose language. I just wish it was explained someplace in a way I could understand.

invisible

R has a function called invisible. Cool, I’ve never seen a language with that before. As far as I can tell this must place the object into a set that is then ignored by the printing machinery. I’m guessing that this set is cleared when ever the read/eval/print loop comes around for another bite to eat. … hm, looks like only the top level of the printing machinery cares.

> 1;2;invisible(3);invisible(4);5
[1] 1
[1] 2
[1] 5
>

Most of the GUI systems I’ve worked on had an invisible predicate used by the rendering mechanism. You need it first to keep from spending huge amounts of time rendering things too small to be seen. Once you have it you start using it give your views an attitude. Sending a message to the read/eval/print loop as to what to do the result being computed; I don’t think I’ve ever seen that before. Interesting idea.

R

I’ve been trying to add R to the tool bench I keep in my noodle. I’ve heard very good things about it, but my first impressions were crummy. The tutorial is weak, there’s no wiki, no IRC channel, the help list is hosted at Yahoo, the book is hundred bucks – mixed signals. And then, it’s community is full of very smart stats dudes, which is scary – they have special words for everything!

Finally I got around to reading the language reference manual. Oh boy! You can tell a lot about a language from the primitive types. In addition to the expected usual boring junk notice these: closure, promise, language, special, environment, expression, weakref. It’s the guest list for a really good party!

Great sentence: “All objects except NULL can have one or more attributes attached to them. Attributes are stored as a list where all elements are named.” You can build some really nice data structures given that, for example custom class systems. So when you want to have, oh time series, you can slip them in “The tsp attribute is used to hold parameters of time series, start, end, and frequency. This construction is mainly used to handle series with periodic substructure such as monthly or quarterly data.” Without messing with the language kernel.

Then somebody is having fun.

> class(d2)
[1] “data.frame”
> typeof(d2)
[1] “list”
> attributes(attributes(d2))
$names
[1] “names” “class” “row.names”

or

> 0:1
[1] 0 1
> 0:10
[1] 0 1 2 3 4 5 6 7 8 9 10
> 2*1:3
[1] 2 4 6
> 0:1*1:10
[1] 0 2 0 4 0 6 0 8 0 10
>

Very amusing, I like a cheerful language.