From c55c0ada15ebcef74cf6764de8e6cd387d80e4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Ga=C5=82kowski?= Date: Mon, 28 Mar 2022 16:59:46 +0200 Subject: [PATCH] add CONCATENATE --- src/sequence.lisp | 13 +++++++++++++ tests/seq.lisp | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/sequence.lisp b/src/sequence.lisp index 9cf0816f..9fc21d04 100644 --- a/src/sequence.lisp +++ b/src/sequence.lisp @@ -458,6 +458,19 @@ (funcall result-collector (apply function args)) (apply function args))))) +(defun check-sequence-type-specifier (specifier) + (unless (member specifier '(vector string list)) + (error "Not a valid sequence type specifier"))) + +(defun concatenate (result-type &rest sequences) + (check-sequence-type-specifier result-type) + (let ((iterators (mapcar #'make-iterator sequences)) + (result-collector (make-collector result-type))) + (dolist (it iterators) + (do ((arg (funcall it) (funcall it))) + ((eq *iterator-done* arg)) + (funcall result-collector arg))) + (funcall result-collector))) ;;; remove duplicates (defun %remove-duplicates (seq from-end test test-not key start end) diff --git a/tests/seq.lisp b/tests/seq.lisp index e121213f..761c565f 100644 --- a/tests/seq.lisp +++ b/tests/seq.lisp @@ -277,6 +277,24 @@ (list acc result)) '((3 2 1) t)) +;;; CONCATENATE +(test-equal + (concatenate 'string "all" " " "together" " " "now") + "all together now") + +(test-equal + (concatenate 'list "ABC" '(d e f) #(1 2 3) "1011") + '(#\A #\B #\C D E F 1 2 3 #\1 #\0 #\1 #\1)) + +(test-equal + (concatenate 'list) + nil) + +(test-equal + (handler-case + (concatenate '(vector * 2) "a" "bc") + (error () :signalled)) + :signalled) ;;; REMOVE-DUPLICATES