Created
January 5, 2022 12:58
-
-
Save lokedhs/7cd32d974c082bc87b554a0113ef121f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun map-subseq (fn seq &optional start end from-end) | |
"Helper function to map SEQ between START and END." | |
(declare (type (or null array-index) start end) | |
(optimize (debug 0) (safety 1) | |
(compilation-speed 0))) | |
;; (when (and start end) | |
;; (assert (<= start end))) | |
(let ((start (or start 0)) | |
(fn (ensure-function fn))) | |
(fbind (fn) | |
(seq-dispatch seq | |
(if (null end) | |
(if from-end | |
(list-map-from-end/bordeaux fn seq :start start) | |
(dolist (item (nthcdr start seq)) | |
(fn item))) | |
(if from-end | |
(list-map-from-end/bordeaux fn seq :start start :end end) | |
(loop for item in (nthcdr start seq) | |
for i below (- end start) | |
do (fn item)))) | |
(with-subtype-dispatch vector | |
(simple-bit-vector | |
bit-vector | |
(simple-array character (*)) | |
simple-base-string) | |
seq | |
(let ((end (or end (length seq)))) | |
(if from-end | |
(loop for i downfrom (1- end) to start | |
do (fn (vref seq i))) | |
(loop for i from start below end | |
do (fn (vref seq i)))))) | |
(let ((end (or end (length seq)))) | |
(if from-end | |
(loop for i downfrom (1- end) to start | |
do (fn (elt seq i))) | |
(loop for i from start below end | |
do (fn (elt seq i))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment