Sugar: readability & convenience library
(require sugar) | package: sugar |
A collection of small functions to help make Racket code simpler & more readable.
1 Installation & updates
raco pkg install sugar |
raco pkg update sugar |
2 Coercion
(require sugar/coerce) | package: sugar |
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.
2.1 Values
Numbers are rounded down to the nearest integer.
Examples: | ||||||||
|
Stringlike values — paths, symbols, and strings — are converted to numbers and rounded down.
Examples: | ||||||
|
Characters are directly converted to integers.
Examples: | ||||
|
Lists, vectors, and other multi-value datatypes return their length (using len).
Examples: | ||||
|
Example: | ||
|
Examples: | ||||||||||||
|
Examples: | ||||||||||||
|
Examples: | ||||||||||||
|
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: | ||||||||||||||
|
Examples: | ||||||||||||||
|
Examples: | ||||||||||
|
procedure
v : any/c
procedure
(stringish? v) → boolean?
v : any/c
procedure
(symbolish? v) → boolean?
v : any/c
procedure
v : any/c
procedure
(complete-pathish? v) → boolean?
v : any/c
procedure
v : any/c
procedure
(vectorish? v) → boolean?
v : any/c
Examples: | ||||||||||||||
|
2.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
Examples: | ||||||||||||||||||
|
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:
You want to be liberal about input types, but don’t want to deal with the housekeeping and manual conversions between types.
Your contract involves an expensive operation that you’d rather avoid performing twice.
3 Container
(require sugar/container) | package: sugar |
Type-neutral functions for getting elements out of a container, or testing membership.
procedure
container : (or/c list? vector? sequence? dict? string? symbol? path?) which : any/c end_which : (or/c (and/c integer? positive?) #f) = #f
Examples: | |||||
|
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: | |||||||||||||||||||||
|
When container is a path, it’s treated as a list of path elements (created by explode-path), not as a stringlike value.
Examples: | ||||
|
To slice to the end of container, use (len container) as the value of end_which.
Examples: | ||||||||||||
|
Examples: | ||||||||||||||
|
As with get, when container is a path, it’s treated as a list of exploded path elements, not as a stringlike value.
Examples: | ||||
|
4 Debug
(require sugar/debug) | package: sugar |
Debugging utilities.
syntax
(report expr)
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) (if (and (report (first-condition? x)) (second-condition? x)) (one-thing) (other-thing))
x = 42
(first-condition? x) = #t
But don’t do this, because 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)
5 File
(require sugar/file) | package: sugar |
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.
Examples: | ||||||||||
|
procedure
file-path : pathish? ext : stringish?
Examples: | ||||||
|
procedure
(remove-ext file-path) → path?
file-path : pathish?
Examples: | ||||||||
|
procedure
(remove-ext* file-path) → path?
file-path : pathish?
Examples: | ||||||||
|
procedure
file-path : pathish? ext : stringish?
Examples: | ||||||
|
procedure
(get-enclosing-dir path) → path?
path : pathish?
Examples: | |||||||||||
|
6 Len
(require sugar/len) | package: sugar |
Examples: | ||||||||||||
|
Perhaps ironically, positive integers do not have a length.
Example: | ||
|
7 List
(require sugar/list) | package: sugar |
procedure
lst : list? pred : procedure?
Examples: | ||||||||
|
procedure
(filter-split lst pred) → (listof list?)
lst : list? pred : procedure?
Examples: | ||||||||
|
procedure
(frequency-hash lst) → hash?
lst : list?
Examples: | ||||
|
Examples: | ||||
|
Examples: | ||||||
|
syntax
(when/splice test expr)
Examples: | ||||||||
|
syntax
(values->list values)
Examples: | ||||||
|
8 String
(require sugar/string) | package: sugar |
procedure
(starts-with? str starter) → boolean?
str : stringish? starter : stringish?
Examples: | ||||||||
|
procedure
(ends-with? str ender) → boolean?
str : stringish? ender : stringish?
Examples: | ||||||||
|
procedure
(capitalized? str) → boolean?
str : stringish?
Examples: | ||||||
|
9 License & source code
This module is licensed under the LGPL.
Source repository at http://github.com/mbutterick/sugar. Suggestions & corrections welcome.