source: project/wiki/eggref/5/holes @ 37979

Last change on this file since 37979 was 37979, checked in by juergen, 3 months ago

holes version 1.1

File size: 5.0 KB
Line 
1[[tags: egg]]
2
3== holes
4
5[[toc:]]
6
7=== Description
8
9Besides the documentation procedure, this module exports two curry
10procedures, @> and @<, one macro, @@, and a sharp-read-macro, ##, as
11well as a read-macro, ^, which abbreviates the call of that latter
12macro. They all create partial procedures.
13
14Holes are spezial identifiers, enclosing zero or more digits either by <
15and >, the new way, or by bangs !!, the old way.  You can choose between
16the two versions with a parameter named hole-delimiters.
17
18The macro @@ transforms expressions with zero or more holes into a
19procedure which has as many arguments as there are different holes in
20the expression. Moreover, the arguments are ordered according to the
21digits enclosed by the delimiters.
22
23For example, (@@ 5), ##5 or ^5, which are all the same, is a thunk,
24because there are no holes. And ^(- <2> <1> <2>) is a procedure of two
25arguments, <1> and <2> in this order, returning (- 20 10 20) when
26applied to 10 20: (^(- <2> <1> <2>) 10 20) is -10.
27
28But holes aren't restricted to flat list expressions, nested ones are
29accepted as well. For example, ^(+ 5 (* <> 2)) is a unary procedure
30which applied to 7 gives 19. The holes may appear in different
31nesting levels, which gives great flexibility.
32
33Notice the difference of @@ to cut and cute, where two pairs <> <> accept
34different arguments, @@ would accept only one.
35
36=== Documentation
37
38==== holes
39
40<procedure>(holes [sym])</procedure>
41
42documentation procedure. Shows either the list of exported symbols or
43the documentation of sym.
44
45==== hole-delimiters
46
47<parameter>(hole-delimiters [str])</parameter>
48
49returns or sets the hole-delimiters to either "<>", the default, or
50"!!".
51
52==== @@
53
54<macro>(@@ code)</macro>
55
56extracts the holes out of the argument expression, code, sorts them
57numerically while removing dups and considers the resulting list as the
58argument list of a procedure, with code as body.
59
60This macro can be called with sharp-read-syntax ## as well. Note, that
61##xpr is always a procedure, maybe a thunk, if there are no holes in
62xpr.
63
64==== @>
65
66<procedure>(@> proc . head)</procedure>
67
68returns a curried procedure with arguments tail, which applies proc to
69(append head tail)
70
71==== @<
72
73<procedure>(@< proc . tail)</procedure>
74
75returns a curried procedure with arguments head, which applies proc to
76(append head tail)
77
78=== Examples
79
80<enscript highlight=scheme>
81
82(hole-delimiters "!!")
83
84((@> map add1) '(0 1 2)) ; -> '(1 2 3)
85((@< list-ref 2) '(0 1 2 3)) ; -> 2
86((@@ 5)) ; -> 5
87(##5) ; -> 5
88##(vector 1 !!) ; -> procedure
89(call-with-values ##(values 1 2 3) list)
90  ; -> '(1 2 3)
91(##(+ !1! 5) 2) ; -> 7
92(##'(1 . 2)) ; -> '(1 . 2)
93(##(+ 5 (* !! 2)) 7) ; -> 19
94(##(!! 1 2 3) *) ; -> 6
95(##(!! 1 2 3) +) ; -> 6
96(##(list 1 2 (vector 3 4 !! 6)) 5)
97   ; -> '(1 2 #(3 4 5 6))
98(##(list 1 2 #(3 4 !! 6)))
99   ; note that the third arg of list is quoted
100   ; hence there are no holes and we get a thunk
101   ; -> '(1 2 #(3 4 !! 6)))
102(##(vector 1 2 !!) 3)
103   ; -> #(1 2 3)
104(##(list 1 !! 2 !1!) 3 4)
105   ; -> '(1 3 2 4)
106(##(list 1 !! 2 !!) 3)
107   ; -> '(1 3 2 3)
108(##(list 1 !! 2 (vector !!)) 3)
109   ; -> '(1 3 2 #(3))
110((##(lambda (x) (- x !!)) 2) 3) ; -> 1
111(##(list !! !1! 2 (vector !!)) 1 2)
112   ; -> '(1 2 2 #(1)))
113(##(cons !2! !1!) 1 2) ; -> '(2 . 1)
114(##(list (cons !2! !1!) (cons !1! !2!)) 1 2)
115   ; -> '((2 . 1) (1 . 2))
116(##(x y : (+ x y)) 1 2) ; -> 3
117</enscript>
118
119=== Requirements
120
121none
122
123== Last update
124
125Nov 07, 2019
126
127== Author
128
129[[/users/juergen-lorenz|Juergen Lorenz]]
130
131== License
132
133 Copyright (c) 2016-2019, Juergen Lorenz
134 All rights reserved.
135
136 Redistribution and use in source and binary forms, with or without
137 modification, are permitted provided that the following conditions are
138 met:
139 
140 Redistributions of source code must retain the above copyright
141 notice, this list of conditions and the following disclaimer.
142 
143 Redistributions in binary form must reproduce the above copyright
144 notice, this list of conditions and the following disclaimer in the
145 documentation and/or other materials provided with the distribution.
146 Neither the name of the author nor the names of its contributors may be
147 used to endorse or promote products derived from this software without
148 specific prior written permission.
149   
150 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
151 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
152 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
153 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
154 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
155 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
156 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
157 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
158 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
159 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
160 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
161
162== Version History
163; 1.1 : parameter hole-delimiters added accepting "<>" or "!!"
164; 1.0 : port from chicken-4, version 1.4,  with modifications
165
Note: See TracBrowser for help on using the repository browser.