emacs-devel
[Top][All Lists]

## Re: [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations

 From: Yuri Khan Subject: Re: [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations Date: Mon, 26 Sep 2016 00:41:02 +0600

```On Sun, Sep 25, 2016 at 10:38 PM, Michael Heerdegen

> If you asking the question implies that you find the semantics not
> trivially clear

Yes. Naming things is listed as the second hardest problem in computer
science, and having a concise explanation of the semantics helps with
that greatly.

> maybe the following simpler (even superior) approach
> would be much better: Add an optional integer type argument to
> `seq-drop-while' and `seq-take-while' that allows to shift the index by
> the specified amount (as far as possible):
>
> #+begin_src emacs-lisp
> (cl-defgeneric seq-drop-while (pred sequence &optional less)
>   (seq-drop sequence (- (seq--count-successive pred sequence) (or less 0))))
>
> (cl-defgeneric seq-take-while (pred sequence &optional more)
>   (seq-take sequence (+ (seq--count-successive pred sequence) (or more 0))))
> #+end_src

Let me see if I can formulate the semantics here. I will work from
cases first and then try to describe the intended behavior in a single
sentence.

seq-drop-while

* Easiest happy case: A finite number, no fewer than LESS, of initial
elements satisfy PRED. They are counted, then LESS subtracted,
yielding a non-negative count of elements to drop.

* Fewer than LESS (possibly zero) initial elements satisfy PRED. They
are counted, then LESS subtracted, yielding a negative drop count.
Presumably then seq-drop returns the original sequence.

* The sequence is infinite and its every successive element satisfies
PRED. Then the above implementation diverges, trying to count to
infinity.

My guess at intended semantics: Return the subsequence of SEQUENCE
obtained by dropping all but the last LESS initial elements that
satisfy PRED. (This does not explain the negative drop count case.)

seq-take-while

* A finite number (possibly zero) of initial elements satisfy PRED,
and after that follow at least MORE elements.

* A finite number (possibly zero) of initial elements satisfy PRED,
and after that follow fewer than MORE (possibly zero) elements.
Presumably, seq-take will return the original sequence.

* The sequence is infinite and its every successive element satisfies
PRED. The counting implementation will again diverge.

My guess: Return the initial subsequence of SEQUENCE whose elements
satisfy PRED, followed by at most MORE more successive elements.
(Another implementation can be devised that handles the infinite
case.)

Looking back at the thread, I recall that this exploration started
from my conjecture that detecting the presence of an absorbing element
could make reduction lazy wrt all following elements. The possibility
is fun to explore, but I am now thinking that was not such a useful
idea.

```