Sugar
1 Installation & updates
2 Cache
make-caching-proc
define/  caching
3 Coercion
3.1 Values
->int
->string
->symbol
->path
->complete-path
->list
->vector
->boolean
intish?
stringish?
symbolish?
pathish?
complete-pathish?
listish?
vectorish?
3.2 Coercion contracts
coerce/  int?
coerce/  string?
coerce/  symbol?
coerce/  path?
coerce/  boolean?
coerce/  list?
4 Container
get
in?
5 Debug
report
report/  line
report/  file
report*
report*/  line
report*/  file
repeat
time-repeat
time-repeat*
compare
6 File
get-ext
has-ext?
remove-ext
remove-ext*
add-ext
get-enclosing-dir
7 Include
include-without-lang-line
8 Len
len
9 List
trimf
filter-split
slice-at
slicef
slicef-at
slicef-after
frequency-hash
members-unique?
members-unique?/  error
values->list
sublist
break-at
shift
shifts
shift/  values
10 String
starts-with?
ends-with?
capitalized?
11 XML
xml-string->xexprs
xexprs->xml-string
12 License & source code
6.3.90.900

Sugar

Matthew Butterick <mb@mbtype.com>

 (require sugar) package: sugar
 (require (submod sugar safe))

A collection of small functions to help make Racket code simpler & more readable.

Sugar can be invoked two ways: as an ordinary library, or as a library with contracts (using the safe submodule).

1 Installation & updates

At the command line:

raco pkg install sugar

After that, you can update the package from the command line:

raco pkg update sugar

2 Cache

 (require sugar/cache) package: sugar
 (require (submod sugar/cache safe))

If, like Ricky Bobby and me, you want to go fast, then try using more caches. They’re wicked fast.

procedure

(make-caching-proc proc)  procedure?

  proc : procedure?
Make a caching version of proc. This means a hash table will be attached to proc, and result values will automatically be saved & retrieved. The arguments to the procedure are used as the hash key.

In the example below, notice that both invocations of slow-op take approximately the same time, whereas the second invocation of fast-op gets its value from the cache, and is thus nearly instantaneous.

Examples:
> (define (slow-op x) (for/sum ([i (in-range 100000000)]) i))
> (time (slow-op 42))

cpu time: 279 real time: 279 gc time: 0

4999999950000000

> (time (slow-op 42))

cpu time: 280 real time: 279 gc time: 0

4999999950000000

> (define fast-op (make-caching-proc slow-op))
> (time (fast-op 42))

cpu time: 279 real time: 280 gc time: 0

4999999950000000

> (time (fast-op 42))

cpu time: 0 real time: 0 gc time: 0

4999999950000000

Keep in mind that the cache is only available to external callers of the resulting function. So if proc calls itself recursively, these calls are not accelerated by the cache. If that’s the behavior you need, use define/caching to create a new recursive function.

syntax

(define/caching (name arg ... . rest-arg) body ...)

Like define, but automatically uses make-caching-proc to define a caching version of the function. If the function is recursive, the cache will be used for the recursive calls.

In the example below, fib is a recursive function. Notice that simply wrapping the function in make-caching-proc doesn’t work in this case, because fib’s recursive calls to itself bypass the cache. But fib-fast is rewritten to recur on the caching function, and the caching works as expected.

Examples:
> (define (fib x)
    (if (< x 2) 1 (+ (fib (- x 1)) (fib (- x 2)))))
> (define fibber (make-caching-proc fib))
> (define/caching (fib-fast x)
    (if (< x 2) 1 (+ (fib-fast (- x 1)) (fib-fast (- x 2)))))
> (time (fib 32))

cpu time: 79 real time: 80 gc time: 0

3524578

> (time (fibber 32))

cpu time: 81 real time: 80 gc time: 0

3524578

> (time (fib-fast 32))

cpu time: 0 real time: 0 gc time: 0

3524578

3 Coercion

 (require sugar/coerce) package: sugar
 (require (submod sugar/coerce safe))

Functions that coerce the datatype of a value to another type. Racket already has type-specific conversion functions. But if you’re handling values of indeterminate type — as sometimes happens in an untyped language — then handling the possible cases individually gets to be a drag.

3.1 Values

procedure

(->int v)  integer?

  v : any/c
Convert v to an integer in the least surprising way, or raise an error if no conversion is possible.

Numbers are rounded down to the nearest integer.

