/
cl-stl-functional.lisp
1184 lines (993 loc) · 36 KB
/
cl-stl-functional.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
(in-package :cl-stl)
(declaim (inline not1
not2
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) bind1st
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) bind2nd
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) ptr_fun1
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) ptr_fun2
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) mem_fun
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) mem_fun_ref
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) mem_fun1
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14) mem_fun1_ref
#-cl-stl-0x98 is_placeholder
#-cl-stl-0x98 is_bind_expression
#-cl-stl-0x98 target))
;-------------------------------------------------------------------------------
;
; functor basis
;
;-------------------------------------------------------------------------------
(defclass functor (clonable)
((closure :type cl:function
:initform nil
:initarg :closure
:accessor __functor-closure)))
(defclass unary_function (functor) ()) ; deprecated in 0x11 or later
(defclass binary_function (functor) ()) ; deprecated in 0x11 or later
;-------------------------------------------------------------------------------
;
; default methods for functor
;
;-------------------------------------------------------------------------------
(labels ((empty-fnc (&rest args)
(declare (ignore args))
(error 'bad_function_call :what "empty function.")))
(defmethod initialize-instance :after ((fnctr functor) &key)
(let ((closure (__functor-closure fnctr)))
(if (null closure)
(setf (__functor-closure fnctr) #'empty-fnc)
(closer-mop:set-funcallable-instance-function fnctr closure))))
(defmethod (setf __functor-closure) :after (closure (fnctr functor))
(if (null closure)
(setf (__functor-closure fnctr) #'empty-fnc)
(closer-mop:set-funcallable-instance-function fnctr closure))))
(defmethod functor_function ((func cl:function))
func)
(defmethod functor_function ((func functor))
(__functor-closure func))
(defmethod operator_clone ((func cl:function))
func)
(defmethod operator_clone ((func functor))
(make-instance (type-of func)
:closure (__functor-closure func)))
(defmacro define-functor (name (&rest superclasses) (&rest slot-specifiers) &rest class-options)
`(defclass ,name (,@superclasses closer-mop:funcallable-standard-object)
,slot-specifiers
(:metaclass closer-mop:funcallable-standard-class)
,@class-options))
;-------------------------------------------------------------------------------
;
; 20.3.2, arithmetic operations:
;
;-------------------------------------------------------------------------------
;------------------------------------------------------------
; class plus
;------------------------------------------------------------
(define-functor plus (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function) ())
(declare-constructor plus (0))
(labels ((__plus (arg1 arg2) (+ arg1 arg2)))
(define-constructor plus ()
(make-instance 'plus :closure #'__plus)))
;------------------------------------------------------------
; class minus
;------------------------------------------------------------
(define-functor minus (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function) ())
(declare-constructor minus (0))
(labels ((__minus (arg1 arg2) (- arg1 arg2)))
(define-constructor minus ()
(make-instance 'minus :closure #'__minus)))
;------------------------------------------------------------
; class multiplies
;------------------------------------------------------------
(define-functor multiplies (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function) ())
(declare-constructor multiplies (0))
(labels ((__multiplies (arg1 arg2) (* arg1 arg2)))
(define-constructor multiplies ()
(make-instance 'multiplies :closure #'__multiplies)))
;------------------------------------------------------------
; class divides
;------------------------------------------------------------
(define-functor divides (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function) ())
(declare-constructor divides (0))
(labels ((__divides (arg1 arg2) (/ arg1 arg2)))
(define-constructor divides ()
(make-instance 'divides :closure #'__divides)))
;------------------------------------------------------------
; class modulus
;------------------------------------------------------------
(define-functor modulus (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function) ())
(declare-constructor modulus (0))
(labels ((__modulus (arg1 arg2) (mod arg1 arg2)))
(define-constructor modulus ()
(make-instance 'modulus :closure #'__modulus)))
;------------------------------------------------------------
; class negate
;------------------------------------------------------------
(define-functor negate (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function) ())
(declare-constructor negate (0))
(labels ((__negate (arg) (* -1 arg)))
(define-constructor negate ()
(make-instance 'negate :closure #'__negate)))
;-------------------------------------------------------------------------------
;
; 20.3.3, comparisons:
;
;-------------------------------------------------------------------------------
;------------------------------------------------------------
; class equal_to
;------------------------------------------------------------
(define-functor equal_to (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :initform nil
:initarg :operator
:accessor equal_to-operator)))
(declare-constructor equal_to (0 1))
(labels ((__equal_to-ctor (op)
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'equal_to
:operator op
:closure (lambda (arg1 arg2)
(funcall fnc arg1 arg2))))))
(define-constructor equal_to ()
(__equal_to-ctor #'operator_==))
#-cl-stl-noextra
(define-constructor equal_to (op)
(__equal_to-ctor op))
(defmethod operator_clone ((func equal_to))
(__equal_to-ctor (equal_to-operator func))))
;------------------------------------------------------------
; class not_equal_to
;------------------------------------------------------------
(define-functor not_equal_to (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :initform nil
:initarg :operator
:accessor not_equal_to-operator)))
(declare-constructor not_equal_to (0 1))
(labels ((__not_equal_to-ctor (op)
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'not_equal_to
:operator op
:closure (lambda (arg1 arg2)
(funcall fnc arg1 arg2))))))
(define-constructor not_equal_to ()
(__not_equal_to-ctor #'operator_/=))
#-cl-stl-noextra
(define-constructor not_equal_to (op)
(__not_equal_to-ctor op))
(defmethod operator_clone ((func not_equal_to))
(__not_equal_to-ctor (not_equal_to-operator func))))
;------------------------------------------------------------
; class greater
;------------------------------------------------------------
(define-functor greater (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :initform nil
:initarg :operator
:accessor greater-operator)))
(declare-constructor greater (0 1))
(labels ((__greater-ctor (op)
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'greater
:operator op
:closure (lambda (arg1 arg2)
(funcall fnc arg1 arg2))))))
(define-constructor greater ()
(__greater-ctor #'operator_>))
#-cl-stl-noextra
(define-constructor greater (op)
(__greater-ctor op))
(defmethod operator_clone ((func greater))
(__greater-ctor (greater-operator func))))
;------------------------------------------------------------
; class less
;------------------------------------------------------------
(define-functor less (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :initform nil
:initarg :operator
:accessor less-operator)))
(declare-constructor less (0 1))
(labels ((__less-ctor (op)
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'less
:operator op
:closure (lambda (arg1 arg2)
(funcall fnc arg1 arg2))))))
(define-constructor less ()
(__less-ctor #'operator_<))
#-cl-stl-noextra
(define-constructor less (op)
(__less-ctor op))
(defmethod operator_clone ((func less))
(__less-ctor (less-operator func))))
;------------------------------------------------------------
; class greater_equal
;------------------------------------------------------------
(define-functor greater_equal (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :initform nil
:initarg :operator
:accessor greater_equal-operator)))
(declare-constructor greater_equal (0 1))
(labels ((__greater_equal-ctor (op)
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'greater_equal
:operator op
:closure (lambda (arg1 arg2)
(funcall fnc arg1 arg2))))))
(define-constructor greater_equal ()
(__greater_equal-ctor #'operator_>=))
#-cl-stl-noextra
(define-constructor greater_equal (op)
(__greater_equal-ctor op))
(defmethod operator_clone ((func greater_equal))
(__greater_equal-ctor (greater_equal-operator func))))
;------------------------------------------------------------
; class less_equal
;------------------------------------------------------------
(define-functor less_equal (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :initform nil
:initarg :operator
:accessor less_equal-operator)))
(declare-constructor less_equal (0 1))
(labels ((__less_equal-ctor (op)
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'less_equal
:operator op
:closure (lambda (arg1 arg2)
(funcall fnc arg1 arg2))))))
(define-constructor less_equal ()
(__less_equal-ctor #'operator_<=))
#-cl-stl-noextra
(define-constructor less_equal (op)
(__less_equal-ctor op))
(defmethod operator_clone ((func less_equal))
(__less_equal-ctor (less_equal-operator func))))
;-------------------------------------------------------------------------------
;
; 20.3.4, logical operations:
;
;-------------------------------------------------------------------------------
;------------------------------------------------------------
; class logical_and
;------------------------------------------------------------
(define-functor logical_and (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function) ())
(declare-constructor logical_and (0))
(labels ((__logical_and (arg1 arg2)
(and arg1 arg2)))
(define-constructor logical_and ()
(make-instance 'logical_and :closure #'__logical_and)))
;------------------------------------------------------------
; class logical_or
;------------------------------------------------------------
(define-functor logical_or (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function) ())
(declare-constructor logical_or (0))
(labels ((__logical_or (arg1 arg2)
(or arg1 arg2)))
(define-constructor logical_or ()
(make-instance 'logical_or :closure #'__logical_or)))
;------------------------------------------------------------
; class logical_not
;------------------------------------------------------------
(define-functor logical_not (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function) ())
(declare-constructor logical_not (0))
(labels ((__logical_not (arg)
(not arg)))
(define-constructor logical_not ()
(make-instance 'logical_not :closure #'__logical_not)))
;-------------------------------------------------------------------------------
;
; 20.3.5, negators:
;
;-------------------------------------------------------------------------------
;------------------------------------------------------------
; class unary_negate & function not1
;------------------------------------------------------------
(define-functor unary_negate (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function)
((op :initform nil
:initarg :operator
:accessor unary_negate-operator)))
(declare-constructor unary_negate (1))
(define-constructor unary_negate (op)
#+cl-stl-warn-deprecated
(progn
#-(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(warn "unary_negate is deprecated."))
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'unary_negate
:operator op
:closure (lambda (arg)
(not (funcall fnc arg))))))
(defun not1 (op)
#+cl-stl-warn-deprecated
(progn
#-(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(warn "not1 is deprecated."))
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'unary_negate
:operator op
:closure (lambda (arg)
(not (funcall fnc arg))))))
(defmethod operator_clone ((func unary_negate))
(let* ((op (clone (unary_negate-operator func)))
(fnc (functor_function op)))
(make-instance 'unary_negate
:operator op
:closure (lambda (arg)
(not (funcall fnc arg))))))
;------------------------------------------------------------
; class binary_negate & function not2
;------------------------------------------------------------
(define-functor binary_negate (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :initform nil
:initarg :operator
:accessor binary_negate-operator)))
(declare-constructor binary_negate (1))
(define-constructor binary_negate (op)
#+cl-stl-warn-deprecated
(progn
#-(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(warn "binary_negate is deprecated."))
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'binary_negate
:operator op
:closure (lambda (arg1 arg2)
(not (funcall fnc arg1 arg2))))))
(defun not2 (op)
#+cl-stl-warn-deprecated
(progn
#-(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(warn "not2 is deprecated."))
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'binary_negate
:operator op
:closure (lambda (arg1 arg2)
(not (funcall fnc arg1 arg2))))))
(defmethod operator_clone ((func binary_negate))
(let* ((op (clone (binary_negate-operator func)))
(fnc (functor_function op)))
(make-instance 'binary_negate
:operator op
:closure (lambda (arg1 arg2)
(not (funcall fnc arg1 arg2))))))
;-------------------------------------------------------------------------------
;
; 20.3.6, binders:
;
;-------------------------------------------------------------------------------
;------------------------------------------------------------
; class binder1st & function bind1st
;------------------------------------------------------------
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-functor binder1st (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function)
((op :initform nil
:initarg :operator
:accessor binder1st-operator)
(arg :initform nil
:initarg :arg
:accessor binder1st-arg)))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(declare-constructor binder1st (2))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-constructor binder1st (op arg1)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "binder1st is deprecated."))
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'binder1st
:operator op
:arg arg1
:closure (lambda (arg2)
(funcall fnc arg1 arg2)))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun bind1st (functor arg)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "bind1st is deprecated."))
(let* ((op (clone functor))
(fnc (functor_function op)))
(make-instance 'binder1st
:operator op
:arg arg
:closure (lambda (arg2)
(funcall fnc arg arg2)))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defmethod operator_clone ((func binder1st))
(let* ((op (clone (binder1st-operator func)))
(arg1 (binder1st-arg func))
(fnc (functor_function op)))
(make-instance 'binder1st
:operator op
:arg arg1
:closure (lambda (arg2)
(funcall fnc arg1 arg2)))))
;------------------------------------------------------------
; class binder2nd & function bind2nd
;------------------------------------------------------------
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-functor binder2nd (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function)
((op :initform nil
:initarg :operator
:accessor binder2nd-operator)
(arg :initform nil
:initarg :arg
:accessor binder2nd-arg)))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(declare-constructor binder2nd (2))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-constructor binder2nd (op arg2)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "binder2nd is deprecated."))
(let* ((op (clone op))
(fnc (functor_function op)))
(make-instance 'binder2nd
:operator op
:arg arg2
:closure (lambda (arg1)
(funcall fnc arg1 arg2)))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun bind2nd (functor arg)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "bind2nd is deprecated."))
(let* ((op (clone functor))
(fnc (functor_function op)))
(make-instance 'binder2nd
:operator op
:arg arg
:closure (lambda (arg1)
(funcall fnc arg1 arg)))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defmethod operator_clone ((func binder2nd))
(let* ((op (clone (binder2nd-operator func)))
(arg2 (binder2nd-arg func))
(fnc (functor_function op)))
(make-instance 'binder2nd
:operator op
:arg arg2
:closure (lambda (arg1)
(funcall fnc arg1 arg2)))))
;-------------------------------------------------------------------------------
;
; 20.3.7, adaptors:
;
;-------------------------------------------------------------------------------
;------------------------------------------------------------
; class pointer_to_unary_function & function ptr_fun1
; ( NONSENSE in CL-STL )
;------------------------------------------------------------
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-functor pointer_to_unary_function (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function)
((op :type cl:function
:initform nil
:initarg :operator
:accessor pointer_to_unary_function-operator)))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(declare-constructor pointer_to_unary_function (1))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-constructor pointer_to_unary_function (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "pointer_to_unary_function is deprecated."))
(make-instance 'pointer_to_unary_function
:operator func
:closure (lambda (arg)
(funcall func arg))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun ptr_fun1 (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "ptr_fun1 is deprecated."))
(make-instance 'pointer_to_unary_function
:operator func
:closure (lambda (arg)
(funcall func arg))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defmethod operator_clone ((func pointer_to_unary_function))
(let ((func (pointer_to_unary_function-operator func)))
(make-instance 'pointer_to_unary_function
:operator func
:closure (lambda (arg)
(funcall func arg)))))
;------------------------------------------------------------
; class pointer_to_binary_function & function ptr_fun2
; ( NONSENSE in CL-STL )
;------------------------------------------------------------
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-functor pointer_to_binary_function (#-cl-stl-0x98 functor
#+cl-stl-0x98 binary_function)
((op :type cl:function
:initform nil
:initarg :operator
:accessor pointer_to_binary_function-operator)))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(declare-constructor pointer_to_binary_function (1))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-constructor pointer_to_binary_function (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "pointer_to_binary_function is deprecated."))
(make-instance 'pointer_to_binary_function
:operator func
:closure (lambda (arg1 arg2)
(funcall func arg1 arg2))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun ptr_fun2 (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "ptr_fun2 is deprecated."))
(make-instance 'pointer_to_binary_function
:operator func
:closure (lambda (arg1 arg2)
(funcall func arg1 arg2))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defmethod operator_clone ((func pointer_to_binary_function))
(let ((func (pointer_to_binary_function-operator func)))
(make-instance 'pointer_to_binary_function
:operator func
:closure (lambda (arg1 arg2)
(funcall func arg1 arg2)))))
;-------------------------------------------------------------------------------
;
; 20.3.8, adaptors:
;
;-------------------------------------------------------------------------------
;------------------------------------------------------------
; class mem_fun_t & function mem_fun etc.
; ( NONSENSE in CL-STL )
;------------------------------------------------------------
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-functor mem_fun_t (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function)
((op :type cl:function
:initform nil
:initarg :operator
:accessor mem_fun_t-operator)))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(declare-constructor mem_fun_t (1))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-constructor mem_fun_t (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "mem_fun_t is deprecated."))
(make-instance 'mem_fun_t
:operator func
:closure (lambda (obj)
(funcall func obj))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun mem_fun (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "mem_fun is deprecated."))
(make-instance 'mem_fun_t
:operator func
:closure (lambda (obj)
(funcall func obj))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun mem_fun_ref (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "mem_fun_ref is deprecated."))
(make-instance 'mem_fun_t
:operator func
:closure (lambda (obj)
(funcall func obj))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defmethod operator_clone ((func mem_fun_t))
(let ((func (mem_fun_t-operator func)))
(make-instance 'mem_fun_t
:operator func
:closure (lambda (obj)
(funcall func obj)))))
;------------------------------------------------------------
; class mem_fun1_t & function mem_fun1 etc.
; ( NONSENSE in CL-STL )
;------------------------------------------------------------
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-functor mem_fun1_t (#-cl-stl-0x98 functor
#+cl-stl-0x98 unary_function)
((op :type cl:function
:initform nil
:initarg :operator
:accessor mem_fun1_t-operator)))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(declare-constructor mem_fun1_t (1))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(define-constructor mem_fun1_t (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "mem_fun1_t is deprecated."))
(make-instance 'mem_fun1_t
:operator func
:closure (lambda (obj arg)
(funcall func obj arg))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun mem_fun1 (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "mem_fun1 is deprecated."))
(make-instance 'mem_fun1_t
:operator func
:closure (lambda (obj arg)
(funcall func obj arg))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defun mem_fun1_ref (func)
#+cl-stl-warn-deprecated
(progn
#-cl-stl-0x98 (warn "mem_fun1_ref is deprecated."))
(make-instance 'mem_fun1_t
:operator func
:closure (lambda (obj arg)
(funcall func obj arg))))
#+(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defmethod operator_clone ((func mem_fun1_t))
(let ((func (mem_fun1_t-operator func)))
(make-instance 'mem_fun1_t
:operator func
:closure (lambda (obj arg)
(funcall func obj arg)))))
;------------------------------------------------------------
; class mem_fun_ref-t;
;------------------------------------------------------------
; --> use mem_fun_t instead.
;------------------------------------------------------------
; class mem_fun1_ref-t;
;------------------------------------------------------------
; --> use mem_fun1_t instead.
;-------------------------------------------------------------------------------
;
; 0x11
;
;-------------------------------------------------------------------------------
#-cl-stl-0x98
(defun is_placeholder (arg)
(if (not (keywordp arg))
0
(handler-case (values (parse-integer (symbol-name arg)))
(error () 0))))
#-cl-stl-0x98
(defun is_bind_expression (arg)
(eq (type-of arg) '__binded-expr))
;------------------------------------------------------------
; class __binded-expr
;------------------------------------------------------------
#-cl-stl-0x98
(define-functor __binded-expr (functor)
((bindee :initarg :bindee
:accessor __binded-expr-bindee)
(params :type cl:vector
:initarg :params
:accessor __binded-expr-params)
(cloner :type cl:function
:initarg :cloner
:accessor __binded-expr-cloner)))
#-cl-stl-0x98
(defmethod operator_clone ((func __binded-expr))
(funcall (__binded-expr-cloner func) func))
;------------------------------------------------------------
; bind macro
;------------------------------------------------------------
#-cl-stl-0x98
(defmacro bind (func &rest args)
(let ((max-arg 0)
(arg-hash (make-hash-table)))
(labels ((get-argsym (idx)
(let ((is-new nil)
(ret (gethash idx arg-hash)))
(unless ret
(setf ret (gensym (format nil "ARG~A-" idx)))
(setf (gethash idx arg-hash) ret)
(setf is-new t)
(when (< max-arg idx)
(setf max-arg idx)))
(values ret is-new)))
(make-lambda-list (idx lambda-list ignore-list)
(if (< max-arg idx)
(values (nreverse lambda-list)
(nreverse ignore-list))
(multiple-value-bind (sym is-new) (get-argsym idx)
(cl:push sym lambda-list)
(when is-new
(cl:push sym ignore-list))
(make-lambda-list (1+ idx) lambda-list ignore-list))))
(imp (args acc1 acc2 prm-sym &optional (idx 0))
(if (null args)
(values (nreverse acc1)
(nreverse acc2))
(let* ((item (car args))
(ret (is_placeholder item)))
(if (< 0 ret)
(cl:push (get-argsym ret) acc2)
(progn
(cl:push `(aref ,prm-sym ,idx) acc2)
(cl:push `(_= (aref ,prm-sym ,idx) ,(car args)) acc1)
(incf idx)))
(imp (cdr args) acc1 acc2 prm-sym idx)))))
(let ((g-imp (gensym "IMP"))
(g-fnc (gensym "FNC"))
(g-bindee (gensym "BINDEE"))
(g-bd-fnc (gensym "BD-FNC"))
(g-params (gensym "PARAMS"))
(g-cloner (gensym "CLONER")))
(multiple-value-bind (arg-lst prm-lst) (imp args nil nil g-params)
(multiple-value-bind (lambda-list ignore-list) (make-lambda-list 1 nil nil)
`(labels ((,g-imp (,g-bindee ,g-params ,g-cloner)
(declare (type cl:vector ,g-params))
(let ((,g-bd-fnc (functor_function ,g-bindee)))
(declare (type cl:function ,g-bd-fnc))
(make-instance '__binded-expr
:bindee ,g-bindee :params ,g-params :cloner ,g-cloner
:closure (lambda (,@lambda-list)
,(when ignore-list
`(declare (ignore ,@ignore-list)))
(funcall ,g-bd-fnc ,@prm-lst))))))
(let* ((,g-bindee (clone ,func))
(,g-params (make-array ,(length arg-lst) :initial-element nil))
(,g-cloner (lambda (,g-fnc)
(,g-imp (clone (__binded-expr-bindee ,g-fnc))
(clone (__binded-expr-params ,g-fnc))
(__binded-expr-cloner ,g-fnc)))))
,@arg-lst
(,g-imp ,g-bindee ,g-params ,g-cloner)))))))))
;------------------------------------------------------------
; class function
;------------------------------------------------------------
#-cl-stl-0x98
(eval-when (:compile-toplevel :load-toplevel :execute)
(define-functor function (functor)
((target :initform nil
:initarg :target
:accessor __function-target))))
#-cl-stl-0x98
(declare-constructor function (0 1))
#-cl-stl-0x98
(labels ((__function-ctor (op)
(if (null op)
(make-instance 'function :target nil :closure nil)
(let* ((target (clone op)))
(make-instance 'function
:target target
:closure (functor_function target))))))
(define-constructor function ()
(__function-ctor nil))
(define-constructor function ((op (eql nil)))
(__function-ctor nil))
(define-constructor function ((op cl:function))
(__function-ctor op))
(define-constructor function ((op functor))
(__function-ctor op))
(define-constructor function ((fn function))
(__function-ctor (__function-target fn)))
(defmethod operator_clone ((fn function))
(__function-ctor (__function-target fn))))
#-cl-stl-0x98
(define-constructor function ((rm& remove-reference))
(with-reference (rm)
(let ((fnctr rm))
(__check-type-of-move-constructor fnctr function)
(let ((target (__function-target fnctr))
(closure (__functor-closure fnctr)))
(setf (__function-target fnctr) nil)
(setf (__functor-closure fnctr) nil)
(make-instance 'function :target target :closure closure)))))
#-cl-stl-0x98
(defmethod operator_cast ((fn function) (typename (eql 'boolean)))
(if (__function-target fn) t nil))
#-cl-stl-0x98
(locally (declare (optimize speed))
(labels ((__assign (fnc op)
(declare (type function fnc))
(if (null op)
(progn
(setf (__function-target fnc) nil)
(setf (__functor-closure fnc) nil))
(let* ((target (clone op))
(closure (functor_function op)))
(setf (__function-target fnc) target)
(setf (__functor-closure fnc) closure)))))
(declare (inline __assign))
(defmethod operator_= ((lhs function) rhs)
(error 'type-mismatch :what (format nil "Can't convert ~A to stl:function." rhs))
lhs)
(defmethod operator_= ((lhs function) (rhs function))
(__assign lhs (__function-target rhs))
lhs)
(defmethod operator_= ((lhs function) (rhs (eql nil)))
(setf (__function-target lhs) nil)
(setf (__functor-closure lhs) nil)
lhs)
(defmethod-overload assign ((this function) (fnc function))
(__assign this (__function-target fnc))
nil)
(defmethod-overload assign ((this function) (fnc (eql nil)))
(setf (__function-target this) nil)
(setf (__functor-closure this) nil)
nil)))
#-cl-stl-0x98
(defmethod operator_move ((lhs function) (rhs function))
(unless (eq lhs rhs)
(setf (__function-target lhs) (__function-target rhs))