You are looking at historical revision 37979 of this page. It may differ significantly from its current revision.

holes

Description

Besides the documentation procedure, this module exports two curry procedures, @> and @<, one macro, @@, and a sharp-read-macro, ##, as well as a read-macro, ^, which abbreviates the call of that latter macro. They all create partial procedures.

Holes are spezial identifiers, enclosing zero or more digits either by < and >, the new way, or by bangs !!, the old way. You can choose between the two versions with a parameter named hole-delimiters.

The macro @@ transforms expressions with zero or more holes into a procedure which has as many arguments as there are different holes in the expression. Moreover, the arguments are ordered according to the digits enclosed by the delimiters.

For example, (@@ 5), ##5 or ^5, which are all the same, is a thunk, because there are no holes. And ^(- <2> <1> <2>) is a procedure of two arguments, <1> and <2> in this order, returning (- 20 10 20) when applied to 10 20: (^(- <2> <1> <2>) 10 20) is -10.

But holes aren't restricted to flat list expressions, nested ones are accepted as well. For example, ^(+ 5 (* <> 2)) is a unary procedure which applied to 7 gives 19. The holes may appear in different nesting levels, which gives great flexibility.

Notice the difference of @@ to cut and cute, where two pairs <> <> accept different arguments, @@ would accept only one.

Documentation

holes

[procedure] (holes [sym])

documentation procedure. Shows either the list of exported symbols or the documentation of sym.

hole-delimiters

[parameter] (hole-delimiters [str])

returns or sets the hole-delimiters to either "<>", the default, or "!!".

@@

[syntax] (@@ code)

extracts the holes out of the argument expression, code, sorts them numerically while removing dups and considers the resulting list as the argument list of a procedure, with code as body.

This macro can be called with sharp-read-syntax ## as well. Note, that ##xpr is always a procedure, maybe a thunk, if there are no holes in xpr.

@>

[procedure] (@> proc . head)

returns a curried procedure with arguments tail, which applies proc to (append head tail)

@<

[procedure] (@< proc . tail)

returns a curried procedure with arguments head, which applies proc to (append head tail)

Examples


(hole-delimiters "!!")

((@> map add1) '(0 1 2)) ; -> '(1 2 3)
((@< list-ref 2) '(0 1 2 3)) ; -> 2
((@@ 5)) ; -> 5
(##5) ; -> 5
##(vector 1 !!) ; -> procedure
(call-with-values ##(values 1 2 3) list)
  ; -> '(1 2 3)
(##(+ !1! 5) 2) ; -> 7
(##'(1 . 2)) ; -> '(1 . 2)
(##(+ 5 (* !! 2)) 7) ; -> 19
(##(!! 1 2 3) *) ; -> 6
(##(!! 1 2 3) +) ; -> 6
(##(list 1 2 (vector 3 4 !! 6)) 5)
   ; -> '(1 2 #(3 4 5 6))
(##(list 1 2 #(3 4 !! 6)))
   ; note that the third arg of list is quoted
   ; hence there are no holes and we get a thunk
   ; -> '(1 2 #(3 4 !! 6)))
(##(vector 1 2 !!) 3)
   ; -> #(1 2 3)
(##(list 1 !! 2 !1!) 3 4)
   ; -> '(1 3 2 4)
(##(list 1 !! 2 !!) 3)
   ; -> '(1 3 2 3)
(##(list 1 !! 2 (vector !!)) 3)
   ; -> '(1 3 2 #(3))
((##(lambda (x) (- x !!)) 2) 3) ; -> 1
(##(list !! !1! 2 (vector !!)) 1 2)
   ; -> '(1 2 2 #(1)))
(##(cons !2! !1!) 1 2) ; -> '(2 . 1)
(##(list (cons !2! !1!) (cons !1! !2!)) 1 2)
   ; -> '((2 . 1) (1 . 2))
(##(x y : (+ x y)) 1 2) ; -> 3

Requirements

none

Last update

Nov 07, 2019

Author

Juergen Lorenz

License

Copyright (c) 2016-2019, Juergen Lorenz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission. 
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version History

1.1
parameter hole-delimiters added accepting "<>" or "!!"
1.0
port from chicken-4, version 1.4, with modifications