chickadee » holes

holes

Description

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

The macro transforms expressions with zero or more holes into a procedure. Insofar, it's a bit like cut or cute. But while in cut or cute a hole is the special identifier <>, in this module it's a pair of bangs, possibly enclosing a sequence of digits, for examples !! or !1!. In this way, holes needn't name different variables like in cut or cute.

Another difference to cut or cute is, that the holes may appear in nested expressions at different levels. This gives great flexibility. And the sharp-read-syntax ## adds ease of use.

The macro also provides an alternative syntax for anonymous procedures. ##(x y : xpr . xprs) abbreviates (lambda (x y) xpr . xprs)

Documentation

holes

holesprocedure

shows the definition of the @@ macro.

@@

(@@ code)syntax

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.

Alternatively, searches for a colon in code, checks if the expressions to the left of it are all symbols without dups, and considers those symbols as argument list of a procedure with body the expressions to the right of the colon.

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.

@>

@> proc #!rest headprocedure

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

@<

@< proc #!rest tailprocedure

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

->proc

(->proc code)syntax

alias to @@, deprecated

Examples

((@> 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

Feb 11, 2017

Author

Juergen Lorenz

License

Copyright (c) 2016-2017, 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.4
->proc renamed @@, curried procedures @> and @< added
1.3
added alternative lambda syntax with a colon
1.2
holes now numerically sorted
1.1
bug with adjacent holes fixed, regexes replaced
1.0
initial import

Contents »