From: Okamsn via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 73431@debbugs.gnu.org
Cc: Nicolas Petton <nicolas@petton.fr>
Subject: bug#73431: Add `setf` support for `stream.el` in ELPA
Date: Mon, 23 Sep 2024 01:33:24 +0000 [thread overview]
Message-ID: <827cc7fc-10be-4b93-bd67-f275193e5d84@protonmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 310 bytes --]
Hello,
The attached patch adds `setf` support for `stream-first`,
`stream-rest`, and `seq-elt` for streams. The support for `setf` with
`seq-elt` for streams uses the added support for `stream-first`,
following the definition of `seq-elt` for streams.
Would you like anything changed?
Thank you.
[-- Attachment #2: 0001-Add-setf-support-to-stream.el.patch --]
[-- Type: text/x-patch, Size: 5425 bytes --]
From fed785a332bb335522a4b71ef8a68896f304e1d0 Mon Sep 17 00:00:00 2001
From: Earl Hyatt <okamsn@protonmail.com>
Date: Sun, 22 Sep 2024 19:23:36 -0400
Subject: [PATCH] Add setf support to stream.el.
* stream.el (\(setf\ stream-first\), \(setf\ stream-rest\)): Add support to
`setf' for stream-first and stream-rest.
* stream.el (\(setf\ seq-elt\)): Support `setf' with `seq-elt' for streams.
* tests/stream-tests.el (setf-stream-first, setf-stream-first-error)
(setf-stream-rest, setf-stream-rest-error, setf-stream-seq-elt)
(setf-stream-seq-elt-error): Add tests for the above features.
---
stream.el | 23 ++++++++++++++++
tests/stream-tests.el | 64 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+)
diff --git a/stream.el b/stream.el
index 7135ee0..eb8b179 100644
--- a/stream.el
+++ b/stream.el
@@ -212,11 +212,23 @@ (defun stream-first (stream)
Return nil if STREAM is empty."
(car (stream--force stream)))
+(defun \(setf\ stream-first\) (store stream)
+ "Set the first element of STREAM to value STORE."
+ (if (stream-empty-p stream)
+ (error "Cannot set first element of empty stream: %s" stream)
+ (setcar (stream--force stream) store)))
+
(defun stream-rest (stream)
"Return a stream of all but the first element of STREAM."
(or (cdr (stream--force stream))
(stream-empty)))
+(defun \(setf\ stream-rest\) (new-stream stream)
+ "Set the remainder of STREAM to NEW-STREAM."
+ (if (stream-empty-p stream)
+ (error "Cannot set remainder of empty stream: %s" stream)
+ (setcdr (stream--force stream) new-stream)))
+
(defun stream-append (&rest streams)
"Concatenate the STREAMS.
Requesting elements from the resulting stream will request the
@@ -273,6 +285,17 @@ (cl-defmethod seq-elt ((stream stream) n)
(setq n (1- n)))
(stream-first stream))
+(cl-defmethod \(setf\ seq-elt\) (store (stream stream) n)
+ "Set the element of STREAM at index N to value STORE."
+ (let ((stream-for-signal stream)
+ (n-for-signal n))
+ (while (> n 0)
+ (setq stream (stream-rest stream))
+ (setq n (1- n)))
+ (if (stream-empty-p stream)
+ (signal 'args-out-of-range (list stream-for-signal n-for-signal))
+ (setf (stream-first stream) store))))
+
(cl-defmethod seq-length ((stream stream))
"Return the length of STREAM.
This function will eagerly consume the entire stream."
diff --git a/tests/stream-tests.el b/tests/stream-tests.el
index ba304f1..f82c206 100644
--- a/tests/stream-tests.el
+++ b/tests/stream-tests.el
@@ -308,5 +308,69 @@ (deftest-for-delayed-evaluation (stream-scan #'* 1 (make-delayed-test-stream)))
(deftest-for-delayed-evaluation (stream-concatenate (stream (list (make-delayed-test-stream)
(make-delayed-test-stream)))))
+;; Test `setf' support
+(ert-deftest setf-stream-first ()
+ (should (= 100 (let ((test (stream (vector 0 1 2 3 4))))
+ (setf (stream-first test) 100)
+ (stream-first test))))
+
+ (should (= 100 (let ((test (stream-range 0 10 2)))
+ (setf (stream-first test) 100)
+ (stream-first test)))))
+
+(ert-deftest setf-stream-first-error ()
+ (should-error (let ((test (stream-empty)))
+ (setf (stream-first test) 100)
+ (stream-first test))))
+
+(ert-deftest setf-stream-rest ()
+ (should (equal '(0 11 12 13 14)
+ (let ((test (stream (vector 0 1 2 3 4))))
+ (setf (stream-rest test) (stream (list 11 12 13 14)))
+ (seq-into test 'list))))
+
+ (should (equal '(0 11 12 13 14)
+ (let ((test (stream-range 0 10 2)))
+ (setf (stream-rest test) (stream (list 11 12 13 14)))
+ (seq-into test 'list))))
+
+ (should (equal '(0 11 12 13 14)
+ (let ((test (stream-range 0 10 2)))
+ ;; Test using an evaluated stream.
+ (setf (stream-rest test)
+ (let ((stream (stream (list 11 12 13 14))))
+ (seq-do #'ignore stream)
+ stream))
+ (seq-into test 'list)))))
+
+(ert-deftest setf-stream-rest-error ()
+ (should-error (let ((test (stream-empty)))
+ (setf (stream-rest test) (stream (list 11 12 13 14)))
+ (seq-into test 'list))))
+
+(ert-deftest setf-stream-seq-elt ()
+ (should (= 100 (let ((test (stream (vector 0 1 2 3 4))))
+ (setf (seq-elt test 3) 100)
+ (seq-elt test 3))))
+
+ (should (equal '(0 1 2 100 4)
+ (let ((test (stream (vector 0 1 2 3 4))))
+ (setf (seq-elt test 3) 100)
+ (seq-into test 'list))))
+
+ (should (= 100 (let ((test (stream-range 0 10 2)))
+ (setf (seq-elt test 3) 100)
+ (seq-elt test 3))))
+
+ (should (equal '(0 2 4 100 8)
+ (let ((test (stream-range 0 10 2)))
+ (setf (seq-elt test 3) 100)
+ (seq-into test 'list)))))
+
+(ert-deftest setf-stream-seq-elt-error ()
+ (should-error (let ((test (stream (vector 0 1 2 3 4))))
+ (setf (seq-elt test 1000) 100))
+ :type 'args-out-of-range))
+
(provide 'stream-tests)
;;; stream-tests.el ends here
--
2.34.1
next reply other threads:[~2024-09-23 1:33 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-23 1:33 Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-09-24 10:15 ` bug#73431: Add `setf` support for `stream.el` in ELPA Philip Kaludercic
2024-09-24 13:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-25 0:17 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-25 2:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-25 20:22 ` Philip Kaludercic
2024-09-26 13:53 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-27 15:11 ` Philip Kaludercic
2024-09-27 16:14 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-27 20:08 ` Philip Kaludercic
2024-09-27 20:39 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-28 3:08 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-28 14:57 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-29 19:30 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-30 22:19 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-02 1:02 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-02 19:39 ` Philip Kaludercic
2024-10-03 0:19 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-04 8:55 ` Philip Kaludercic
2024-10-05 2:44 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-05 9:14 ` Philip Kaludercic
2024-10-06 1:36 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-19 0:59 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-21 15:48 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-21 20:39 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-22 13:12 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-24 2:51 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-27 10:06 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-11-08 1:45 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-11-18 2:16 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-27 14:26 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-28 9:42 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 1:15 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 2:00 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 9:57 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 10:35 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 12:43 ` Eli Zaretskii
2024-10-29 13:31 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 15:43 ` Eli Zaretskii
2024-10-29 16:09 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 17:06 ` Eli Zaretskii
2024-10-29 17:29 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 17:50 ` Eli Zaretskii
2024-10-29 15:03 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 15:05 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 16:19 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-29 16:25 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-19 10:41 ` Philip Kaludercic
2024-10-05 13:32 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-06 0:37 ` Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-27 23:55 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=827cc7fc-10be-4b93-bd67-f275193e5d84@protonmail.com \
--to=bug-gnu-emacs@gnu.org \
--cc=73431@debbugs.gnu.org \
--cc=nicolas@petton.fr \
--cc=okamsn@protonmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).