• Re: (funcall #'or my-list)

    From B. Pym@21:1/5 to Rob St. Amant on Sun Sep 8 04:39:11 2024
    XPost: comp.lang.scheme

    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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)