Examples:
> (->int 3)

3

> (->int 3.5)

3

> (->int -2.5)

-3

> (->int (+ 3 (/ 1 2)))

3

Stringlike values — paths, symbols, and strings — are converted to numbers and rounded down.

Examples:
> (->int "3.5")

3

> (->int '3.5)

3

> (->int (string->path "3.5"))

3

Characters are directly converted to integers.

Examples:
> (->int #\A)

65

> (->int #\◊)

9674

Lists, vectors, and other multi-value datatypes return their length (using len).

Examples:
> (->int (list 5 6 7))

3

> (->int (hash 'a 1 'b 2 'c 3))

3

The function will raise an error if no sensible conversion is possible.

Example:
> (->int #t)

->int: Can't convert #t to int

procedure

(->string v)  string?

  v : any/c
Return the most natural string representation of v, or raise an error if none exists.

Examples:
> (->string "string")

"string"

> (->string 'symbol)

"symbol"

> (->string 98.6)

"98.6"

> (->string (string->path "stdio.h"))

"stdio.h"

> (->string #\A)

"A"

> (->string #t)

->string: Can't convert #t to string

procedure

(->symbol v)  symbol?

  v : any/c
Same as ->string, but return a symbol rather than a string.

Examples:
> (->symbol "string")

'string

> (->symbol 'symbol)

'symbol

> (->symbol 98.6)

'|98.6|

> (->symbol (string->path "stdio.h"))

'stdio.h

> (->symbol #\A)

'A

> (->symbol #t)

->symbol: Can't convert #t to symbol

procedure

(->path v)  path?

  v : any/c

procedure

(->complete-path v)  complete-path?

  v : any/c
Same as ->string, but return a path (or complete path) rather than a string.

Examples:
> (->path "string")

#<path:string>

> (->path 'symbol)

#<path:symbol>

> (->complete-path 98.6)

#<path:/home/racket/build-pkgs/user/.racket/6.3.90.900/pkgs/sugar/98.6>

> (->complete-path (string->path "stdio.h"))

#<path:/home/racket/build-pkgs/user/.racket/6.3.90.900/pkgs/sugar/stdio.h>

> (->complete-path #\A)

#<path:/home/racket/build-pkgs/user/.racket/6.3.90.900/pkgs/sugar/A>

> (->complete-path #t)

->complete-path: Can't convert #t to complete-path

procedure

(->list v)  list?

  v : any/c
If v is a listlike data type — a vector, set, stream, sequence, or list — convert it to a list. A hash or dictionary becomes a list using dict->list. If v is an atomic value, turn it into a single-member list.

Note that a string is treated as an atomic value rather than decomposed with string->list. This is done so the function handles strings the same way as symbols and paths.

Examples:
> (->list '(a b c))

'(a b c)

> (->list (list->vector '(a b c)))

'(a b c)

> (->list (make-hash '((k . v) (k2 . v2))))

'((k . v) (k2 . v2))

> (->list "string")

'("string")

> (->list 'symbol)

'(symbol)

> (->list (string->path "path"))

'(#<path:path>)

> (->list +)

'(#<procedure:+>)

procedure

(->vector v)  vector?

  v : any/c
Same as ->list, but returns a vector rather than a list.

Examples:
> (->vector '(a b c))

'#(a b c)

> (->vector (list->vector '(a b c)))

'#(a b c)

> (->vector (make-hash '((k . v) (k2 . v2))))

'#((k . v) (k2 . v2))

> (->vector "string")

'#("string")

> (->vector 'symbol)

'#(symbol)

> (->vector (string->path "path"))

'#(#<path:path>)

> (->vector +)

'#(#<procedure:+>)

procedure

(->boolean v)  boolean?

  v : any/c
Return #t for all v except #f, which remains #f.

Examples:
> (->boolean "string")

#t

> (->boolean 'symbol)

#t

> (->boolean +)

#t

> (->boolean '(l i s t))

#t

> (->boolean #f)

#f

procedure

(intish? v)  boolean?

  v : any/c

procedure

(stringish? v)  boolean?

  v : any/c

procedure

(symbolish? v)  boolean?

  v : any/c

procedure

(pathish? v)  boolean?

  v : any/c

procedure

(complete-pathish? v)  boolean?

  v : any/c

procedure

(listish? v)  boolean?

  v : any/c

procedure

(vectorish? v)  boolean?

  v : any/c
Predicates that report whether v can be coerced to the specified type.

Examples:
> (map intish? (list 3 3.5 #\A "A" + #t))

'(#t #t #t #f #f #f)

> (map stringish? (list 3 3.5 #\A "A" + #t))

'(#t #t #t #t #f #f)

> (map symbolish? (list 3 3.5 #\A "A" + #t))

'(#t #t #t #t #f #f)

> (map pathish? (list 3 3.5 #\A "A" + #t))

'(#t #t #t #t #f #f)

> (map complete-pathish? (list 3 3.5 #\A "A" + #t))

'(#t #t #t #t #f #f)

> (map listish? (list 3 3.5 #\A "A" + #t))

'(#t #t #t #t #t #t)

> (map vectorish? (list 3 3.5 #\A "A" + #t))

'(#t #t #t #t #t #t)

3.2 Coercion contracts

procedure

(coerce/int? v)  integer?

  v : any/c

procedure

(coerce/string? v)  string?

  v : any/c

procedure

(coerce/symbol? v)  symbol?

  v : any/c

procedure

(coerce/path? v)  path?

  v : any/c

procedure

(coerce/boolean? v)  boolean?

  v : any/c

procedure

(coerce/list? v)  list?

  v : any/c
If v can be coerced to the specified type, change it to that type, then return it. If not, raise the usual contract error. These contracts can be used with input or output values.

Examples:
> (define/contract (add-ints x y)
      (coerce/int? coerce/int? . -> . any/c)
      (+ x y))
; Input arguments will be coerced to integers, then added
> (add-ints 1.6 3.8)

4

> (define/contract (int-sum x y)
      (any/c any/c . -> . coerce/int?)
      (+ x y))
; Input arguments will be added, and the result coerced to an integer
> (int-sum 1.6 3.8)

5

Please note: this is not an officially sanctioned way to use Racket’s contract system, because contracts aren’t supposed to mutate their values (see make-contract).

But coercion contracts can be useful in two situations:

4 Container

 (require sugar/container) package: sugar
 (require (submod sugar/container safe))

Type-neutral functions for getting elements out of a container, or testing membership. This submodule is untyped only.

procedure

(get container which [end_which])  any/c

  container : (or/c list? vector? sequence? dict? string? symbol? path?)
  which : any/c
  end_which : (or/c (and/c integer? positive?) #f) = #f
For a container that’s a dict?, retrieve the element associated with the key which. Raise an error if the key doesn’t exist.

Examples:
> (get (make-hash '((a . 1) (b . 2) (c  . 3))) 'b)

2

> (get (make-hash '((a . 1) (b . 2) (c  . 3))) 'z)

get: couldn't retrieve item z from #hash((a . 1) (b . 2) (c

. 3))

For other container types — which are all sequence-like — retrieve the element located at which. Or if the optional end_which argument is provided, retrieve the elements from which to (sub1 end_which), inclusive (i.e., make a slice). Raise an error if which or end_which is out of bounds.

Examples:
> (get '(0 1 2 3 4 5) 2)

2

> (get '(0 1 2 3 4 5) 2 4)

'(2 3)

> (get '(0 1 2 3 4 5) 100)

get: couldn't retrieve item 100 from (0 1 2 3 4 5)

> (get '(0 1 2 3 4 5) 2 100)

get: couldn't retrieve items 2 through 100 from (0 1 2 3 4

5)

> (get (list->vector '(0 1 2 3 4 5)) 2)

2

> (get (list->vector '(0 1 2 3 4 5)) 2 4)

'#(2 3)

> (get "purple" 2)

"r"

> (get "purple" 2 4)

"rp"

> (get 'purple 2)

'r

> (get 'purple 2 4)

'rp

When container is a path, it’s treated as a list of path elements (created by explode-path), not as a stringlike value.

Examples:
> (get (string->path "/root/foo/bar/file.txt") 1)

#<path:root>

> (get (string->path "/root/foo/bar/file.txt") 0 3)

'(#<path:/> #<path:root> #<path:foo>)

To slice to the end of container, use (len container) as the value of end_which.

Examples:
> (define xs '(0 1 2 3 4 5))
> (get xs 2 (len xs))

'(2 3 4 5)

> (get (list->vector xs) 2 (len (list->vector xs)))

'#(2 3 4 5)

> (define color "purple")
> (get color 2 (len color))

"rple"

procedure

(in? item container)  boolean?

  item : any/c
  container : (or/c list? vector? sequence? set? dict? string? symbol? path?)
Return #t if item is in container, or #f otherwise.

Examples:
> (in? 2 '(0 1 2 3 4 5))

#t

> (in? 'a '(0 1 2 3 4 5))

#f

> (in? 2 (list->vector '(0 1 2 3 4 5)))

#t

> (in? "pu" "purple")

#t

> (in? "zig" "purple")

#f

> (in? 'b (make-hash '((a . 1) (b . 2) (c  . 3))))

#t

> (in? 'z (make-hash '((a . 1) (b . 2) (c  . 3))))

#f

As with get, when container is a path, it’s treated as a list of exploded path elements, not as a stringlike value.

Examples:
> (in? "foo" (string->path "/root/foo/bar/file.txt"))

#t

> (in? "zam" (string->path "/root/foo/bar/file.txt"))

#f

5 Debug

 (require sugar/debug) package: sugar
 (require (submod sugar/debug safe))

Debugging utilities.

syntax

(report expr)

(report expr maybe-name)
Print the name and value of expr to current-error-port, but also return the evaluated result of expr as usual. This lets you see the value of an expression or variable at runtime without disrupting any of the surrounding code. Optionally, you can use maybe-name to change the name shown in current-error-port.

For instance, suppose you wanted to see how first-condition? was being evaluted in this expression:

(if (and (first-condition? x) (second-condition? x))
  (one-thing)
  (other-thing))

You can wrap it in report and find out:

(if (and (report (first-condition? x)) (second-condition? x))
  (one-thing)
  (other-thing))

This code will run the same way as before. But when it reaches first-condition?, you willl see in current-error-port:

(first-condition? x) = #t

You can also add standalone calls to report as a debugging aid at points where the return value will be irrelevant, for instance:

(report x x-before-function)
(if (and (report (first-condition? x)) (second-condition? x))
  (one-thing)
  (other-thing))

x-before-function = 42
(first-condition? x) = #t

But be careful — in the example below, the result of the if expression will be skipped in favor of the last expression, which will be the value of x:

(if (and (report (first-condition? x)) (second-condition? x))
  (one-thing)
  (other-thing))
  (report x)

syntax

(report/line expr)

(report/line expr maybe-name)
Same as report, but also shows the line number of expr.

syntax

(report/file expr)

(report/file expr maybe-name)
Same as report, but also shows the line number and source-file name of expr.

syntax

(report* expr ...)

syntax

(report*/line expr ...)

syntax

(report*/file expr ...)

Apply the relevant report macro separately to each expr in the list.

syntax

(repeat num expr ...)

Evaluate expr repeatedly — num times, in fact — and return the last value.

Example:
> (repeat 1000
  (for/sum ([i (in-range 1000)]) i))

499500

syntax

(time-repeat num expr ...)

Shorthand for using time with repeat. Repeat the whole list of expressions, print the total time, and return the last value.

Example:
> (time-repeat 1000
  (for/product ([i (in-range 1000)]) i)
  (for/sum ([i (in-range 1000)]) i))

cpu time: 8 real time: 7 gc time: 0

499500

syntax

(time-repeat* num expr ...)

Apply time-repeat to each expr individually.

Example:
> (time-repeat* 1000
  (for/product ([i (in-range 1000)]) i)
  (for/sum ([i (in-range 1000)]) i))

cpu time: 4 real time: 4 gc time: 0

cpu time: 3 real time: 3 gc time: 0

0

499500

syntax

(compare expr id id-alt ...)

Evaluate expr first using id, and then again substituting id-alt in place of id, and then again for every other id-alt in the list. This is useful for comparing the performance of multiple versions of a function.

Examples:
> (define (fib x)
    (if (< x 2) 1 (+ (fib (- x 1)) (fib (- x 2)))))
> (define/caching (fib-fast x)
    (if (< x 2) 1 (+ (fib-fast (- x 1)) (fib-fast (- x 2)))))
> (compare (time (fib 34)) fib fib-fast)

cpu time: 207 real time: 207 gc time: 0

cpu time: 0 real time: 0 gc time: 0

9227465

9227465

6 File

 (require sugar/file) package: sugar
 (require (submod sugar/file safe))

File utilities, mostly in the realm of file extensions. These functions don’t access the filesystem.

Arguments that are pathish? can take either a string or a path. For clarity below, I’ve used strings.

procedure

(get-ext file-path)  (or/c #f string?)

  file-path : pathish?
Return the last file extension of file-path as a string, or #f if it has no extension. Omit the intervening . separator.

Examples:
> (get-ext "foo.txt")

"txt"

> (get-ext "/path/to/foo.txt")

"txt"

> (get-ext "/path/to/foo.txt.bar")

"bar"

> (get-ext "/path/to/file-without-extension")

#f

> (get-ext "/path/to/directory/")

#f

procedure

(has-ext? file-path ext)  boolean?

  file-path : pathish?
  ext : stringish?
Return #t if the last file extension of file-path is ext, otherwise #f.

Examples:
> (has-ext? "foo.txt" "txt")

#t

> (has-ext? "foo.txt" "jpg")

#f

> (has-ext? "foo.jpg.txt" "jpg")

#f

procedure

(remove-ext file-path)  path?

  file-path : pathish?
Remove the last file extension of file-path, and return the path that remains. If file-path has no extension, you just get the same file-path. Does not use the filesystem.

Examples:
> (remove-ext "foo.txt")

#<path:foo>

> (remove-ext "/path/to/foo.txt")

#<path:/path/to/foo>

> (remove-ext "/path/to/foo.txt.bar")

#<path:/path/to/foo.txt>

> (remove-ext (remove-ext "/path/to/foo.txt.bar"))

#<path:/path/to/foo>

procedure

(remove-ext* file-path)  path?

  file-path : pathish?
Like remove-ext, just more. Remove all file extensions from file-path, and return the path that remains. If file-path has no extensions, you just get the same file-path. Does not use the filesystem.

Examples:
> (remove-ext* "foo.txt")

#<path:foo>

> (remove-ext* "/path/to/foo.txt")

#<path:/path/to/foo>

> (remove-ext* "/path/to/foo.txt.bar")

#<path:/path/to/foo>

> (remove-ext* (remove-ext* "/path/to/foo.txt.bar"))

#<path:/path/to/foo>

procedure

(add-ext file-path ext)  path?

  file-path : pathish?
  ext : stringish?
Return a new file-path with ext appended. Note that this does not replace an existing file extension. If that’s what you want, then do (add-ext (remove-ext file-path) ext).

Examples:
> (add-ext "foo" "txt")

#<path:foo.txt>

> (add-ext "foo.txt" "jpg")

#<path:foo.txt.jpg>

> (add-ext (remove-ext "foo.txt") "jpg")

#<path:foo.jpg>

procedure

(get-enclosing-dir path)  path?

  path : pathish?
Return the enclosing directory of path. Does not consult the filesystem about whether path is valid. If you reach the root directory, then (get-enclosing-dir root) will just return root again.

Examples:
> (define bin (string->path "/usr/bin"))
> bin

#<path:/usr/bin>

> (get-enclosing-dir bin)

#<path:/usr/>

> (get-enclosing-dir (get-enclosing-dir bin))

#<path:/>

> (get-enclosing-dir (get-enclosing-dir (get-enclosing-dir bin)))

#<path:/>

7 Include

 (require sugar/include) package: sugar

syntax

(include-without-lang-line path-spec)

Inline the syntax in the file designated by path-spec, after stripping off the #lang line of the file (if it exists, otherwise just include the file as usual).

8 Len

 (require sugar/len) package: sugar
 (require (submod sugar/len safe))

procedure

(len x)  integer?

  x : (or/c list? vector? set? string? symbol? path? hash?)
Calculate the length of x in the least surprising way possible, or if it can’t be done, raise an error. Named in honor of the original discoverer of the length-reticulation algorithm, Prof. Leonard Spottiswoode.

Examples:
> (len '(a b c))

3

> (len (list->vector '(a b c)))

3

> (len 'abc)

3

> (len "abc")

3

> (len (string->path "abc"))

3

> (len (make-hash `((a . 1)(b . 2)(c . 3))))

3

Perhaps ironically, positive integers do not have a length.

Example:
> (len 3)

len: can't calculate length of 3

9 List

 (require sugar/list) package: sugar
 (require (submod sugar/list safe))

procedure

(trimf lst pred)  list?

  lst : list?
  pred : procedure?
Drop elements from each end of lst that satisfy pred. Exactly equivalent to (dropf-right (dropf lst pred) pred).

Examples:
> (trimf '(1 2 3 a b c 4 5 6) integer?)

'(a b c)

> (trimf '(1 2 3 a b c) integer?)

'(a b c)

> (trimf '(a b c) integer?)

'(a b c)

> (trimf '(a b c 1 2 3 d e f) integer?)

'(a b c 1 2 3 d e f)

procedure

(filter-split lst pred)  (listof list?)

  lst : list?
  pred : procedure?
Like string-split, but for lists. Drop elements from anywhere in lst that satisfy pred — ends, middle, you name it — and return a list of the sublists that remain.

Examples:
> (filter-split '(1 a b c 2 d e f 3) integer?)

'((a b c) (d e f))

> (filter-split '(1 a b c 2 d e f 3) (compose not integer?))

'((1) (2) (3))

> (filter-split '(a b c 1 2 3 d e f) integer?)

'((a b c) (d e f))

> (filter-split '(a b c 1 2 3 d e f) (compose not integer?))

'((1 2 3))

procedure

(slice-at lst len [force?])  (listof list?)

  lst : list?
  len : (and/c integer? positive?)
  force? : boolean? = #f
Divide lst into sublists of length len. If lst cannot be divided evenly by len, the last sublist will be shorter. If this displeases you, set force? to #t and a stumpy final sublist will be ignored.

Examples:
> (slice-at (range 5) 1)

'((0) (1) (2) (3) (4))

> (slice-at (range 5) 2)

'((0 1) (2 3) (4))

> (slice-at (range 5) 2 #t)

'((0 1) (2 3))

> (slice-at (range 5) 3)

'((0 1 2) (3 4))

> (slice-at (range 5) 5)

'((0 1 2 3 4))

> (slice-at (range 5) 5 #t)

'((0 1 2 3 4))

> (slice-at (range 5) 100000)

'((0 1 2 3 4))

> (slice-at (range 5) 100000 #t)

'()

procedure

(slicef lst pred)  (listof list?)

  lst : list?
  pred : procedure?
Divide lst into sublists that are homogeneously pred or not pred. If none of the elements match pred, there is no slice to be made, and the result is the whole input list.

Examples:
> (slicef '(1 2 2 1 2) even?)

'((1) (2 2) (1) (2))

> (slicef (range 5) odd?)

'((0) (1) (2) (3) (4))

> (slicef (range 5) string?)

'((0 1 2 3 4))

procedure

(slicef-at lst pred [force?])  (listof list?)

  lst : list?
  pred : procedure?
  force? : boolean? = #f
Divide lst into sublists starting with elements matching pred. The first element of the first sublist may not match pred. Or, if you really & truly want only the sublists starting with an element matching pred, set force? to #t.

Examples:
> (slicef-at (range 5) even?)

'((0 1) (2 3) (4))

> (slicef-at '(1 2 2 1 2) even?)

'((1) (2) (2 1) (2))

> (slicef-at '(1 2 2 1 2) even? #t)

'((2) (2 1) (2))

> (slicef-at (range 5) odd?)

'((0) (1 2) (3 4))

> (slicef-at (range 5) odd? #t)

'((1 2) (3 4))

procedure

(slicef-after lst pred)  (listof list?)

  lst : list?
  pred : procedure?
Divide lst into sublists ending with elements matching pred. (Distinct from slicef-at, which gives you sublists that start with elements matching pred.) If none of the elements match pred, there is no slice to be made, and the result is the whole input list.

Examples:
> (slicef-after '(1 2 2 1 2) even?)

'((1 2) (2) (1 2))

> (slicef-after (range 5) odd?)

'((0 1) (2 3) (4))

> (slicef-after (range 5) string?)

'((0 1 2 3 4))

procedure

(frequency-hash lst)  hash?

  lst : list?
Count the frequency of each element in lst, and return a hash whose keys are the unique elements of lst, and each value is the frequency of that element within lst.

Examples:
> (frequency-hash '(a b b c c c))

'#hash((b . 2) (c . 3) (a . 1))

> (frequency-hash '(c b c a b c))

'#hash((c . 3) (b . 2) (a . 1))

procedure

(members-unique? container)  boolean?

  container : (or/c list? vector? string?)
Return #t if every element in container is unique, otherwise #f.

Examples:
> (members-unique? '(a b c d e f))

#t

> (members-unique? '(a b c d e f a))

#f

procedure

(members-unique?/error container)  boolean?

  container : (or/c list? vector? string?)
Same as members-unique?, but if the members are not unique, raises a descriptive error rather than returning #f.

Examples:
> (members-unique?/error '(a b c d e f))

#t

> (members-unique?/error '(a b c d e f a))

members-unique? failed because item isn't unique: (a)

> (members-unique?/error '(a b c d e f a b))

members-unique? failed because items aren't unique: (b a)

syntax

(values->list values)

Convert values to a simple list.

Examples:
> (split-at '(a b c d e f) 3)

'(a b c)

'(d e f)

> (values->list (split-at '(a b c d e f) 3))

'((a b c) (d e f))

procedure

(sublist lst start-idx end-idx)  list?

  lst : list?
  start-idx : (and/c integer? (not/c negative?))
  end-idx : (and/c integer? (not/c negative?))
Return a sublist of the lst starting with item start-idx and ending one item before item end-idx. (Similar to how list slices are denominated in Python.) Thus the maximum value for end-idx is (length lst). Errors will be triggered by nonsensical values for end-idx.

Bear in mind that sublist is built for convenience, not performance. If you need to do a lot of random access into the middle of an ordered sequence of items, you’d be better off putting them into a vector and using vector-copy.

Examples:
> (sublist '(0 1 2 3 4 5 6 7 8) 0 8)

'(0 1 2 3 4 5 6 7)

> (sublist '(0 1 2 3 4 5 6 7 8) 8 9)

'(8)

> (sublist '(0 1 2 3 4 5 6 7 8) 2 5)

'(2 3 4)

> (sublist '(0 1 2 3 4 5 6 7 8) 5 2)

sublist: starting index 5 is larger than ending index 2

> (sublist '(0 1 2 3 4 5 6 7 8) 2 10)

sublist: ending index 10 exceeds length of list

procedure

(break-at lst indexes)  (listof list?)

  lst : list?
  indexes : (or/c integer? (listof integer?))
Break lst into smaller lists at the index positions in indexes. If a single integer value is given for indexes, it’s treated as a one-element list. Errors will arise if a breakpoint index exceeds the length of the list, or if the breakpoints are not increasing.

Examples:
> (break-at '(0 1 2 3 4 5 6 7 8) 3)

'((0 1 2) (3 4 5 6 7 8))

> (break-at '(0 1 2 3 4 5 6 7 8) '(3))

'((0 1 2) (3 4 5 6 7 8))

> (break-at '(0 1 2 3 4 5 6 7 8) '(3 6))

'((0 1 2) (3 4 5) (6 7 8))

> (break-at '(0 1 2 3 4 5 6 7 8) '(3 6 8))

'((0 1 2) (3 4 5) (6 7) (8))

> (break-at '(0 1 2 3 4 5 6 7 8) '(3 6 8 10))

break-at: breakpoint in '(3 6 8 10) is greater than or equal

to input list length = 9

procedure

(shift lst how-far [fill-item cycle?])  list?

  lst : list?
  how-far : integer?
  fill-item : any/c = #f
  cycle? : boolean? = #f
Move the items in lst to the right (if how-far is positive) or left (if how-far is negative). By default, vacated spaces in the list are filled with fill-item. But if cycle? is true, elements of the list wrap around (and fill-item is ignored). Either way, the result list is always the same length as the input list. (If you don’t care about the lengths being the same, you probably want take or drop instead.) If how-far is 0, return the original list. If how-far is bigger than the length of lst, raise an error.

Examples:
> (define xs (range 5))
> (shift xs 2)

'(#f #f 0 1 2)

> (shift xs -2 0)

'(2 3 4 0 0)

> (shift xs 2 'boing)

'(boing boing 0 1 2)

> (shift xs 2 'boing #t)

'(3 4 0 1 2)

> (shift xs 0)

'(0 1 2 3 4)

> (shift xs 42)

shift: index is too large for list

index: 42

list: '(0 1 2 3 4)

procedure

(shifts lst how-far [fill-item cycle?])  (listof list?)

  lst : list?
  how-far : (listof integer?)
  fill-item : any/c = #f
  cycle? : boolean? = #f
Same as shift, but how-far is a list of integers rather than a single integer, and the result is a list of lists rather than a single list.

Examples:
> (define xs (range 5))
> (shifts xs '(-2 2))

'((2 3 4 #f #f) (#f #f 0 1 2))

> (shifts xs '(-2 2) 0)

'((2 3 4 0 0) (0 0 0 1 2))

> (shifts xs '(-2 2) 'boing)

'((2 3 4 boing boing) (boing boing 0 1 2))

> (shifts xs '(-2 2) 'boing #t)

'((2 3 4 0 1) (3 4 0 1 2))

procedure

(shift/values lst how-far [fill-item])  any

  lst : list?
  how-far : (or/c integer? (listof integer?))
  fill-item : any/c = #f
When how-far is a single integer, same as shift, but the resulting list is returned as values. When how-far is a list of integers, same as shifts, but the resulting lists are returned as multiple values rather than as a list of lists.

Examples:
> (define xs (range 5))
> (shift xs 1)

'(#f 0 1 2 3)

> (shift/values xs 1)

#f

0

1

2

3

> (shifts xs '(-1 0 1))

'((1 2 3 4 #f) (0 1 2 3 4) (#f 0 1 2 3))

> (shift/values xs '(-1 0 1))

'(1 2 3 4 #f)

'(0 1 2 3 4)

'(#f 0 1 2 3)

10 String

 (require sugar/string) package: sugar
 (require (submod sugar/string safe))

procedure

(starts-with? str starter)  boolean?

  str : stringish?
  starter : stringish?
Return #t if str starts with starter, otherwise #f.

Examples:
> (starts-with? "foobar" "foo")

#t

> (starts-with? "foobar" "foobar")

#t

> (starts-with? "foobar" "zam")

#f

> (starts-with? "foobar" "foobars")

#f

procedure

(ends-with? str ender)  boolean?

  str : stringish?
  ender : stringish?
Return #t if str ends with ender, otherwise #f.

Examples:
> (ends-with? "foobar" "foo")

#f

> (ends-with? "foobar" "foobar")

#t

> (ends-with? "foobar" "zam")

#f

> (ends-with? "foobar" "foobars")

#f

procedure

(capitalized? str)  boolean?

  str : stringish?
Return #t if str starts with a capital letter, otherwise #f.

Examples:
> (capitalized? "Brennan")

#t

> (capitalized? "Brennan stinks")

#t

> (capitalized? "stinks")

#f

11 XML

 (require sugar/xml) package: sugar
 (require (submod sugar/xml safe))

Making it easier to do the simplest kind of round-trip with XML: convert an XML string to X-expressions, manipulate, and then convert these X-expressions back to an XML string. This submodule is untyped only.

procedure

(xml-string->xexprs xml-string)  
xexpr? xexpr?
  xml-string : string?
Take a string containg XML and break it into two X-expressions: one representing the prolog of the document, and the other representing everything under the root node. Your xml-string must have a root node, but it doesn’t need a prolog.

Examples:
> (define str "<?xml encoding=\"utf-8\"?>\n<root>hello</root>")
> (xml-string->xexprs str)

(prolog

 (list (p-i (location 1 0 1) (location 1 24 25) 'xml "encoding=\"utf-8\""))

 #f

 '())

'(root () "hello")

> (define root-only "<root>hello</root>")
> (xml-string->xexprs root-only)

(prolog '() #f '())

'(root () "hello")

> (define prolog-only "<?xml encoding=\"utf-8\"?>")
> (xml-string->xexprs prolog-only)

read-xml: parse-error: expected root element - received

#<eof>

procedure

(xexprs->xml-string prolog-xexpr    
  root-xexpr)  string?
  prolog-xexpr : xexpr?
  root-xexpr : xexpr?
Take two X-expressions representing the prolog and root of an XML document and join them back into an XML string. In other words, the inverse of the function above.

Examples:
> (define str "<?xml encoding=\"utf-8\"?>\n<root>hello</root>")
> (define-values (prolog doc) (xml-string->xexprs str))
> prolog

(prolog

 (list (p-i (location 1 0 1) (location 1 24 25) 'xml "encoding=\"utf-8\""))

 #f

 '())

> doc

'(root () "hello")

> (xexprs->xml-string prolog doc)

"<?xml encoding=\"utf-8\"?>\n<root>hello</root>"

12 License & source code

This module is licensed under the LGPL.

Source repository at http://github.com/mbutterick/sugar. Suggestions & corrections welcome.