Check-in [2b9e9a2608]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added keyword and optional syntax for lambda and define
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:2b9e9a26086207254b0e489f877b854ca41fc570
User & Date: aldo 2018-12-09 15:23:10
Context
2018-12-09
15:35
fixed bug for in args-fold.sls thanks to anonymous check-in: 75d1b77428 user: aldo tags: trunk
15:23
added keyword and optional syntax for lambda and define check-in: 2b9e9a2608 user: aldo tags: trunk
15:21
added redis check-in: 1eaafa95a1 user: aldo tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to thunder-utils.sls.

13
14
15
16
17
18
19
20


21
22


23
24
25
26
27
28
29
...
126
127
128
129
130
131
132

133









































































































134
135
;; See the License for the specific language governing permissions and
;; limitations under the License.

(library (thunder-utils)
  (export string-split string-replace bytevector-copy* read-string
	  print-stack-trace
	  sub-bytevector  sub-bytevector=?
	  load-bytevector save-bytevector)


  
  (import (scheme) (srfi s14 char-sets))



  ;; s is a string , c is a character-set or a list of chars
  ;; null strings are discarded from result by default unless #f is specified as third argument
  (define string-split
    (case-lambda
     [(s c)
      (string-split s c #t)]
................................................................................
      ((nest outer ... inner)
       (fold-right (lambda (o i)
		     (with-syntax (((outer ...) o)
				   (inner i))
		       #'(outer ... inner)))
		   #'inner (syntax->list #'(outer ...))))))












































































































  );library








|
>
>

|
>
>







 







>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
;; See the License for the specific language governing permissions and
;; limitations under the License.

(library (thunder-utils)
  (export string-split string-replace bytevector-copy* read-string
	  print-stack-trace
	  sub-bytevector  sub-bytevector=?
	  load-bytevector save-bytevector
	  define/optional lambda/optional /optional
	  define/keys lambda/keys /keys decode-keys)
  
  (import (scheme) (srfi s14 char-sets)
	  (only (srfi s1 lists) take drop)
	  (srfi private auxiliary-keyword))

  ;; s is a string , c is a character-set or a list of chars
  ;; null strings are discarded from result by default unless #f is specified as third argument
  (define string-split
    (case-lambda
     [(s c)
      (string-split s c #t)]
................................................................................
      ((nest outer ... inner)
       (fold-right (lambda (o i)
		     (with-syntax (((outer ...) o)
				   (inner i))
		       #'(outer ... inner)))
		   #'inner (syntax->list #'(outer ...))))))

  (define-auxiliary-keywords /optional /keys)

  (define-syntax define/optional
    (lambda (stx)
      (syntax-case stx (/optional)
	[(_ (name params ... (/optional opts ...)) body ...)
	 (let ([opts-list
		(map (lambda (opt)
		       (let ([x (syntax->datum opt)])
			 (if (list? x)
			     (list (datum->syntax (car (syntax->list opt)) (car x))
				   (datum->syntax (car (syntax->list opt)) (cadr x) ))
			     (list opt #f))))
		     (syntax->list #'(opts ...)))])
	   #`(define name
	       (case-lambda
		 #,@(map (lambda (i)
			   #`[(params ... #,@(take (map car opts-list) i))
			      (name params ...
				    #,@(take (map car opts-list) i)
				    #,@(drop (map cadr opts-list) i))])
			 (iota  (length opts-list)))
		 [(params ... #,@(map car opts-list))
		  body ...])))])))

  
  (define-syntax lambda/optional
    (lambda (stx)
      (syntax-case stx (/optional)
	[(_ (params ... (/optional opts ...)) body ...)
	 (let ([opts-list
		(map (lambda (opt)
		       (let ([x (syntax->datum opt)])
			 (if (list? x)
			     (list (datum->syntax (car (syntax->list opt)) (car x))
				   (datum->syntax (car (syntax->list opt)) (cadr x) ))
			     (list opt #f))))
		     (syntax->list #'(opts ...)))])
	   #`(letrec ([func 
		       (case-lambda
			 #,@(map (lambda (i)
				   #`[(params ... #,@(take (map car opts-list) i))
				      (func params ...
					    #,@(take (map car opts-list) i)
					    #,@(drop (map cadr opts-list) i))])
				 (iota  (length opts-list)))
			 [(params ... #,@(map car opts-list))
			  body ...])])
	       func))])))

  (define (decode-keys func-name names keys)
    (for-each (lambda (k)
		(unless (assq (car k) names)
		  (errorf func-name "unknown keyword argument ~d" k)))
	      keys)
    (apply values (map (lambda (name)
			 (let ([p (assq (if (pair? name) (car name) name) keys)])
			   (if p
			       (if (pair? (cdr p))
				   (cadr p)
				   (cdr p))
			       (if (pair? name)
				   (if (pair? (cdr name))
				       (cadr name)
				       (cdr name))
				   #f))))
		       names)))
  
  (define-syntax define/keys 
    (lambda (stx)
      (syntax-case stx (/keys)
	[(_ (name params ... (/keys keys ...)) body ...)
	 (let ([keys-list
		(map (lambda (key)
		       (let ([x (syntax->datum key)])
			 (if (list? x)
			     (list (datum->syntax (car (syntax->list key)) (car x))
				   (datum->syntax (car (syntax->list key)) (cadr x) ))
			     (list key #f))))
		     (syntax->list #'(keys ...)))])
 	   #`(define name
	       (lambda (params ... . keys*)
		 (import (only (data-structures) chop))
		 (import (only (thunder-utils) decode-keys))
		 (let-values ([(#,@(map car keys-list))
			       (decode-keys 'name '#,keys-list (chop keys* 2))])
		   body ...))))])))

  
  (define-syntax lambda/keys 
    (lambda (stx)
      (syntax-case stx (/keys)
	[(_ (params ... (/keys keys ...)) body ...)
	 (let ([keys-list
		(map (lambda (key)
		       (let ([x (syntax->datum key)])
			 (if (list? x)
			     (list (datum->syntax (car (syntax->list key)) (car x))
				   (datum->syntax (car (syntax->list key)) (cadr x) ))
			     (list key #f))))
		     (syntax->list #'(keys ...)))])
 	   #`(lambda (params ... . keys*)
	       (import (only (data-structures) chop))
	       (import (only (thunder-utils) decode-keys))
	       (let-values ([(#,@(map car keys-list))
			     (decode-keys 'func '#,keys-list (chop keys* 2))])
		 body ...)))])))
  );library