From c78fe1324732b6e9ab9cad4d1388c8c45ffb231c Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Fri, 23 May 2025 13:48:35 +0200 Subject: [PATCH] more general solution with rlambda --- decons.lisp | 10 ++++++++-- test-decons.lisp | 13 +++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/decons.lisp b/decons.lisp index 5d0482a..84333a1 100644 --- a/decons.lisp +++ b/decons.lisp @@ -65,13 +65,19 @@ (defun rapply (op &rest args) (if (null (cadr args)) (rcall op (car args)) - (apply #'rapply (lambda (x) (funcall op (car args) x)) (cdr args)) + ;(apply #'rapply (lambda (x) (funcall op (car args) x)) (cdr args)) + (apply #'rapply (rlambda op (car args)) (cdr args)) )) (defgeneric rcall (op arg) (:method (op arg) (funcall op arg)) (:method (op (arg list)) - (mapcar (lambda (x) (rcall op x)) arg))) + (mapcar (lambda (i) (rcall op i)) arg))) + +(defgeneric rlambda (op arg) + (:method (op arg) (lambda (j) (funcall op arg j))) + (:method (op (arg list)) + (lambda (j) (mapcar (lambda (i) (funcall op i j)) arg)))) ;;;; parameterized functions diff --git a/test-decons.lisp b/test-decons.lisp index c9cbb62..3a07685 100644 --- a/test-decons.lisp +++ b/test-decons.lisp @@ -11,7 +11,7 @@ (defun run () (let ((t:*test-suite* (t:test-suite "decons"))) (test-basic) - (test-lib) + (test-tensor) (test-rapply) (test-line) (t:show-result))) @@ -26,7 +26,7 @@ (== (decons:remainder 7 4) 3) ) -(deftest test-lib () +(deftest test-tensor () (== (decons:scalar-p 7) t) (== (decons:scalar-p '(a b)) nil) (let ((t1 (decons:tensor '(4) '(0 1 2 3)))) @@ -41,13 +41,18 @@ (== (decons:rapply #'1+ '(2 3)) '(3 4)) (== (decons:rapply #'+ 2 3) 5) (== (decons:rapply #'+ 3 '(4 5)) '(7 8)) + (== (decons:rapply #'+ '(2 3) '(4 5)) '((6 7) (7 8))) ; not '(6 8) + (== (decons:rapply #'- '(6 7) '(4 5)) '((2 3) (1 2))) ) (deftest test-line () (let ((p1 (decons:line 0.0)) (p2 (decons:line 1.0)) - (ds1 '((decons:tensor '(4) '(2.0 1.0 4.0 3.0)) - (decons:tensor '(4) '(1.8 1.2 4.2 3.3))))) + (ds1 '((2.0 1.0 4.0 3.0) + (1.8 1.2 4.2 3.3))) + ps1) (== (funcall p1 '(0.5 2.0)) 2.0) (== (funcall p2 '(0.5 2.0)) 2.5) + (setf ps1 (decons:line (car ds1))) + (== (funcall ps1 '(0.5 2.0)) '(3.0 2.5 4.0 3.5)) ))