πŸ’Ύ Archived View for idiomdrottning.org β€Ί dwim-sort captured on 2023-01-29 at 03:58:39. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-07-16)

-=-=-=-=-=-=-

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