在这种情况下,map
可以考虑使用make-sequence
来创建结果序列。
但是,您的问题太宽泛了,任何答案基本上都是一种意见。你可以做任何你想做的事情,但本质上,最直接的方法是扩展序列类型参数以包含具有填充指针和/或可调整的向量:
;;; For use by the following deftype only
(defun my-vector-typep (obj &key (adjustable '*) (fill-pointer '*))
(and (or (eql adjustable '*)
;; (xor adjustable (adjustable-array-p obj))
(if adjustable
(adjustable-array-p obj)
(not (adjustable-array-p obj))))
(or (eql fill-pointer '*)
(if (null fill-pointer)
(not (array-has-fill-pointer-p obj))
(and (array-has-fill-pointer-p obj)
(or (eql fill-pointer t)
(eql fill-pointer (fill-pointer obj))))))))
;;; A type whose purpose is to describe vectors
;;; with fill pointers and/or that are adjustable.
;;;
;;; element-type and size are the same as in the type vector
;;; adjustable is a generalized boolean, or *
;;; fill-pointer is a valid fill pointer, or t or nil, or *
(deftype my-vector (&key element-type size adjustable fill-pointer)
(if (and (eql adjustable '*) (eql fill-pointer '*))
`(vector element-type size)
;; TODO: memoize combinations of adjustable = true/false/* and fill-pointer = t/nil/*
(let ((function-name (gensym (symbol-name 'my-vector-p))))
(setf (symbol-function function-name)
#'(lambda (obj)
(my-vector-p obj :adjustable adjustable :fill-pointer fill-pointer)))
`(and (vector ,element-type ,size)
(satisfies ,function-name)))))
(defun my-make-sequence (resulting-sequence-type size &key initial-element)
(when (eql resulting-sequence-type 'my-vector)
(setf resulting-sequence-type '(my-vector)))
(if (and (consp resulting-sequence-type)
(eql (first resulting-sequence-type) 'my-vector))
(destructuring-bind (&key (element-type '*) (type-size '* :size) (adjustable '*) (fill-pointer '*))
(rest resulting-sequence-type)
(assert (or (eql type-size '*) (<= size type-size)))
(make-array (if (eql type-size '*) size type-size)
:element-type (if (eql element-type '*) t element-type)
:initial-element initial-element
:adjustable (if (eql adjustable '*) nil adjustable)
:fill-pointer (if (eql fill-pointer '*) nil fill-pointer)))
(make-sequence resulting-sequence-type size
:initial-element initial-element)))
;; > (my-make-sequence '(my-vector :adjustable t) 10)
(defun my-map (resulting-sequence-type function &rest sequences)
(apply #'map-into
(my-make-sequence resulting-sequence-type
(if (null sequences)
0
(reduce #'min (rest sequences)
:key #'length
:initial-value (length (first sequence))))
function
sequences)))
;; > (my-map '(my-vector :adjustable t) #'+ '(1 2 3) '(3 2 1))
还有许多其他选择,例如简单地显式提供目标序列map-into
(已排序,可能使用树;如果它的元素很少,可能使用 conses)并且可以返回任何类型的序列。
真的,这是一个开放式问题,主要取决于您的需求和/或想象力。