From: Michael Heerdegen <michael_heerdegen@web.de>
To: Nicolas Petton <nicolas@petton.fr>
Cc: Emacs developers <emacs-devel@gnu.org>
Subject: Re: [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations
Date: Fri, 21 Apr 2017 04:34:27 +0200 [thread overview]
Message-ID: <87pog6lc98.fsf@drachen> (raw)
In-Reply-To: <87o9wtb5n6.fsf@drachen> (Michael Heerdegen's message of "Wed, 22 Mar 2017 18:09:01 +0100")
[-- Attachment #1: Type: text/plain, Size: 264 bytes --]
Michael Heerdegen <michael_heerdegen@web.de> writes:
> But we still want to add the new file to the "stream" package - correct?
Ok, find the final patch I intend to install attached. Shall I bump the
version of the stream package as well?
Regards,
Michael.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-file-stream-x.el-to-the-stream-package.patch --]
[-- Type: text/x-diff, Size: 5883 bytes --]
From 2c073c2aa3c2373c5a5f71d8565b79732e4a6a87 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
Date: Fri, 21 Apr 2017 04:14:15 +0200
Subject: [PATCH] Add file "stream-x.el" to the stream package
---
packages/stream/stream-x.el | 150 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 150 insertions(+)
create mode 100644 packages/stream/stream-x.el
diff --git a/packages/stream/stream-x.el b/packages/stream/stream-x.el
new file mode 100644
index 000000000..2f97102ba
--- /dev/null
+++ b/packages/stream/stream-x.el
@@ -0,0 +1,150 @@
+;;; stream-x.el --- Additional functions for working with streams -*- lexical-binding: t -*-
+
+;; Copyright (C) 2017 Free Software Foundation, Inc
+
+;; Author: Michael Heerdegen <michael_heerdegen@web.de>
+;; Maintainer: Michael Heerdegen <michael_heerdegen@web.de>
+;; Created: 2017_03_22
+;; Keywords: stream, laziness, sequences
+
+
+;; This file is not part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
+
+
+;;; Commentary:
+
+;; This file contains additional functions for working with streams.
+
+
+;;; Code:
+
+(require 'stream)
+
+
+(defun stream-substream-before (stream rest)
+ "Return a stream of the elements of STREAM before REST.
+
+REST is a rest of STREAM: it must either be `eq' to STREAM or to
+one of the subsequent calls of `stream-rest' on STREAM. The
+return value is a newly created stream containing the first
+elements of STREAM with REST cut off.
+
+When REST appears multiple times as a rest of STREAM, a stream
+with the minimal number of elements is returned."
+ (stream-make
+ (if (eq stream rest)
+ nil
+ (cons (stream-first stream)
+ (stream-substream-before (stream-rest stream) rest)))))
+
+(defun stream-divide-with-get-rest-fun (stream get-rest-fun)
+ "Divide STREAM into two parts according to GET-REST-FUN.
+
+The return value is a list (S R) where R is the result of
+(funcall get-rest-fun STREAM) and S a stream of minimal length so
+that (stream-append S R) is equivalent to STREAM.
+
+Calling GET-REST-FUN on STREAM must be `eq' to one of
+STREAM, (stream-rest STREAM), (stream-rest (stream-rest STREAM)),
+..."
+ (let ((rest (funcall get-rest-fun stream)))
+ (list (stream-substream-before stream rest) rest)))
+
+(defun stream-divide (stream predicate)
+ "Divide STREAM between the first pair of elements for that PREDICATE fails.
+
+When STREAM generates the elements S_1, S_2, ..., call
+(PREDICATE S_i, S_i+1) for i=1,2,... until the first index i=k is
+found so that (funcall PREDICATE S_k S_k+1) returns nil.
+
+The return value is a list of two streams (HEAD REST) where
+HEAD generates the elements S_1, ... S_k and REST is the rest of STREAM
+generating the remaining elements S_k+1, ...
+
+Example:
+
+ (mapcar #'seq-into-sequence
+ (stream-divide
+ (stream (list 1 2 3 5 6 7 9 10 11 23))
+ (lambda (this next) (< (- next this) 2))))
+==> ((1 2 3)
+ (5 6 7 9 10 11 23))
+
+
+If STREAM is finite and no index k with (funcall PREDICATE S_k S_k+1) ==>
+nil is found, return (STREAM E) where E is an empty stream. When
+STREAM is infinite and no such index is found, this function will not
+terminate.
+
+See `stream-divide-with-get-rest-fun' for a generalization of this function."
+ (stream-divide-with-get-rest-fun stream (stream-divide--get-rest-fun predicate)))
+
+(defun stream-divide--get-rest-fun (pred)
+ (lambda (s)
+ (unless (stream-empty-p s)
+ (while (let ((this (stream-pop s)))
+ (unless (stream-empty-p s)
+ (funcall pred this (stream-first s))))))
+ s))
+
+(defun stream-partition (stream predicate)
+ "Partition STREAM into bunches where PREDICATE returns non-nil for subsequent elements.
+
+The return value is a stream S: S_1, S_2, ... of streams S_i of
+maximal length so that (stream-concatenate S) is equivalent to STREAM
+and for any pair of subsequent elements E, F in any S_i
+(PREDICATE E F) evals to a non-nil value.
+
+Often, but not necessarily, PREDICATE is an equivalence predicate.
+
+Example:
+
+ (seq-into-sequence
+ (seq-map #'seq-into-sequence
+ (stream-partition
+ (stream (list 1 2 3 5 6 7 9 10 15 23))
+ (lambda (x y) (< (- y x) 2)))))
+ ==> ((1 2 3)
+ (5 6 7)
+ (9 10)
+ (15)
+ (23))
+
+See `stream-partition-with-get-rest-fun' for a generalization of this function."
+ (stream-partition-with-get-rest-fun stream (stream-divide--get-rest-fun predicate)))
+
+(defun stream-partition-with-get-rest-fun (stream get-rest-fun)
+ "Call `stream-divide-with-get-rest-fun' on stream ad finitum.
+The return value is a (not necessarily finite) stream S of
+streams S_i where (stream-concatenate S) is equivalent to STREAM,
+
+ (S_1 R_1) := (stream-divide-with-get-rest-fun STREAM get-rest-fun)
+
+and
+
+ (S_i+1 R_i+1) := (stream-divide-with-get-rest-fun R_i get-rest-fun)
+
+as long as R_i is not empty."
+ (stream-make
+ (if (stream-empty-p stream) nil
+ (let ((divided (stream-divide-with-get-rest-fun stream get-rest-fun)))
+ (cons (car divided)
+ (stream-partition-with-get-rest-fun (cadr divided) get-rest-fun))))))
+
+
+(provide 'stream-x)
+
+;;; stream-x.el ends here
--
2.11.0
next prev parent reply other threads:[~2017-04-21 2:34 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-02 15:42 [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations Michael Heerdegen
2016-06-02 15:50 ` Michael Heerdegen
2016-06-02 19:33 ` Nicolas Petton
2016-06-02 19:44 ` Michael Heerdegen
2016-06-08 19:52 ` Michael Heerdegen
2016-06-09 11:58 ` Nicolas Petton
2016-06-09 15:06 ` Michael Heerdegen
2016-06-09 15:46 ` Nicolas Petton
2016-06-09 16:01 ` Davis Herring
2016-06-09 16:24 ` Michael Heerdegen
2016-06-09 17:11 ` Yuri Khan
2016-06-09 19:41 ` Michael Heerdegen
2016-06-09 21:06 ` Yuri Khan
2016-06-10 15:57 ` Michael Heerdegen
2016-06-10 16:13 ` Yuri Khan
2016-06-10 19:37 ` Michael Heerdegen
2016-09-16 23:52 ` Michael Heerdegen
2016-09-17 6:22 ` Yuri Khan
2016-09-25 15:38 ` Michael Heerdegen
2016-09-25 18:41 ` Yuri Khan
2016-09-28 1:07 ` Michael Heerdegen
2016-09-28 4:13 ` Yuri Khan
2016-09-28 8:50 ` Nicolas Petton
2016-09-28 18:27 ` Michael Heerdegen
2016-09-28 19:19 ` Yuri Khan
2017-03-02 2:36 ` Michael Heerdegen
2017-03-02 5:00 ` Michael Heerdegen
2017-03-02 12:58 ` Nicolas Petton
2017-03-02 12:55 ` Nicolas Petton
2017-03-02 22:38 ` Michael Heerdegen
2017-03-15 14:42 ` Michael Heerdegen
2017-03-21 11:37 ` Nicolas Petton
2017-03-22 17:09 ` Michael Heerdegen
2017-04-21 2:34 ` Michael Heerdegen [this message]
2017-04-22 20:34 ` Nicolas Petton
2017-04-23 5:08 ` Michael Heerdegen
2017-03-20 11:29 ` Nicolas Petton
2016-09-25 20:49 ` John Wiegley
2016-06-12 8:34 ` Markus Triska
2016-06-12 14:07 ` Michael Heerdegen
2016-06-12 14:31 ` Nicolas Petton
2016-06-12 22:28 ` Markus Triska
2016-06-11 1:34 ` Michael Heerdegen
2016-07-06 23:20 ` Michael Heerdegen
2016-08-01 21:13 ` Michael Heerdegen
2016-08-01 22:05 ` Nicolas Petton
2016-08-02 0:39 ` Michael Heerdegen
2016-06-09 15:48 ` Nicolas Petton
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=87pog6lc98.fsf@drachen \
--to=michael_heerdegen@web.de \
--cc=emacs-devel@gnu.org \
--cc=nicolas@petton.fr \
/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).