Skip to content

Interested by those map-array macros? #2

@q3cpma

Description

@q3cpma

Made these "proper" macros that mimic the sequence map because I didn't like vectorize's need for symbols.

(defmacro map-array-into (dest fun &rest arrays)
  "map for arrays. Only the arrays' total-size must be equal."
  (assert arrays () "map-array-into needs at least one array argument")
  (let ((array-num (length arrays)))
    (once-only (dest (arrays ``(,,@arrays)))
      `(iter
         (initially (assert (apply #'= (array-total-size ,dest) (mapcar #'array-total-size ,arrays))
                            () "map-array-into array arguments must have the same total size"))
         (for i below (array-total-size (car ,arrays)))
         (with arrays-vec = (coerce ,arrays 'vector))
         (setf (row-major-aref ,dest i)
               (funcall ,fun
                        ,@(iter (for i below array-num)
                            (collect `(row-major-aref (aref arrays-vec ,i) i)))))
         (finally (return ,dest))))))

(defmacro map-array (fun &rest arrays)
  "map for arrays. Only the arrays' total-size must be equal.
The resulting array uses the dimensions of the first array parameter."
  (assert arrays () "map-array needs at least one array argument")
  (let ((array-num (length arrays)))
    (once-only ((arrays ``(,,@arrays)))
      `(iter
         (initially (assert (apply #'= (mapcar #'array-total-size ,arrays))
                            () "map-array array arguments must have the same total size"))
         (with ret = (make-array (array-dimensions (car ,arrays))))
         (for i below (array-total-size (car ,arrays)))
         (with arrays-vec = (coerce ,arrays 'vector))
         (setf (row-major-aref ret i)
               (funcall ,fun
                        ,@(iter (for i below array-num)
                            (collect `(row-major-aref (aref arrays-vec ,i) i)))))
         (finally (return ret))))))

would you be interested? If so, I'd convert iter to loop and make a PR. I also plan on making an array version for all the relevant sequence functions (substitute, count, find, position, replace, fill, etc...).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions