| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
7.5.18 SRFI-26 - specializing parameters
This SRFI provides a syntax for conveniently specializing selected parameters of a function. It can be used with,
(use-modules (srfi srfi-26))
- library syntax: cut slot …
- library syntax: cute slot …
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 tomy-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 ofwrite.<>A slot to be filled by an argument from the created procedure. Arguments are assigned to
<>slots in the order they appear in thecutform, 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
cutform.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))
For
cutthe specializing expressions are evaluated each time the new procedure is called. Forcutethey’re evaluated just once, when the new procedure is created. The namecutestands for “cutwith evaluated arguments”. In all cases the evaluations take place in an unspecified order.The following illustrates the difference between
cutandcute,(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
cutandcutewhere some expressions would be evaluated every time but others evaluated only once.)cutis really just a shorthand for the sort oflambdaforms shown in the above examples. But noticecutavoids the need to name unspecialized parameters, and is more compact. Use in functional programming style or just withmap,for-eachor similar is typical.(map (cut * 2 <>) '(1 2 3 4)) (for-each (cut write <> my-port) my-list)
| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on February 3, 2012 using texi2html 5.0.
