Deutsch   English   Français   Italiano  
<vbj9pc$1qhv5$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: (funcall #'or my-list)
Date: Sun, 8 Sep 2024 04:39:11 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <vbj9pc$1qhv5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 08 Sep 2024 06:39:11 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="28a2661e92d5fbf480e61cc300081820";
	logging-data="1918949"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/B0DD40EQU7iBk1UQ8ea48"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:SflS1AHIiV5lkiOhI4utOGqsPCE=
Bytes: 1785

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)