I can never remember Scala‘s notation to indicate a covariant type vs. a contravariant type, so here it is:
Covariant [+A]
List can contain an object of type A or any subtype of A:
class List[+A]
Contravariant [-A]
Function1 accepts as an input an object of type T1 or any supertype of T1 (its return type R, however, is covariant):
trait Function1[-T1, +R]
Upper Type Bound [T <: A]
The following findSimilar function (borrowed from this example) accepts an instance of and a List of Similar objects or of objects that are subtypes of Similar:
I love humor with depth. I’ve lately been enjoying some metahumor—humor that alludes to a detail related to the work, not the story of the work itself:
In Harry Potter & The Half-Blood Prince, Professor Slughorn never can quite remember the name of Harry’s friend Ron Weasley. Slughorn tells Harry that he has “a house-elf taste every bottle after what happened to your poor friend Rupert.”
Rowling is clearly nodding to the fact that the actor who portrays Ron in the movies is a fellow by the name of Rupert Grint. [chuckle]
In a recent episode of Phineas & Ferb, Lawrence (Ferb’s father) is watching television. (We don’t see what he’s watching, just the flickering of light on his face.) Finally he pipes, “This isn’t much of a horror movie! Where are all the rock & roll numbers?”
Now that’s pretty funny by itself in a Monty Python-esque non sequitur sort of way. But it’s bloomin’ hilarious if you realize that the guy who voices Lawrence Fletcher is Richard O’Brien, the man behind The Rocky Horror Picture Show, a musical “horror” film. Brilliant! (Rocky Horror is not exactly a film I recommend, by the way, but I must confess that I have seen it.)
Also in a recent Phineas & Ferb (perhaps the same episode), they did a takeoff of one of the Twilight movies (which they called something like “Almost Dark”—heh, heh). The werewolf character is voiced by none other than Michael J. Fox, the original ’80s Teen Wolf. Genius!
Ever wanted to iterate over some objects using Java’s for each syntax, but the only interface exposed is an Enumeration? I have.
There’s an exercise in the Head First Design Patterns book that adapts an Enumeration to the Iterable interface. Pretty nifty, so I coded it up and used it in some code at work. Works great!
But then I found this: the list() method of the Collections class. It stuffs all the items in an Enumeration into a List. And it’s been there since Java 1.4! Where in the world have I been?
Anyway, the upshot is that instead of having to use this old drivel
Enumeration<Nargle> nargles = Nargle.getNargles();
while (nargles.hasMoreElements()) {
Nargle nargle = nargles.nextElement();
// do something with nargle
}
You can do this
for (Nargle nargle : Collections.list(Nargle.getNargles())) {
// do something with nargle
}
I have lived all 35 of my years in Limestone County/Athens, Alabama. I never remember having snow on Christmas. The most we hope for around here is a wet Christmas, but never a white Christmas. Really, for most of us around here, “just like the ones I used to know,” is simply a bald-faced lie.
Well in outright protest against history, we awoke this morning to this:
Early morning snowfallSnow-covered wellWhite Christmas on the front porchSnowy swing set
We Southerners hardly know what to do with ourselves.
(Update: Evidently I have forgotten that we had snow on Christmas in 1989. Nevertheless, it was nothing like this.)
I’ve been trying to put some of CSS3’s nth-* selectors to use in a site I’m working on right now, and I’ve run into a problem. I’m testing my markup and styles in Safari 5 (before moving on to other browsers), and I am trying to style child elements 3, 7, 11, etc. So I write this rule:
p:nth-of-type(4n - 1) { … }
That should work, right? After all, 4(1) − 1 = 3; 4(2) − 1 = 7; 4(3) − 1 = 11.
Well I open up Safari, and my changes haven’t taken. OK. How about a different but equivalent equation?
p:nth-of-type(4n + 3) { … }
Again, pretty simple: 4(0) + 3 = 3; 4(1) + 3 = 7; 4(2) + 3 = 11. The last one hasn’t taken, for whatever reason, but this one’s right on.
Still nothing.
I begin to doubt my math skills. I’ve been doing software of one type or another for 15 years, but my bachelor’s degree is in electrical engineering. I had to take four calculus classes, linear algebra, and differential equations. OK, I haven’t used most of that in years, but an + b is simple algebra—stuff I’ve been doing for over 20 years. I know simple algebra.
I decide to fire up Firefox (version 3.6.12). Lo and behold, there is the formatting I’ve been trying desperately to get to show up. Firefox gets it (so does Opera, for the record), but Safari doesn’t (neither does Chrome). Evidently we have a Webkit bug.
For grins, even though it won’t get me where I want to go, I try this:
p:nth-of-type(4n) { … }
The formatting appears correctly in both Firefox and Safari! Therefore Webkit does understand nth-of-type, but something about an + b gives it heartburn that an doesn’t.
I try one more thing: remove the whitespace in the equation.
p:nth-of-type(4n+3) { … }
It works! Safari and Firefox both rendered the formatting properly. Webkit just doesn’t care for the whitespace.
But is whitespace forbidden in the equation? Here’s what the spec has to say:
Whitespace is permitted after the “(“, before the “)”, and on either side of the “+” or “-” that separates the an and b parts when both are present.
So then, we do have a bug. Webkit doesn’t respect the whitespace in nth-child and the other nth-* selectors. The workaround is easy enough, but it’s going to be hard for me to break the habit of adding space around the arithmetic operator. It’s a best practice, so far as I’m concerned, for code readability. Nevertheless, the expressions are simple enough that it’s not a terrible price to pay to get Safari/Chrome to play.
If you want to see it in action, check out this test file. Below is a screen capture of Safari’s rendering (left) and Firefox’s rendering (right). As you can see, Safari only renders nth-child(2n+1) correctly while Firefox renders them both correctly.
nth-child Rendering Comparison (Safari vs. Firefox)