πΎ Archived View for idiomdrottning.org βΊ dwim-sort captured on 2022-07-16 at 13:51:46. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
The dwimmiest sort of all time!
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
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")
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
dwim-sort is available as an egg for Chicken Scheme. For source code (BSD 1-clause licence):
git clone https://idiomdrottning.org/dwim-sort