推广Lisp函数

时间:2017-03-07 16:22:48

标签: mapping common-lisp generalization

为了快速进行原型设计,我想开始构建Common Lisp中提供的一些基本功能的通用版本库,而不是仅为当前任务收集(或导入)专用功能。例如,map函数适度推广以对任何类型的序列进行操作,但不处理可调整的向量。在下面编写特殊用途扩展名似乎足以满足当前用途,但仍然有限:

(defun map-adjustable-vector (function adjustable-vector)
  "Provides mapping across items in an adjustable vector."
  (let ((new-adj-vec 
          (make-array (array-total-size adjustable-vector)
                        :element-type (array-element-type adjustable-vector)
                        :adjustable t
                        :fill-pointer (fill-pointer adjustable-vector))))
    (dotimes (i (array-total-size adjustable-vector))
      (setf (aref new-adj-vec i) (funcall function (aref adjustable-vector i))))
    new-adj-vec))

我希望看到的是如何编写这样的函数,另外采用输出类型规范(包括新的'可调矢量类型)并允许多种列表和向量作为输入 - 换句话说,在map之后形成图案。

更广泛地说,了解是否存在与编写此类通用函数相关的基本原则或想法将是有用的。例如,专门针对输出类型规范的泛型方法是否是上述函数的可行方法?或者,或许可以利用map本身的概括,如前一篇文章Mapping into a tree of nested sequences中所示的coredump?我不知道泛化是否有任何限制(除了某种低效率),但我想知道它能走多远。

1 个答案:

答案 0 :(得分:0)

在这种情况下,可以认为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)并且可以返回任何类型的序列。

真的,这是一个开放式的问题,主要取决于你的需求和/或想象力。