(defun prior-sib-if (self list &optional (test-fn #'true-that))
"Find nearest preceding sibling passing TEST-FN"
(labels ((check-priors (sibs)
(if (eql self (first sibs))
nil
(or (check-priors (rest sibs))
(when (funcall test-fn (first sibs))
(first sibs))))))
(check-priors list)))
Ah, I missed that bit in the maze of twisty, recursive passages, all
alike. How about this bit of double loop delight:
(defun prior-sib-if (self list &optional (test-fn #'true-that))
"Find nearest preceding sibling passing TEST-FN"
(loop with candidates = nil
for node in list
until (eql node self) do (push node candidates)
finally (return (loop for c in candidates when (funcall test-fn c) retur
n c))))
Kenny Tilton wrote:
(defun prior-sib-if (self list &optional (test-fn #'true-that))
"Find nearest preceding sibling passing TEST-FN"
(labels ((check-priors (sibs)
(if (eql self (first sibs))
nil
(or (check-priors (rest sibs))
(when (funcall test-fn (first sibs))
(first sibs))))))
(check-priors list)))
Peter Seibel wrote:
Ah, I missed that bit in the maze of twisty, recursive passages, all
alike. How about this bit of double loop delight:
(defun prior-sib-if (self list &optional (test-fn #'true-that))
"Find nearest preceding sibling passing TEST-FN"
(loop with candidates = nil
for node in list
until (eql node self) do (push node candidates)
finally (return (loop for c in candidates when (funcall test-fn c) retur
n c))))
Gauche Scheme
(use srfi-1) ;; take-while
(define (prior-sib-if self the-list test-fn)
(find test-fn
(reverse (take-while (^x (not (equal? self x))) the-list))))
gosh> (prior-sib-if 8 '(0 2 3 4 5 6 8 2 8) even?)^^^^^^
6
gosh> (prior-sib-if 8 '(0 2 3 4 5 6 8 2 8) odd?)
5
Another way:
(define (prior-sib-if self the-list test-fn)
(let go ((lst the-list) (seen '()))
(if (equal? self (car lst))
(find test-fn seen)
(go (cdr lst) (cons (car lst) seen)))))
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 55:48:31 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,709 |