[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams
From: |
Michael Heerdegen |
Subject: |
[PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams |
Date: |
Tue, 13 Sep 2016 18:23:31 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) |
Hi,
I would like to install the following patch to Gnu Elpa. Does it look
ok to you, Nicolas?
>From 77871facc3b00b8fe6d032e38c4b229d114a97ae Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <address@hidden>
Date: Wed, 7 Sep 2016 15:45:07 +0200
Subject: [PATCH] Pinpoint semantics of `seq-subseq's implementation for
streams
- Make argument END optional.
- Forbid negative index arguments.
- Add tests.
---
packages/stream/stream.el | 15 +++++++++++++--
packages/stream/tests/stream-tests.el | 10 ++++++++--
2 files changed, 21 insertions(+), 4 deletions(-)
diff --git a/packages/stream/stream.el b/packages/stream/stream.el
index 8c156f1..9954fc8 100644
--- a/packages/stream/stream.el
+++ b/packages/stream/stream.el
@@ -229,8 +229,19 @@ This function will eagerly consume the entire stream."
(setq stream (stream-rest stream)))
len))
-(cl-defmethod seq-subseq ((stream stream) start end)
- (seq-take (seq-drop stream start) (- end start)))
+(cl-defmethod seq-subseq ((stream stream) start &optional end)
+ "Return a stream of elements of STREAM from START to END.
+
+END is exclusive. If END is omitted, include all elements from
+START on. Both START and END must be non-negative. Since
+streams are a delayed type of sequences, don't signal an error if
+START or END are larger than the number of elements (the returned
+stream will simply be accordingly shorter, or even empty)."
+ (when (or (< start 0) (and end (< end 0)))
+ (error "seq-subseq: only non-negative indexes allowed for streams"))
+ (let ((stream-from-start (seq-drop stream start)))
+ (if end (seq-take stream-from-start (- end start))
+ stream-from-start)))
(cl-defmethod seq-into-sequence ((stream stream))
"Convert STREAM into a sequence."
diff --git a/packages/stream/tests/stream-tests.el
b/packages/stream/tests/stream-tests.el
index 16b5756..31c3530 100644
--- a/packages/stream/tests/stream-tests.el
+++ b/packages/stream/tests/stream-tests.el
@@ -112,8 +112,14 @@
(should (stream-empty-p (stream-rest (stream-rest rest))))))
(ert-deftest stream-seq-subseq-test ()
- ;; TODO
- )
+ (should (equal (seq-into (seq-subseq (stream (list 0 1 2 3 4)) 1 3) 'list)
+ (seq-subseq (list 0 1 2 3 4) 1 3)))
+ (should (= (stream-first (seq-subseq (stream-range 0) 5))
+ 5))
+ (should (= (stream-first (seq-subseq (seq-subseq (stream-range 0) 5) 5))
+ 10))
+
+ (should-error (seq-subseq (stream-range 0) -1)))
(ert-deftest stream-seq-into-test ()
(should (streamp (seq-into (stream-empty) 'stream)))
--
2.9.3
Regards,
Michael.
- [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams,
Michael Heerdegen <=
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Clément Pit--Claudel, 2016/09/13
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Michael Heerdegen, 2016/09/13
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Clément Pit--Claudel, 2016/09/13
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Michael Heerdegen, 2016/09/14
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Clément Pit--Claudel, 2016/09/14
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, John Mastro, 2016/09/14
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Clément Pit--Claudel, 2016/09/14
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, John Mastro, 2016/09/15
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Michael Heerdegen, 2016/09/15
- Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams, Clément Pit--Claudel, 2016/09/15