unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams
@ 2016-09-13 16:23 Michael Heerdegen
  2016-09-13 18:02 ` Clément Pit--Claudel
  2016-09-13 22:20 ` Nicolas Petton
  0 siblings, 2 replies; 24+ messages in thread
From: Michael Heerdegen @ 2016-09-13 16:23 UTC (permalink / raw)
  To: Emacs Development; +Cc: Nicolas Petton

[-- Attachment #1: Type: text/plain, Size: 97 bytes --]

Hi,

I would like to install the following patch to Gnu Elpa.  Does it look
ok to you, Nicolas?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Pinpoint-semantics-of-seq-subseq-s-implementation-for-streams.patch --]
[-- Type: text/x-diff, Size: 2527 bytes --]

From 77871facc3b00b8fe6d032e38c4b229d114a97ae Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
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


[-- Attachment #3: Type: text/plain, Size: 21 bytes --]



Regards,

Michael.

^ permalink raw reply related	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2016-09-15 23:08 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-13 16:23 [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams Michael Heerdegen
2016-09-13 18:02 ` Clément Pit--Claudel
2016-09-13 21:17   ` Michael Heerdegen
2016-09-14  1:24     ` Clément Pit--Claudel
2016-09-14 15:05       ` Michael Heerdegen
2016-09-14 23:26         ` Clément Pit--Claudel
2016-09-15  0:51           ` John Mastro
2016-09-15  2:00             ` Clément Pit--Claudel
2016-09-15 17:01               ` John Mastro
2016-09-15 21:07               ` Michael Heerdegen
2016-09-15 22:18                 ` Clément Pit--Claudel
2016-09-15 22:28                   ` Michael Heerdegen
2016-09-15 22:52                     ` Clément Pit--Claudel
2016-09-15  0:58           ` Michael Heerdegen
2016-09-15  3:47             ` Clément Pit--Claudel
2016-09-15  8:42               ` Nicolas Petton
2016-09-15 22:30                 ` Michael Heerdegen
2016-09-15 23:08                   ` Nicolas Petton
2016-09-15 21:29               ` Michael Heerdegen
2016-09-14  1:28     ` John Wiegley
2016-09-14 15:15       ` Michael Heerdegen
2016-09-13 22:20 ` Nicolas Petton
2016-09-13 22:40   ` Michael Heerdegen
2016-09-14  8:25     ` Nicolas Petton

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).