Lathe Comforts
1 Evergreen utilities for binding syntax and pure FP
1.1 Binding syntax utilities
binds
define-simple-normalizing-binder
1.2 Functional programming utilities
1.2.1 Bindings and recursion
pass
w-
fn
w-loop
loopfn
1.2.2 Conditionals
mat
expect
matfns
expectfn
dissect
dissectfn
6.12

Lathe Comforts

(require #/for-label #/only-in parendown pd)

 (require lathe-comforts) package: lathe-comforts-lib

Lathe Comforts for Racket is a collection of utilities that are handy for writing Racket code. This is a non-intrusive toolkit; in most cases it should only make certain Racket code easier to write, without substantially changing the architecture of the project it’s used in.

Some of these utilities are designed with Parendown in mind. In some cases, Parendown’s weak opening brackets make it easier to get by with higher-order functions instead of custom syntax. (Note that due to limitations of Scribble’s Racket code formatter, we use Parendown’s ‘pd‘ macro to achieve these weak parens, rather than using its custom reader syntax.)

    1 Evergreen utilities for binding syntax and pure FP

      1.1 Binding syntax utilities

      1.2 Functional programming utilities

        1.2.1 Bindings and recursion

        1.2.2 Conditionals

1 Evergreen utilities for binding syntax and pure FP

1.1 Binding syntax utilities

splicing syntax class

binds

Matches syntax in any of three formats:

In all cases, this binds two attributes of ellipsis depth 1, namely var and val, and they carry the same number of matches.

(See _, expr, and id.)

syntax

(define-simple-normalizing-binder (id pattern ...)
  (template ...))
Defines a syntax transformer named id which "normalizes" its binding syntax. Its input is a form that uses the rather permissive binds splicing syntax class, and its output specifically uses the ([var val] ...) binding format expected by most Racket binding syntaxes.

Specifically, the generated macro is equivalent to the following, where pattern ... and template ... are expanded right away, and the rest of the ellipses are part of the generated macro:

(define-simple-macro (id pattern ... vars:binds body:expr ...)
  (template ... ([vars.var vars.val] ...)
    body ...))

(See expr.)

As an example, w- and w-loop are defined straightforwardly in terms of let:

(define-simple-normalizing-binder (w-)
  (let))
(define-simple-normalizing-binder (w-loop proc:id)
  (let proc))

1.2 Functional programming utilities

1.2.1 Bindings and recursion

procedure

(pass arg func)  any

  arg : any/c
  func : (-> any/c any)
Invokes the given procedure with the given argument value. In other words, (pass arg func) is just like (func arg) but in a different order.

This utility can come in handy when experimenting with a new operation that returns procedures—for example, match-lambda. Instead of going to the trouble to define another operation that acts as a let binding—in this case, matchit’s easy enough to use pass and a weak bracket to accomplish basically the same programming style:

> (match (list 1 2 3)
    [(list) #f]
    [(cons first rest) rest])

'(2 3)

> (pd / pass (list 1 2 3) / match-lambda
    [(list) #f]
    [(cons first rest) rest])

'(2 3)

syntax

(w- binds body-expr ...)

Works just like a let form with no proc-id, but uses the binds splicing syntax class for the syntax of its bindings, so parentheses can usually be omitted.

Examples:
> (w- ([a 1] [b 2])
    (+ a b))

3

> (w- [a 1 b 2]
    (+ a b))

3

> (w- a 1 b 2
    (+ a b))

3

syntax

(fn arg-id ... body-expr)

Creates a procedure with positional arguments arg-id ... and body body-expr.

This is only a frequently useful shorthand, not a full replacement of lambda. Unlike lambda, fn can only be used to create functions of fixed arity, with no keyword arguments, and the body may only consist of one expression (although this expression may be a begin form of course). Hence, programs that use fn may still need to use lambda on occasion.

Examples:
> (pd / hash-map (hash 'a 1 'b 2) / fn k v
    (format "(~s, ~s)" k v))

'("(a, 1)" "(b, 2)")

> (pd / build-list 5 / fn ~ / * 10 ~)

'(0 10 20 30 40)

syntax

(w-loop proc-id binds body ...)

Works just like a let form with a proc-id, but uses the binds splicing syntax class for the syntax of its bindings, so parentheses can usually be omitted.

This example reverses and squares the numbers in a list, using the next procedure to continue the loop:

> (pd / w-loop next original (list 1 2 3) result (list)
    (expect original (cons first rest) result
    / next rest / cons (* first first) result))

'(9 4 1)

syntax

(loopfn proc-id arg-id ... body-expr)

Creates a procedure with positional arguments arg-id ... and body body-expr, which can refer to itself in the body using the name proc-id.

1.2.2 Conditionals

syntax

(mat val-expr pat then-expr else-expr)

syntax

(expect val-expr pat else-expr then-expr)

Checks whether pat matches the result of val-expr. If it does, this evaluates then-expr in tail position with the bindings introduced by pat. Otherwise, this evaluates else-expr without any new bindings.

The only difference between mat and expect is the order of then-expr and else-expr in the form. When these are used with Parendown’s weak opening brackets, they enable a programming style where run time error checking and other early exit conditions are kept toward the top of a procedure body, without affecting the indentation of the procedure’s main logic.

Examples:
> (pd / define (rev lst)
    (w-loop next lst lst result (list)
  
  
      (mat lst (list) result
  
  
  
      / expect lst (cons first rest)
        (error "Expected a list")
  
  
  
      / next rest / cons first result)))
> (rev (list 1 2 3))

'(3 2 1)

> (rev 3)

Expected a list

syntax

(matfns pat then-expr elsefn-expr)

 
  elsefn-expr : (-> any/c any)
Returns a procedure. The procedure takes a single argument value and checks whether it matches the match pattern pat. If it does, the procedure evaluates then-expr in tail position with the bindings introduced by pat. Otherwise, the procedure makes a tail call to the procedure resulting from elsefn-expr, passing in the same argument value.

syntax

(expectfn pat else-expr then-expr)

Returns a procedure. The procedure takes a single argument value and checks whether it matches the match pattern pat. If it does, the procedure evaluates then-expr in tail position with the bindings introduced by pat. Otherwise, the procedure evaluates else-expr in tail position without any new bindings.

syntax

(dissect val-expr pat then-expr)

Checks whether pat matches the result of val-expr. If it does, this evaluates then-expr in tail position with the bindings introduced by pat. Otherwise, the exn:misc:match? exception is raised.

If you need a custom error message, use expect with an expression that raises an exeption.

syntax

(dissectfn pat then-expr)

Returns a procedure. The procedure takes a single argument value and checks whether it matches the match pattern pat. If it does, the procedure evaluates then-expr in tail position with the bindings introduced by pat. Otherwise, the exn:misc:match? exception is raised.

If you need a custom error message, use expectfn with an expression that raises an exeption.