• .Re: #'reduce

    From Robert L.@21:1/5 to All on Sat Mar 26 22:09:32 2022
    (reduce #'max '("This" "is" "a" "test" ".") :key #'length)


    Reduce doesn't accept :KEY according to CLtL2. In fact, if you
    look at REDUCE more closely, you'll see that :KEY wouldn't make sense.

    Instead of REDUCE, try the following:

    (APPLY #'MAX (MAPCAR #'LENGTH '("This" "is" "a" "test" ".")))

    Even more efficient would be:

    (LET ((max 0))
    (DOLIST (elt '("This" "is" "a" "test" "."))
    (SETF max (MAX max (LENGTH elt))))
    max)


    Macintosh Common Lisp (MCL) accepts the :key keyword parameter:
    ? (reduce #'max '("This" "is" "a" "test" ".") :key #'length)
    4

    You can also use loop to make it clearer:
    ? (loop for el in '("This" "is" "a" "test" ".")
    maximizing (length el))
    4

    Gauche Scheme:

    (use gauche.lazy) ;; For lmap.

    (fold max 0 (lmap string-length '("is" "a" "test")))

    ===>
    4

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