-
Notifications
You must be signed in to change notification settings - Fork 5
/
useful-macros.lisp
2464 lines (2074 loc) · 71.8 KB
/
useful-macros.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; useful_macros.lisp -- A collection of really handy macros
;;
;; DM/HMSC 11/97
;; -----------------------------------------------------------
(in-package "USEFUL-MACROS")
;; -----------------------------------------------------------
(defun wholepart (x)
(truncate x))
(defun fracpart (x)
(- x (wholepart x)))
;; ----------------------------------------
(defmacro with (bindings &body body)
`(let* ,bindings ,@body))
(defmacro letp (bindings &body body)
`(let ,bindings ,@body))
#+:LISPWORKS
(editor:setup-indent "with" 1)
#+:LISPWORKS
(editor:setup-indent "letp" 1)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun collect-decls (forms)
(nlet iter ((forms forms)
(decls nil))
(let ((form (car forms)))
(if (or (stringp form)
(and (consp form)
(eq (car form) 'declare)))
(iter (cdr forms) (cons form decls))
(values forms (nreverse decls))
))))
(defun symbol-gensym (s)
(gensym (format nil "~A-" (symbol-name s))))
) ;; end of eval-when
(defmacro! nlet-tail (n letargs &rest body)
(let ((gs (mapcar (lambda (arg)
(declare (ignore arg))
(gensym))
letargs))
(gsx (mapcar (lambda (arg)
(declare (ignore arg))
(gensym))
letargs)))
(multiple-value-bind (body decls)
(collect-decls body)
`(macrolet
((,n ,gs
`(progn
(psetq
,@(apply 'nconc
(mapcar
'list
',gsx
(list ,@gs))))
(go ,',g!n))))
(block ,g!b
(let ,(mapcar #2`(,a1 ,(cadr a2)) gsx letargs)
(tagbody
,g!n
(let ,(mapcar #2`(,(car a2) ,a1) gsx letargs)
,@decls
(return-from
,g!b (progn ,@body)))))) ))))
(defmacro with-gensyms (syms &body body)
`(let ,(mapcar #`(,a1 (symbol-gensym ',a1)) syms)
,@body))
;; -------------------------------------------------------------------
;; WITH-TAIL-PURE-CODE -- Compiled code using this macro can run recursively
;; all day long without ever blowing the stack.
;; Tail calls are effectively made into direct jumps.
;;
;; N.B. DM/RAL 02/07 -- tests with LWM 5.01 indicate that this may be
;; unnecessary. Code must be compiled for tail optimization. Interpreted code
;; has problems no matter what.
(defmacro with-tail-pure-code (&body body)
`(locally
(declare (optimize (debug 1) (safety 1)))
,@body))
;; ----------------------------------------------------------------------
(defmacro! dlambda (&rest ds)
`(lambda (&rest ,g!args)
(let ((,g!tail (cdr ,g!args)))
(case (car ,g!args)
,@(mapcar
(lambda (d)
`(,(if (eq t (car d))
t
(list (car d))
;; (car d)
)
(apply (lambda ,@(cdr d))
,(if (eq t (car d))
g!args
g!tail)) ))
ds)))))
(defmacro dcase (args &rest clauses)
`(apply (um:dlambda
,@clauses)
,args))
#+:LISPWORKS
(editor:setup-indent "dcase" 1)
;; ----------------------------------------------------------------------
#|
(um:defmacro! make-state-machine ((initial-state inp-var
&key on-reset)
&rest state-bindings)
`(let ((,g!state ,initial-state)
,g!state-stack)
(macrolet ((push-state (state)
`(push ,state ,',g!state-stack))
(pop-state ()
`(pop ,',g!state-stack)))
(dlambda
(:reset ()
(setf ,g!state ,initial-state
,g!state-stack nil)
,on-reset)
(t (,inp-var)
(cond ,@(mapcar
(lambda (binding)
(destructuring-bind (from-state &rest sub-clauses)
binding
(let (t-seen)
`((eq ,g!state ,from-state)
(cond ,@(mapcar
(lambda (subclause)
(destructuring-bind (test to-state &rest clause)
subclause
(setf t-seen (or t-seen
(eq test 't)
(eq test 'otherwise)))
`(,test
(setf ,g!state ,to-state)
,@clause)))
sub-clauses)
,@(unless t-seen
`((t (error "State machine error in state: ~A"
,g!state)))) ))
)))
state-bindings)
(t (error "State machine error")) ))) )))
#+:LISPWORKS
(editor:setup-indent "make-state-machine" 2 4)
;; ----------------------------------------------------------------------
(um:defmacro! run-state-machine ((initial-state (inp-var step-expr)) &rest state-bindings)
`(let ((,g!machine (make-state-machine (,initial-state ,inp-var) ,@state-bindings)))
(block ,g!block
(tagbody
,g!top
(multiple-value-bind (,g!ans ,g!done)
(funcall ,g!machine ,step-expr)
(if ,g!done
(return-from ,g!block ,g!ans)
(go ,g!top))
)))))
#+:LISPWORKS
(editor:setup-indent "run-state-machine" 2 4)
|#
;; ----------------------------------------------------------------------
;; ----------------------------------------------------------------------
(defun make-rubber-vector (&key (length 16) element-type)
(make-array length
:fill-pointer 0
:adjustable t
:element-type element-type))
;; ----------------------------------------------------------------------
;; ----------------------------------------------------------------------
(defmacro! ichain-before (&rest body)
`(let ((,g!indir-env ,a!this))
(setq ,a!this
(lambda (&rest ,g!temp-args)
,@body
(apply ,g!indir-env
,g!temp-args)))))
(defmacro! ichain-after (&rest body)
`(let ((,g!indir-env ,a!this))
(setq ,a!this
(lambda (&rest ,g!temp-args)
(prog1
(apply ,g!indir-env
,g!temp-args)
,@body)))))
(defmacro! ichain-intercept (&rest body)
`(let ((,g!indir-env ,a!this))
(setq ,a!this
(lambda (&rest ,g!temp-args)
(block ,g!intercept
(macrolet ((,a!intercept (v)
`(return-from
,',g!intercept
,v)))
(prog1
(apply ,g!indir-env
,g!temp-args)
,@body)))))))
(defmacro! alet-hotpatch (letargs &rest body)
`(let ((,a!this) ,@letargs)
(setq ,a!this ,@(last body))
,@(butlast body)
(dlambda
(:hotpatch (closure)
(setq ,a!this closure))
(t (&rest args)
(apply ,a!this args)))))
(defmacro! let-hotpatch (letargs &rest body)
`(let ((,g!this) ,@letargs)
(setq ,g!this ,@(last body))
,@(butlast body)
(dlambda
(:hotpatch (closure)
(setq ,g!this closure))
(t (&rest args)
(apply ,g!this args)))))
(defun let-binding-transform (bs)
(if bs
(cons
(cond ((symbolp (car bs))
(list (car bs)))
((consp (car bs))
(car bs))
(t
(error "Bad let bindings")))
(let-binding-transform (cdr bs)))))
;; ----------------------------------------------------------------------
(defun tree-leaves%% (tree test result)
(if tree
(if (listp tree)
(cons
(tree-leaves%% (car tree) test result)
(tree-leaves%% (cdr tree) test result))
(if (funcall test tree)
(funcall result tree)
tree))))
(defmacro tree-leaves (tree test result)
`(tree-leaves%%
,tree
(lambda (x)
(declare (ignorable x))
,test)
(lambda (x)
(declare (ignorable x))
,result)))
(defmacro sublet (bindings% &rest body)
(let ((bindings (let-binding-transform
bindings%)))
(setq bindings
(mapcar
(lambda (x)
(cons (gensym (symbol-name (car x))) x))
bindings))
`(let (,@(mapcar 'list
(mapcar 'car bindings)
(mapcar 'caddr bindings)))
,@(tree-leaves
body
#1=(member x bindings :key 'cadr)
(caar #1#)))))
(defmacro sublet* (bindings &rest body)
`(sublet ,bindings
,@(mapcar 'macroexpand-1 body)))
(defmacro! pandoriclet (letargs &rest body)
(let ((letargs (cons
'(a!this)
(let-binding-transform
letargs))))
`(let (,@letargs)
(setq ,a!this ,@(last body))
,@(butlast body)
(dlambda
(:pandoric-get (sym)
,(pandoriclet-get letargs))
(:pandoric-set (sym val)
,(pandoriclet-set letargs))
(t (&rest args)
(apply ,a!this args))))))
(defun pandoriclet-get (letargs)
`(case sym
,@(mapcar #`((,(car a1)) ,(car a1)) letargs)
(t (error
"Unknown pandoric get: ~a"
sym))))
(defun pandoriclet-set (letargs)
`(case sym
,@(mapcar #`((,(car a1))
(setq ,(car a1) val))
letargs)
(t (error
"Unknown pandoric set: ~a"
sym))))
(declaim (inline get-pandoric))
(defun get-pandoric (box sym)
(funcall box :pandoric-get sym))
(defsetf get-pandoric (box sym) (val)
`(progn
(funcall ,box :pandoric-set ,sym ,val)
,val))
(defmacro! with-pandoric (syms o!box &rest body)
`(symbol-macrolet
(,@(mapcar #`(,a1 (get-pandoric ,g!box ',a1))
syms))
,@body))
(defun pandoric-hotpatch (box new)
(with-pandoric (this) box
(setq this new)))
(defmacro! pandoric-recode (vars box new)
`(with-pandoric (,a!this ,@vars) ,box
(setq ,a!this ,new)))
(defmacro! plambda (largs pargs &rest body)
(let ((pargs (mapcar 'list pargs)))
`(let (,a!this ,a!self)
(setq
,a!this (lambda ,largs ,@body)
,a!self (dlambda
(:pandoric-get (sym)
,(pandoriclet-get pargs))
(:pandoric-set (sym val)
,(pandoriclet-set pargs))
(t (&rest args)
(apply ,a!this args)))))))
(defmacro! defpan (name args &rest body)
`(defun ,name (,a!self)
,(if args
`(with-pandoric ,args ,a!self
,@body)
`(progn ,@body))))
(defvar pandoric-eval-tunnel)
(defmacro pandoric-eval (vars expr)
`(let ((pandoric-eval-tunnel
(plambda () ,vars t)))
(eval `(with-pandoric
,',vars pandoric-eval-tunnel
,,expr))))
;; ----------------------------------------------------------------------
(defmacro dis (args &rest body)
`(disassemble
(compile nil
(lambda ,(mapcar (lambda (a)
(if (consp a)
(cadr a)
a))
args)
(declare
,@(mapcar
#`(type ,(car a1) ,(cadr a1))
(remove-if-not 'consp args)))
,@body))))
;; ----------------------------------------------------------------------
(defmacro! pointer-& (obj)
`(lambda (&optional (,g!set ',g!temp))
(if (eq ,g!set ',g!temp)
,obj
(setf ,obj ,g!set))))
(defun pointer-* (addr)
(funcall addr))
(defsetf pointer-* (addr) (val)
`(funcall ,addr ,val))
(defsetf pointer-& (addr) (val)
`(setf (pointer-* ,addr) ,val))
;; ----------------------------------------------------------------------
(defmacro! with-fast-stack
((sym &key (type 'fixnum) (size 1000)
(safe-zone 100))
&rest body)
`(let ((,g!index ,safe-zone)
(,g!mem (make-array ,(+ size (* 2 safe-zone))
:element-type ',type)))
(declare (type (simple-array ,type) ,g!mem)
(type fixnum ,g!index))
(macrolet
((,(symb 'fast-push- sym) (val)
`(locally #f
(setf (aref ,',g!mem ,',g!index) ,val)
(incf ,',g!index)))
(,(symb 'fast-pop- sym) ()
`(locally #f
(decf ,',g!index)
(aref ,',g!mem ,',g!index)))
(,(symb 'check-stack- sym) ()
`(progn
(if (<= ,',g!index ,,safe-zone)
(error "Stack underflow: ~a"
',',sym))
(if (<= ,,(- size safe-zone)
,',g!index)
(error "Stack overflow: ~a"
',',sym)))))
,@body)))
;; ----------------------------------------------------------------------
(declaim (inline make-tlist tlist-left
tlist-right tlist-empty-p))
(defun make-tlist () (cons nil nil))
(defun tlist-left (tl) (caar tl))
(defun tlist-right (tl) (cadr tl))
(defun tlist-empty-p (tl) (null (car tl)))
(declaim (inline tlist-add-left
tlist-add-right))
(defun tlist-add-left (tl it)
(let ((x (cons it (car tl))))
(if (tlist-empty-p tl)
(setf (cdr tl) x))
(setf (car tl) x)))
(defun tlist-add-right (tl it)
(let ((x (cons it nil)))
(if (tlist-empty-p tl)
(setf (car tl) x)
(setf (cddr tl) x))
(setf (cdr tl) x)))
(declaim (inline tlist-rem-left))
(defun tlist-rem-left (tl)
(if (tlist-empty-p tl)
(error "Remove from empty tlist")
(let ((x (car tl)))
(setf (car tl) (cdar tl))
(if (tlist-empty-p tl)
(setf (cdr tl) nil)) ;; For gc
(car x))))
(declaim (inline tlist-update))
(defun tlist-update (tl)
(setf (cdr tl) (last (car tl))))
(defvar number-of-conses 0)
(declaim (inline counting-cons))
(defun counting-cons (a b)
(incf number-of-conses)
(cons a b))
(defmacro! with-conses-counted (&rest body)
`(let ((,g!orig number-of-conses))
,@body
(- number-of-conses ,g!orig)))
(defmacro counting-push (obj stack)
`(setq ,stack (counting-cons ,obj ,stack)))
(defmacro with-cons-pool (&rest body)
`(let ((cons-pool)
(cons-pool-count 0)
(cons-pool-limit 100))
(declare (ignorable cons-pool
cons-pool-count
cons-pool-limit))
,@body))
(defmacro! cons-pool-cons (o!car o!cdr)
`(if (= cons-pool-count 0)
(counting-cons ,g!car ,g!cdr)
(let ((,g!cell cons-pool))
(decf cons-pool-count)
(setf cons-pool (cdr cons-pool))
(setf (car ,g!cell) ,g!car
(cdr ,g!cell) ,g!cdr)
,g!cell)))
(defmacro! cons-pool-free (o!cell)
`(when (<= cons-pool-count
(- cons-pool-limit 1))
(incf cons-pool-count)
(setf (car ,g!cell) nil)
(push ,g!cell cons-pool)))
(defmacro make-cons-pool-stack ()
`(let (stack)
(dlambda
(:push (elem)
(setf stack
(cons-pool-cons elem stack)))
(:pop ()
(if (null stack)
(error "Tried to pop an empty stack"))
(let ((cell stack)
(elem (car stack)))
(setf stack (cdr stack))
(cons-pool-free cell)
elem)))))
(with-cons-pool
(defun make-shared-cons-pool-stack ()
(make-cons-pool-stack)))
(defmacro with-dynamic-cons-pools (&rest body)
`(locally (declare (special cons-pool
cons-pool-count
cons-pool-limit))
,@body))
(defmacro fill-cons-pool ()
`(let (tp)
(loop for i from cons-pool-count
to cons-pool-limit
do (push
(cons-pool-cons nil nil)
tp))
(loop while tp
do (cons-pool-free (pop tp)))))
;; ----------------------------------------------------------------------
(defmacro! nif (o!expr pos zero neg)
`(cond ((plusp ,g!expr) ,pos)
((zerop ,g!expr) ,zero)
(t ,neg)))
#|
(nif x '+ 0 '-)
(defmacro! square (o!x)
`(* ,g!x ,g!x))
(square expr)
|#
;; ----------------------------------------------------------------------
(declaim (inline last1 single append1 conc1 mklist))
(defun append1 (lst obj)
(append lst (list obj)))
(defun conc1 (lst obj)
(nconc lst (list obj)))
(defun mklist (obj)
(if (listp obj)
obj
(list obj)))
(defun single (arg)
(and (consp arg)
(null (cdr (the cons arg)))))
(defun last1 (lst)
(car (the cons (last lst))))
;; ----------------------------------------------------
;; Special macro versions of functional composition operators
;; All of these expect a parenthesized list of named dummy args
;; immediately following the EXPANDED-xxx macro name, and prior to
;; the actual arguments
;;
(defmacro expanded-combine ((&rest args) op f1 f2)
`(lambda ,args
(funcall ,op (funcall ,f1 ,@args) (funcall ,f2 ,@args))))
(defmacro expanded-compose ((&rest args) &rest fns)
(cond ((null fns) `'identity)
((single fns) (car fns))
((single (rest fns))
`(lambda ,args
(funcall ,(first fns) (funcall ,(second fns) ,@args))
))
(t (let ((fn1 (last1 fns))
(fns (butlast fns)))
`(lambda ,args
(foldr 'funcall (list ,@fns) (funcall ,fn1 ,@args)))
))
))
(defmacro expanded-curry ((&rest suf-args) f &rest pref-args)
`(lambda ,suf-args
(funcall ,f ,@pref-args ,@suf-args)))
(defmacro expanded-rcurry ((&rest pref-args) f &rest suf-args)
`(lambda ,pref-args
(funcall ,f ,@pref-args ,@suf-args)))
(defmacro curried-lambda ((&rest args) &body body)
(if (rest args)
`(lambda (,(first args))
(curried-lambda ,(rest args)
,@body))
`(lambda ,args
,@body)))
#|
;; try them out...
(expanded-compose (tree) 'first 'second 'fifth)
(expanded-compose (tree) 'first 'second)
(expanded-combine (x) '+ 'second 'third)
(expanded-rcurry (seq) 'subseq start end)
(expanded-curry (val) '* 3)
(curried-lambda (a b c) (list a b c))
|#
;; -------------------------------------------------------------
(defmacro! allf (o!val &rest places)
`(setf ,@(mapcan (rcurry 'list #|place|# g!val) places)))
(defmacro nilf (&rest places)
`(allf nil ,@places))
#|
(nilf this that thother)
|#
(defmacro tf (&rest places)
`(allf t ,@places))
;; ------------------------------------
(define-modify-macro addf (&rest args)
+)
(define-modify-macro subf (&rest args)
-)
(define-modify-macro mulf (&rest args)
*)
(define-modify-macro divf (&rest args)
/)
;; ------------------------------------
(defmacro deletef (place item &rest args)
`(setf ,place (delete ,item ,place ,@args)))
(defmacro deletef-if (place pred &rest args)
`(setf ,place (delete-if ,pred ,place ,@args)))
(defmacro removef (place item &rest args)
`(setf ,place (remove ,item ,place ,@args)))
(defmacro removef-if (place pred &rest args)
`(setf ,place (remove-if ,pred ,place ,@args)))
(defmacro aconsf (place key val)
`(setf ,place (acons ,key ,val ,place)))
(defmacro conc1f (place obj)
`(setf ,place (nconc ,place (list ,obj))))
;; ------------------------------------
(defmacro self-init (place &body body)
`(or ,place
(setf ,place
(progn
,@body))))
#+:LISPWORKS
(editor:setup-indent "self-init" 1)
(defmacro! ensure-assoc ((key place &rest args) &body body)
`(let ((,g!key ,key))
(or (assoc ,g!key ,place ,@args)
(car (aconsf ,place ,g!key (progn ,@body))) )))
;; ------------------------------------
(defmacro while (test &body body)
`(do ()
((not ,test))
,@body))
(defmacro until (test &body body)
`(do ()
(,test)
,@body))
(defmacro foreach (fn &rest seqs)
`(map nil ,fn ,@seqs))
(defmacro if-let ((var val) t-clause &optional f-clause)
`(let ((,var ,val))
(if ,var
,t-clause
,f-clause)))
(defmacro when-let ((var val) &body body)
`(let ((,var ,val))
(when ,var
,@body)))
;; ------------------------------------------------------------
;; if-let*
;;
;; (if-let* ((s1 e1)
;; (s2 e2)
;; ... )
;; (... true clause ...)
;; (... false clause ....))
;;
;; => true-clause is only executed if *all* of the expressions in the let clauses are true
;; false-clause is executed if *any* of the expressions is false
;; bindings of sequential symbols in the let clauses are like let*, so that later clauses
;; may refer to symbols representing the tests of eariler clauses.
;; let clauses are evaluated in the order stated, and short-circuit evaluation is performed
;; so that a false let clause aborts the evaluation of all later let clauses.
(defmacro when-let* (let-clauses true-clause)
(labels ((generate-when-let* (lets)
(if lets
`(when-let ,(first lets)
,(generate-when-let* (rest lets)))
true-clause)))
(generate-when-let* let-clauses)))
#|
(when-let* ((a ea) (b eb)) tc)
|#
(defmacro! if-let* (let-clauses true-clause &optional false-clause)
(if false-clause
(labels ((generate-if-let* (lets)
(if lets
`(if-let ,(first lets)
,(generate-if-let* (rest lets))
(go ,g!false))
`(return-from ,g!block ,true-clause)) ))
`(block ,g!block
(tagbody
,(generate-if-let* let-clauses)
,g!false
(return-from ,g!block ,false-clause))))
`(when-let* ,let-clauses ,true-clause) ))
#|
(if-let* ((a ea) (b eb)) tc fc)
(if-let* ((a ea) (b eb)) tc)
|#
#|
;; -------------------------------------------------------
;; Define our own collector objects that
;; perform rapid nconc list accumulation
;;
;; DM/RAL 02/07 -- added a Lock for MP safety
(defclass <collector> ()
((hd :accessor collector-hd)
(tl :accessor collector-tl)
(lock :accessor collector-lock)))
(defmacro with-locked-collector ((c &rest lock-args) &body body)
`(mp:with-lock ((collector-lock ,c) ,@lock-args)
,@body))
(defun collector-discard-contents (c)
(with-locked-collector (c)
(let ((v (list nil)))
(setf (collector-hd c) v
(collector-tl c) v)
)))
(defmethod initialize-instance ((c <collector>) &key &allow-other-keys)
(setf (collector-lock c) (mp:make-lock :name "Collector Lock"))
(collector-discard-contents c))
(defun collector-contents (c &key (discard t))
;; make readout destructive to the collector object
;; so that we can't accidentally hand off a list that
;; is still undergoing active mutation
;;
;; If user doesn't want to discard contents,
;; then hand him a copy of the contents as they exist at this moment.
(with-locked-collector (c)
(let ((lst (cdr (the cons (collector-hd c)))))
(if discard
(progn
(collector-discard-contents c)
lst)
(copy-seq lst))
)))
(defun collector-ncontents (c)
(with-locked-collector (c)
(length (cdr (the cons (collector-hd c))))
))
(defun collector-empty-p (c)
(zerop (collector-ncontents c)))
(defun collector-append-item (c item)
(with-locked-collector (c)
(setf (collector-tl c)
(cdr (the cons (rplacd (the cons (collector-tl c)) (list item))))
)))
(defun collector-push-item (c item)
(with-locked-collector (c)
(setf (collector-hd c)
(cons nil (the cons (rplaca (the cons (collector-hd c)) item)))
)))
(defun collector-pop (c)
(with-locked-collector (c)
(let* ((lst (collector-contents c))
(v (car lst)))
(unless (endp lst)
(setf (collector-hd c) lst))
v)))
(defun make-collector ()
(make-instance '<collector>))
|#
;; ---------------------------------------------------------------------
#+:lispworks
(defun constituent (c)
(and (graphic-char-p c)
(not (lispworks:whitespace-char-p c))))
#-:lispworks
(progn
(defvar *whitespace-chars*
(list #\space #\tab #\newline #\return #\backspace #\Page))
(defun whitespace-char-p (c)
(member c *whitespace-chars*))
(defun constituent (c)
(and (graphic-char-p c)
(not (whitespace-char-p c)))))
#|
;; show whitespace chars
(dolist (item
(loop for ix from 0 below 256
for c = (code-char ix)
when (lw:whitespace-char-p c)
collect (list ix c)))
(format t "~%0x~2,'0x ~S" (first item) (second item)))
;; show graphics chars
(dolist (item
(loop for ix from 0 below 256
for c = (code-char ix)
when (graphic-char-p c)
collect (list ix c)))
(format t "~%0x~2,'0x ~S" (first item) (second item)))
|#
(defun tokens (str &key (test 'constituent) test-not (start 0) end key)
(let ((test (if test-not
(complement test-not)
test)))
(loop for p1 = (position-if test str :start start :end end :key key)
while p1
do (setf start (position-if-not test str :start p1 :end end :key key))
collect (subseq str p1 (or start end))
while start
)))
(defun tokens-if (test str &rest args)
(apply 'tokens str :test test args))
(defun tokens-if-not (test-not str &rest args)
(apply 'tokens str :test-not test-not args))
(defun split-string (str &key delims (start 0) end key)
(if delims
(tokens-if-not (rcurry 'find #|c|# delims) str
:start start :end end :key key)
(tokens str :start start :end end :key key)
))
(defun paste-strings (delim &rest args)
(with-output-to-string (s)
(when args
(princ (car args) s)
(dolist (arg (cdr args))
(princ delim s)
(princ arg s)
))
))
;; ----------------------------------------------------------------
(defmacro fn (args &body body)
`(lambda ,args ,@body))
(defmacro if* (test tclause &rest fclauses)
`(if ,test ,tclause
,(if (< (length fclauses) 2)
(first fclauses)
`(if* ,(first fclauses)
,(second fclauses)
,@(cddr fclauses))
)))
;; Graham's alambda
(defmacro! alambda (parms &body body)
`(labels ((,a!self ,parms ,@body))
#',a!self))
(defmacro! aif (test tclause &optional fclause)
`(let ((,a!it ,test))
(if ,a!it ,tclause ,fclause)))
(defmacro! aif* (test tclause &rest fclauses)
`(let ((,a!it ,test))
(if ,a!it ,tclause
,(if (< (length fclauses) 2)
(first fclauses)
`(aif* ,(first fclauses)
,(second fclauses)
,@(cddr fclauses))
))))
(defmacro! awhen (test &rest clauses)
`(let ((,a!it ,test))
(when ,a!it ,@clauses)))
;; ------------------------------------------------------------------
(defmethod longer ((x list) (y list))
(do ((lx x (cdr lx))
(ly y (cdr ly)))
((or (null lx)
(null ly)) lx)))
(defmethod longer (x y)
(> (length x) (length y)))
(defun filter (fn seq)
(remove-if (complement fn) seq))
#|
(defun group (seq n)
(when (zerop n)
(error "zero length"))
(if seq
(let ((c (make-collector)))
(nlet-tail rec ((seq seq))
(cond ((zerop (length seq))
(collector-contents c))
((>= (length seq) n)