• 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)
  • From B. Pym@21:1/5 to Frode Vatvedt Fjeld on Sat Aug 2 23:54:56 2025
    XPost: comp.lang.scheme

    Frode Vatvedt Fjeld wrote:

    What about (find-if #'identity my-list)? That seems to work.
    Anything better?

    For an obfuscation contest, I think (find-if #'null my-list :key
    #'not) would do well :) Otherwise, the function "some" is often
    used here. I like to define "true" as an alias for the "identity"
    function for such uses.

    Also, loop is often useful:

    (loop for x in my-list thereis x)

    Gauche Scheme

    Using "+" for "identity".

    gosh> (any + '(#f #f "DD"))
    "DD"
    gosh> (any + '(#f #f))
    #f

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