This is clever: Data Gravity.
Friday, June 29, 2012
Atomic weapons
How can you beat this for a conference session title?
atomic<> Weapons: The C++11 Memory Model and Modern Hardware
(h/t the Herb Sutter)
Wednesday, June 27, 2012
A string of pearls
Stuart Archibald at OpenGamma has published a series of tour de force posts. The lastest is Accessing Native Maths Libraries. FORTRAN, gcc, code generation, dynamic linking, magic code glue. What's not to love?
Tuesday, June 19, 2012
Time lies when you're calling functions
Noah Sussman notes Falsehoods programmers believe about time. My favorite:
- You can't be serious.
Nearly as good:
That thing about a minute being longer than an hour was a joke, right?
No.
Generating random Java strings
I found lots of misfitting advice searching Google for how to generate random strings in Java. The misfits assumed I was generating unique keys, often for web page cookies.
What I searched for was random Java strings along the lines of new Random().nextInt()
, to use for probabilistic testing, but I failed to find this so I rolled my own.
Not a thing of beauty, but accomplished by goal. Hopefully someone might find this useful:
String randomString(final Random random, final int minLength, final int maxLength) { final int length = random.nextInt(maxLength - minLength) + minLength; final char[] chars = new char[length]; for (int i = 0, x = chars.length; i < x; ) do { final int cp = random.nextInt(0x10FFFF + 1); if (!Character.isDefined(cp)) continue; final char[] chs = Character.toChars(cp); if (chs.length > x - i) continue; for (final char ch : chs) chars[i++] = ch; break; } while (true); return new String(chars); }
To examine what you get back, consider Character.UnicodeBlock.of(int codePoint)
.
Monday, June 18, 2012
New in Guava 12: TypeToken, a better Class or TypeLiteral
I'm slow to read up on the Guava 12 release — it came out in April.
Among the changes is this gem: TypeToken
.
Guava provides TypeToken, which uses reflection-based tricks to allow you to manipulate and query generic types, even at runtime. Think of a TypeToken as a way of creating, manipulating, and querying Type (and, implicitly Class) objects in a way that respects generics.
Note to Guice users: TypeToken is similar to Guice's TypeLiteral class, but with one important difference: it supports non-reified types such as T, List<T> or even List<? extends Number>; while TypeLiteral does not. TypeToken is also serializable and offers numerous additional utility methods.
I look forward to giving this a spin next time the need arises.
Wednesday, June 13, 2012
Emacs, your makefile friend
After long hiatus I find myself against writing (editing) makefiles. Using Emacs compile
command makes all the difference. Emacs runs make in another process, sending output to an independent buffer (window, tab, panel) default named *compilation*.
In the *compilation* buffer, Emacs colorizes make's output. This greatly aids comprehension. As icing, Emacs recognizes output patterns from dozens of popular tools, and highlights them as information, warning or error as appropriate.
Emacs simplifies navigation with hyperlinking. The TAB key in *compilation* steps through warnings and errors. Magic.
As a bonus, I looked up common patterns stored in the Emacs variable, compilation-error-regexp-alist
. The info page for this variable includes a link to "compilation.txt", a complete list of sample output for each supported tool. It was trivial for me to find right at the top:
* GNU style symbol: gnu foo.c:8: message ../foo.c:8: W: message /tmp/foo.c:8: warning: message
That last line was my ticket. I arrange to echo similar text when something needs attention in my shell command, but without failing the entire build. I take care to break the output into separate shell echo commands so Emacs does not match prematurely when make displays executed commands. I could have prefixed my shell line with "@" (ampersand) to suppress make from printing it, but coding standards here discourage that.
a-file: if $(some_bad_condition_set_in_makefile) ; then \ echo -n $@ ; \ echo :1: warning: $@ is foobar. >&2 ; fi
I emulate the output of one of the tools Emacs groks, and I get the same magical colorizing and hyperlinking with my output:
making in /some/path
if true ; then \
echo -n a-file ; \
echo :1: Warning: a-file is foobar. >&2 ; fi
a-file:1: warning: a-file is foobar.
In *compilation* the TAB key jumps me to the start of a-file when it exists.
Tuesday, June 12, 2012
The generic getter idiom in Java
In our code base we represent Google protobuf messages as rooted trees, informally just trees. Nodes are necessarily heterogeneous: some are value nodes—strings, numbers, etc.—, some are roots of more trees, that is, node collections.
I recently replaced code like this:
StringNode childA = (StringNode) parent.get("child A's name"); Int32Node childB = (Int32Node) parent.get("child B's name");
With code like this:
StringNode childA = parent.get("child A's name"); Int32Node childB = parent.get("child B's name");
How did I do this? I changed the definition of "get" from:
public Node get(final String name) { // Clever implementation }
To:
public <N extends Node> N get(final String name) { // Same implementation with some "(N)" casts }
When the compiler can infer the return type of "get", it will, saving you writing now and reading later. In those cases where it cannot infer the type, you still help:
final String valueA = (String) ((StringNode) parent).get("child A's name").getValue(); final Integer valueB = (Integer) ((Int32Node) parent).get("child B's name").getValue();
Becomes:
final String valueA = parent.<StringNode>get("child A's name").getValue(); final Integer valueB = parent.<Int32Node>get("child B's name").getValue();
There is still a type cast, but you spell it differently using the generics system. Note "getValue" uses the same idiom as "get"; the return type can be inferred, no extra writing needed.
I'm going to call this pattern the Generic Getter Idiom until I find a good reference.
Thursday, June 07, 2012
The view of things to come Java
Lukas Eder notes the possibility of collection and structural literals in Java. A man can dream, can't he? Happily, this dream is built on more than wisps of fancy, just. I don't hold my breath.
Friday, June 01, 2012
Krumins does Bash one-liners
Beginning of a series on Bash one-liners from Peter Krumins. If it's from Peter, you know it'll be good.