emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams


From: Michael Heerdegen
Subject: Re: [PATCH] Elpa: Pinpoint semantics of `seq-subseq' for streams
Date: Fri, 16 Sep 2016 00:30:28 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)

Nicolas Petton <address@hidden> writes:

> I agree, the patch is good as it is now, I'd like to have it
> installed.

Done.

FWIW, if you would like me to push this: I have implemented the sliding
window algorithm as suggested by John, but commented it out, and added a
(hopefully not too wordy) note why we raise an error for now for
negative indexes:

>From df5652f68fc0ca6730b3f830e805cf7e30e5615d Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <address@hidden>
Date: Fri, 16 Sep 2016 00:14:28 +0200
Subject: [PATCH] Add a comment to last change in stream.el and bump version

---
 packages/stream/stream.el | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/packages/stream/stream.el b/packages/stream/stream.el
index 9954fc8..db00b0d 100644
--- a/packages/stream/stream.el
+++ b/packages/stream/stream.el
@@ -4,7 +4,7 @@
 
 ;; Author: Nicolas Petton <address@hidden>
 ;; Keywords: stream, laziness, sequences
-;; Version: 2.2.1
+;; Version: 2.2.2
 ;; Package-Requires: ((emacs "25"))
 ;; Package: stream
 
@@ -237,11 +237,24 @@ 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)))
+  (if (or (< start 0) (and end (< end 0)))
+      ;; We could return something like this (for START and END < 0):
+      ;;
+      ;; (let ((cropped stream))
+      ;;   (cl-dotimes (_ (- start)) (stream-pop cropped))
+      ;;   (while (not (stream-empty-p cropped))
+      ;;     (stream-pop stream)
+      ;;     (stream-pop cropped))
+      ;;   (seq-take stream (- end start)))
+      ;;
+      ;; but we are not sure whether advertising negative indexes is a good
+      ;; idea: this would potentially create a bunch of thrown away objects,
+      ;; and "searching from the end" might be better in most use cases.  So
+      ;; we raise an error instead:
+      (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."
-- 
2.9.3


Michael.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]