Programming

HyLang: Sorted

Sorted in Hylang reports as being a built-in but isn’t documented in the HyLang built-in documentation. After a bit of head-scratching I realised that this is just the Python built-in sorted.

Hy uses keyword parameters to bind keyword arguments in Python interop so to define a new sorting function you assign a lambda to the key keyword.

So to sort pairs by the second value…

(sorted :key (fn [[a b]] b) items)

This is pretty similar to Clojure’s sort-by except that you can’t define the comparator explicitly.

Standard