dwim-sort
The dwimmiest sort of all time!
sort
With one argument, takes whatever and sorts it ascending:
(sort '(colorless green ideas sleep furiously)) ⇒ (colorless furiously green ideas sleep) (sort '(((4 2)) "all kinds" (16 18 19))) ⇒ (((4 2)) (16 18 19) "all kinds") (sort "oh, This is wonderful") ⇒ " ,defhhiilnoorssTuw" (sort '((16 18 19) ((4 2)))) ⇒ (((4 2)) (16 18 19)) (sort '(((4 2)) "all kinds" (16 18 19))) ⇒ (((4 2)) (16 18 19) "all kinds") (sort 14351) ⇒ 11345
With two arguments, it expects a sequence and a comparator, and it calls the original sort (from (chicken sort)).
(sort 14351 >) ⇒ 54311
If your sequence is a list, this is pretty cheap compared to just calling the original sort directly. So use the dwimmy sort while prototyping and then as an intermediate step while profiling you can manually ensure you've got the right sequence type (list) and comparator (type-specific) in there.
(time (sort "fantastic")) 0.001s CPU time, 747/0 mutations (total/tracked), 0/2 GCs (major/minor), maximum live heap: 1.81 MiB (time (sort (string->list "fantastic") char-ci>?)) 0s CPU time, 44/0 mutations (total/tracked), maximum live heap: 1.81 MiB (time (og-sort (string->list "fantastic") char-ci>?)) 0s CPU time, 29/0 mutations (total/tracked), maximum live heap: 1.81 MiB
sorta
Like sort but lets you sort your arguments directly:
(sorta 23 ((ctq a 1 b 2))) ⇒ (23 #<hash-table (2)>) (sorta 23 ((ctq a 1 b 2))) ⇒ (23 #<hash-table (2)>) (sorta "abd" "abc" "abe") ⇒ ("abc" "abd" "abe") (sorta "abd" "abc" +) ⇒ ("abc" "abd" #<procedure C_plus>) (sorta "abc" "abd" #t) ⇒ (#t "abc" "abd") (sorta "potato" "Pizza") ⇒ ("Pizza" "potato")
smallest and biggest
Like min and max, but work on anything. If given a single argument, assumes it's a sequence and gets the smallest of it.
(smallest (list 1087 3 '() "horse")) ⇒ () (smallest list 1087 3 '() "horse") ⇒ () (biggest (list 1087 3 '() "horse")) ⇒ "horse" (biggest list 1087 3 '() "horse") ⇒ #<procedure (scheme#list . lst)> (smallest "magnetic") ⇒ #\a
source code
dwim-sort is available as an egg for Chicken Scheme. For source code (BSD 1-clause licence):
git clone https://idiomdrottning.org/dwim-sort