Created
February 23, 2013 19:36
-
-
Save Fuco1/5021014 to your computer and use it in GitHub Desktop.
-slice for dash
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 -slice (list &optional from to) | |
"Return copy of LIST, starting from index FROM to index TO. | |
FROM or TO may be negative" | |
(setq from (or from 0)) | |
(setq to (or to 0)) | |
(let ((from-list list) | |
(result-list nil) | |
(index 0) | |
(len 0)) | |
(cond | |
((and (>= from 0) (>= to 0)) | |
(--dotimes from (!cdr from-list)) | |
(if (= to 0) | |
from-list | |
(--dotimes (- to from -1) | |
(when from-list | |
(!cons (car from-list) result-list) | |
(!cdr from-list))) | |
(nreverse result-list))) | |
((and (>= from 0) (< to 0)) | |
(--dotimes from (!cdr from-list)) | |
(while from-list | |
(!cons (car from-list) result-list) | |
(!cdr from-list)) | |
(--dotimes (abs to) (!cdr result-list)) | |
(nreverse result-list)) | |
((and (< from 0) (>= to 0)) | |
(while from-list | |
(!cons (car from-list) result-list) | |
(!cdr from-list) | |
(setq len (1+ len))) | |
(--dotimes (- len to 1) (!cdr result-list)) | |
(--dotimes (- (abs from) (- len to 1)) | |
(!cons (car result-list) from-list) | |
(!cdr result-list)) | |
from-list) | |
((and (< from 0) (< to 0)) | |
(while from-list | |
(!cons (car from-list) result-list) | |
(!cdr from-list) | |
(setq len (1+ len))) | |
(--dotimes (abs to) (!cdr result-list)) | |
(--dotimes (- (abs from) (abs to)) | |
(!cons (car result-list) from-list) | |
(!cdr result-list)) | |
from-list) | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment