I am looking for a concise way to map a variadic function across multiple lists, but instead of passing the lists as separate arguments as with MAPCAR, I want to pass a single list consisting of any number of lists, and map through those contained lists. I don’t know beforehand how many lists are in the enclosing list, so I can’t destructure it.
I’ve tried combining MAPCAR and APPLY in all sorts of ways, but can’t figure it out. Must I give up on using MAP and just write the iteration explicitly?
Here’s a function that does what I want:
(defun map-within (fn list-of-lists &optional(maptype #'mapcar))
"Map FN on the lists contained in LIST-OF-LISTS"
(cond ((null list-of-lists) nil)
((null (cdr list-of-lists)) (car list-of-lists))
(t
(funcall maptype fn
(car list-of-lists)
(map-within fn (cdr list-of-lists) maptype)))))
where
(map-within #'+ '((1 2 3) (10 20 30) (100 200 300))) => (111 222 333)
Is there some magical application of a lambda made from a map that could express this with just one line?
You can use
applylike this: