Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding remove-nth #182

Open
bo-tato opened this issue Dec 2, 2024 · 3 comments
Open

Consider adding remove-nth #182

bo-tato opened this issue Dec 2, 2024 · 3 comments

Comments

@bo-tato
Copy link
Contributor

bo-tato commented Dec 2, 2024

For advent of code this year I used the following utility function:

(defun remove-nth (n seq)
  "Remove element with index N from SEQ."
  (append (subseq seq 0 n)
          (subseq seq (1+ n))))

It seems this is a fairly common utility function:

If you want to add this we should probably make it return the same type of sequence it was passed, rather than always returning a list. class-of seems to work although I'm not 100% sure it works for all sequence types or is the correct way to do it. type-of doesn't work as then with (remove-nth 1 #(0 1 2 3)) I get the error that the length (3) doesn't match the type (simple-vector 4)

(defun remove-nth (n seq)
  "Remove element with index N from SEQ."
  (concatenate (class-of seq)
               (subseq seq 0 n)
               (subseq seq (1+ n))))
@ruricolist
Copy link
Owner

There is an internal function, make-sequence-like, that handles making a sequence with a specific length and the same type as another.

@bo-tato
Copy link
Contributor Author

bo-tato commented Dec 23, 2024

Looks interesting but it's SBCL only. SBCL defines implementation for list and vector and otherwise errors unimplemented. concatenate (class-of seq) works portably for list and vector, including subtypes like '(vector double-float). I am not sure of any libraries that implement custom sbcl sequence types to test with but I think they would work also as concatenate will end up calling (make-sequence (class-of seq) ...

@ruricolist
Copy link
Owner

Actually, now that I think about it, this could be defined in terms of splice-seq already, although splice-seq doesn't support extensible sequences (yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants