This SRFI provides a syntax for conveniently specializing selected parameters of a function. It can be used with,
(use-modules (srfi srfi-26))
Return a new procedure which will make a call (slot ...) but with selected parameters specialized to given expressions.
An example will illustrate the idea. The following is a specialization of
write, sending output to
my-output-port,(cut write <> my-output-port) ⇒ (lambda (obj) (write obj my-output-port))
The special symbol
<>indicates a slot to be filled by an argument to the new procedure.
my-output-porton the other hand is an expression to be evaluated and passed, ie. it specializes the behaviour of
- A slot to be filled by an argument from the created procedure. Arguments are assigned to
<>slots in the order they appear in the
cutform, there's no way to re-arrange arguments.
The first argument to
cutis usually a procedure (or expression giving a procedure), but
<>is allowed there too. For example,(cut <> 1 2 3) ⇒ (lambda (proc) (proc 1 2 3))
- A slot to be filled by all remaining arguments from the new procedure. This can only occur at the end of a
For example, a procedure taking a variable number of arguments like
maxbut in addition enforcing a lower bound,(define my-lower-bound 123) (cut max my-lower-bound <...>) ⇒ (lambda arglist (apply max my-lower-bound arglist))
cutthe specializing expressions are evaluated each time the new procedure is called. For
cutethey're evaluated just once, when the new procedure is created. The name
cutestands for “
cutwith evaluated arguments”. In all cases the evaluations take place in an unspecified order.
The following illustrates the difference between
cute,(cut format <> "the time is ~s" (current-time)) ⇒ (lambda (port) (format port "the time is ~s" (current-time))) (cute format <> "the time is ~s" (current-time)) ⇒ (let ((val (current-time))) (lambda (port) (format port "the time is ~s" val))
(There's no provision for a mixture of
cutewhere some expressions would be evaluated every time but others evaluated only once.)
cutis really just a shorthand for the sort of
lambdaforms shown in the above examples. But notice
cutavoids the need to name unspecialized parameters, and is more compact. Use in functional programming style or just with
for-eachor similar is typical.(map (cut * 2 <>) '(1 2 3 4)) (for-each (cut write <> my-port) my-list)