News for hackers. Stuff that matters.
Re:Schwartzian Transform by dang on Thursday December 17, 2015 @10:00PM (#10756335)
I like the Arc idiom for this. Here's how to sort a list (say of stories) by some computed value (say a function called score):

  (sort (compute > score) stories)
If score is expensive, you just wrap it in memo:

  (sort (compare > (memo score)) stories)
Now it will compute each score once, save them in a table, and throw the table away when done.
Re:Schwartzian Transform by pvg on Friday December 18, 2015 @01:33AM (#10757036)
This also pops up in SICP, first in an exercise (a re-tread of fibonacci, SICP's fizzbuzz) and in several other places later.

It seems like an example of the importance and influence of notation - many commonly used languages these days have first class functions but nobody using a lisp-2 variant would have thought to give this particular thing a special name.

Re:Schwartzian Transform by dang on Friday December 18, 2015 @03:19AM (#10757348)
I don't follow the connection to Lisp-2. Can you explain?
Re:Schwartzian Transform by pvg on Friday December 18, 2015 @03:49AM (#10757423)
There isn't one, I fatfingered my 1 into a 2 and it's too late to fix it, sorry. I just meant it ends up being the shortest, least weird notation for function composition. Clearly, the meta-notation has some problems...
Re:Schwartzian Transform by twotwotwo on Thursday December 17, 2015 @11:06PM (#10756561)
A possibly counterintuitive case where "the key...is expensive to compute" can be when accessing the key just requires following a pointer to something not in cache. It's not the same thing syntactically, but, for example, Postgres sped up collation by packing bytes into integers:

http://pgeoghegan.blogspot.com/2015/01/abbreviated-keys-expl...

I've also seen speedups sorting strings by first sorting prefixes of them packed into uint64s (rather than sorting pointer/length references to string data), and in an application where a friend needed to sort a lot of indices into an array, it turned out to greatly speed the sort (by avoiding indirections) to copy the whole array, sort the indices and the copy of the array together, then toss out the copy.

Re:Schwartzian Transform by paulsutter on Thursday December 17, 2015 @10:22PM (#10756404)
tl/dr, a sort optimization: avoid recomputing the sort keys by temporarily associating them with the input items.

Wouldn't it be embarrassing to have one's name in the title of a long winded Wikipedia article for a super simple and obvious optimization.