Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You are a bundle of energy, always on the go.


comp / comp.lang.scheme / Re: (funcall #'or my-list)

SubjectAuthor
o Re: (funcall #'or my-list)B. Pym

1
Subject: Re: (funcall #'or my-list)
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sun, 8 Sep 2024 04:39 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
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=
View all headers

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)

1

rocksolid light 0.9.8
clearnet tor