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

Last change on this file since 37989 was 37989, checked in by juergen, 11 months ago

holes 1.2 docu

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