From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations Date: Thu, 02 Mar 2017 03:36:09 +0100 Message-ID: <87shmw76cm.fsf@drachen> References: <87twhbmwbx.fsf@web.de> <878tynl720.fsf@petton.fr> <8737onlapw.fsf@web.de> <87ziqu7ew9.fsf@petton.fr> <87mvmuxuyo.fsf@web.de> <8760tixi99.fsf@web.de> <87twh1kpem.fsf@web.de> <87y42r4d3a.fsf@web.de> <878tug9eh3.fsf@web.de> <87a8es6dcq.fsf@web.de> <87bmz7517d.fsf@web.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1488422291 16005 195.159.176.226 (2 Mar 2017 02:38:11 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 2 Mar 2017 02:38:11 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) Cc: Nicolas Petton , Emacs developers To: Yuri Khan Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Mar 02 03:38:06 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cjGd0-0003EK-8u for ged-emacs-devel@m.gmane.org; Thu, 02 Mar 2017 03:38:02 +0100 Original-Received: from localhost ([::1]:49838 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cjGd5-0005aY-Me for ged-emacs-devel@m.gmane.org; Wed, 01 Mar 2017 21:38:07 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46729) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cjGbR-0004RZ-Vz for emacs-devel@gnu.org; Wed, 01 Mar 2017 21:36:26 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cjGbM-0003Qa-Vy for emacs-devel@gnu.org; Wed, 01 Mar 2017 21:36:26 -0500 Original-Received: from mout.web.de ([212.227.15.3]:64211) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cjGbM-0003Pb-LO for emacs-devel@gnu.org; Wed, 01 Mar 2017 21:36:20 -0500 Original-Received: from drachen.dragon ([109.44.3.195]) by smtp.web.de (mrweb003 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LwHmQ-1cF0bd4BjC-0182CO; Thu, 02 Mar 2017 03:36:12 +0100 In-Reply-To: (Yuri Khan's message of "Thu, 29 Sep 2016 01:19:04 +0600") X-Provags-ID: V03:K0:Zohs63c3EKo3t5CS9uO289iCpfvnFJDAlPJvfx85dgmWTE95pwX l/8c8IJtTkgYQsuQQty23Ee2o8X9hyT9Wk+ncErBa5QKBnsR7lmsEnKCHq8csNsAYuEjwY/ OjvbqJ0XQEevKg7+0a+tDz56emTSVu/II/wTueIcgqG50eTsXkj5zegTKm3Nvj4iVnKC6sJ U/osMz0YtQO/pZ2Mh/vkg== X-UI-Out-Filterresults: notjunk:1;V01:K0:0wEidqsF8qo=:aBSKsKCSo+wFeplKTEH0Ja xKtsteilwl7jfZ75oFGSucXHEiDJFuo/rnr+37/OlwTqqjVchWSL5SIqhS71PluvwyN2apCHJ gnpPQDfXi6yXlV7IRmfpuw+UHbmGiqyfdBxNkLrwJ7rhaunpfQkQrqge1OEua5iWrjNNLb8v3 tnuE7+m7edy0HuV2P1gb+cIotCelV8noj/3WJ5B2+sMPoMatEdqmskkh/4pTVpMJLHQl+o4lo wLFV1I0tvIG22N3avhCCsIWoqKwMzCJXHg/dqwnXHbDkQykHRWxNHPnnGihasga0R4RpOzEtw yVmgC4bPXNnw/Cuk9VmdKGAs6pi4G9tSfzgOd5bqfLOIgGpbUaqxzxLUPRnum95wjfuyPbPVu zNyPXDuU88Q7Ld/UNIc91gZw3yH6ZbGr5+AtQcyClaENxD+pMLD+p2wdOGH7cp5VMxkVraNeC lL3n1UJxWTXsYYKlQ23ICF+So+iNGzWupDfByGhHPVfehZZHtmh1DNNu6VZBVubMtWI2E/IIt yfNI67T4CmEEqsmYAwKdtsC0ThlESOSXdt6+mBHP6ccgY1EoJJkIEoegnUNc3mpB6uvHZWUij 8Mq0pvaP6X8SbMAbmeHnOAI0KVv+Zqy2/ghlJV/QlhTd9Oe/T39krw6wvU8EfcIDYAW0jEDi9 eM//txfZZZljXFJAVBlrOfbQtl1z2g92xMkXoK9VWEE+/QbWRPK/6FCEl80yLDG3p+YeksS4Z Wui1E9N+sJuPi1xdx0KSo+tzQLEN2p5vcJE7JVJahT1YPSrU149aqOiwGdg= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.15.3 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:212687 Archived-At: --=-=-= Content-Type: text/plain Hi everybody! I want to revive this discussion. I needed something like this for el-search, so I gave it a second thought. > >> As an example, if you want a subsequence starting at the first odd > >> number or at index 1000, whichever is earlier, (seq-drop (min 1000 > >> (seq-position-if 'odd S)) S) will not do the right thing. > > > > Would adding an optional argument that allows to specify an upper limit > > for the indexes the function looks at cover (all of) your concerns? > > That was just an example. Real users will have other needs. If given a > primitive that returns an index of something, they will try solving > their problems in terms of that index. It is an interface that is easy > to use incorrectly. > > But please do not let my theoretical concerns stop you from > implementing useful things. A working implementation is the surest way > of gaining collective experience about a tool. I tried to cope with our problems by using abstraction and higher order functions (so far, only implemented for streams - see below). We talked about pairs of function (take-until, drop-until), and a problem was that the semantics were always a bit vague or hard to define (is a border element in or out?, etc). A difficulty with the discussed concepts was that it was not easily possible to step back to cut a stream before a certain element when we already had skipped that element etc. That brought me to the following idea: The underlying basic "operation" is to divide a stream into two parts. Such a division is non-ambiguously described by specifying the second part, the rest-stream. Any function dividing a stream into two parts can be described by another function F stream -> rest, that takes a stream, and returns a rest. Such functions F are the input of my low-level functions. A subsequent application of divide operations leads to a partition of a stream into a stream of substreams, which I also implemented. I also implemented two more higher-level functions for stream dividing/partitioning that hopefully should cover most of the use cases of seq-drop-while et alter. For the cases that should not be covered, you can use the lower level functions with an appropriate function F as described above. Sounds complicated, but it's just a sum of trivial pieces. And this is how that looks. WDYT? --=-=-= Content-Type: application/emacs-lisp Content-Disposition: inline; filename=stream-divide.el Content-Transfer-Encoding: quoted-printable ;; -*- lexical-binding: t -*- (defun stream-substream-before (stream rest) "Return a stream of the elements of STREAM before REST. REST must either be `eq' to STREAM or to one of the subsequent calls of `stream-rest' on STREAM." (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) (let ((rest (funcall get-rest-fun stream))) (list (stream-substream-before stream rest) rest))) (defun stream-divide (stream pred) "Divide STREAM between the first ELT, NEXT element pair for that PRED ret= urns nil. The return value is a list of two streams. If no such pair of elements exists, return (STREAM E) where E is an empty stream. 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)))) =3D=3D> ((1 2 3) (5 6 7 9 10 11 23))" (stream-divide-with-get-rest-fun stream (stream-divide--get-rest-fun pred= ))) (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 pred) "Partition STREAM into bunches where PRED returns non-nil for subsequent = elements. The return value is a stream of streams (so this function is a reverse of `stream-concatenate'). Often, but not necessarily, PRED 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))))) =3D=3D> ((1 2 3) (5 6 7) (9 10) (15) (23))" (stream-partition-with-get-rest-fun stream (stream-divide--get-rest-fun p= red))) (defun stream-partition-with-get-rest-fun (stream get-rest-fun) (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-fu= n)))))) --=-=-= Content-Type: text/plain Regards, Michael. --=-=-=--