Golf the Pigeonhole sort
Ruby, 43
def s a,*b,&f
a.map{|x|b[f[x]]=x}
b-[p]
end
Calling looks like
p s(["a","ccc","bb","eeeee"],&:length)
-> ["a", "bb", "ccc", "eeeee"]
Tricks: *b
in the arg list creates an empty output array (so long as you don't, y'know, pass any args). &f
creates a lambda reference to the passed-in block that can be called via f[]
(normal Ruby convention is to use yield
). a.map
iterates over the input array without modifying it and is one character shorter than a.each
. b-[p]
strips out the nil values, equivalent to b.compact
.