Sujet : Re: (funcall #'or my-list)
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 08. Sep 2024, 06:39:11
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vbj9pc$1qhv5$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Rob St. Amant wrote:
I've lately found a use for a function that calls a predicate on each
element of a list and returns all non-null results. (I bring this up
here because it has a SOME-like flavor).
(defun all-such-that (predicate list &key key)
"Return non-nil PREDICATE results for elements in LIST."
(loop for elt in list
as value = (if key
(funcall predicate (funcall key elt))
(funcall predicate elt))
when value
collect value))
Gauche Scheme
(define (all-such-that predicate lst :optional (key values))
(filter-map predicate (map key lst)))
(all-such-that (^n (and (odd? n) n)) '(1 2 3 4 5) square)
===>
(1 9 25)
Better:
(define (all-such-that predicate lst :optional (key values))
(filter-map (^x (and (predicate x) x)) (map key lst)))
(all-such-that odd? '(1 2 3 4 5) square)
===>
(1 9 25)