Sorting

These sorting functions are a port of SRFI-95 and, generally the functions take a sequence (a list or an array), a function that can determine a “less than” comparison between two values and an optional accessor function.

The accessor function allows you to indirect through another data structure and have the comparison function sort the value returned by the accessor rather than the original datum. Sorting by proxy, if you will.

For example, suppose you want to sort a list of file names by the size of each file:

  1. stat all the files and put the results in a hash table indexed by file name

    You could put the entire struct-stat struct in the hash table or just the st_size member depending on what you want your accessor function to do.

  2. create a named, or anonymous, accessor function that, given a file name, retrieves the corresponding st_size member from the hash table

  3. the st_size member is a C type so we can use C/< as the “less than” comparison function

  4. call sort with the list of file names, C/< and your accessor function

Obviously, you call reverse if you want the file names sorted in the opposite order.

Functions for sorting file names by st_atime, st_ctime, st_atime and st_size are below.

function sorted? seq less? [opt-key]

Is sequence seq sorted?

Param seq:

sequence to be verified as sorted

Type seq:

list or array

Param less?:

2-ary function that computes less than

Type less?:

function

Param opt-key:

accessor function, defaults to identity

Type opt-key:

function, optional

For a list (x0 x1 ... xm), sorted? returns true when for all 1 <= i <= m:

(not (less? (list-ref list i) (list-ref list (- i 1))))

function sort seq less? [opt-key]

sort the sequence seq using comparitor less?

Param seq:

sequence

Type seq:

list or array

Param less?:

2-ary function that computes less than

Type less?:

function

Param opt-key:

accessor function, defaults to identity

Type opt-key:

function, optional

function sort! seq less? [opt-key]

destructively sort the sequence seq using comparitor less?

Param seq:

sequence

Type seq:

list or array

Param less?:

2-ary function that computes less than

Type less?:

function

Param opt-key:

accessor function, defaults to identity

Type opt-key:

function, optional

function merge a b less? [opt-key]

merge pre-sorted lists a and b

Param a:

sorted list

Type a:

list

Param b:

sorted list

Type b:

list

Param less?:

2-ary function that computes less than

Type less?:

function

Param opt-key:

accessor function, defaults to identity

Type opt-key:

function, optional

returns a new list in which the elements of a and b have been stably interleaved so that (sorted? (merge a b less?) less?) is true.

function merge! a b less? [opt-key]

a destructive merge of pre-sorted lists a and b

Param a:

sorted list

Type a:

list

Param b:

sorted list

Type b:

list

Param less?:

2-ary function that computes less than

Type less?:

function

Param opt-key:

accessor function, defaults to identity

Type opt-key:

function, optional

function sort-atime l

sort the filenames in l by file access time

Param l:

filenames

Type l:

list

function sort-ctime l

sort sort filenames in l by file creation time

Param l:

filenames

Type l:

list

function sort-mtime l

sort the filenames in l by file modification time

Param l:

filenames

Type l:

list

function sort-size l

sort the filenames in l by file size

Param l:

filenames

Type l:

list

function sort-string seq

sort the strings seq using comparitor string<?

Param seq:

sequence of strings

Type seq:

list

function sort-symbol seq

sort the symbols seq

Param seq:

sequence of symbols

Type seq:

list

Example:

sort-symbol (module-exports libc)

Last built at 2024-12-21T07:10:39Z+0000 from 62cca4c (dev) for Idio 0.3.b.6