From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations Date: Wed, 08 Jun 2016 21:52:27 +0200 Message-ID: <8737onlapw.fsf@web.de> References: <87twhbmwbx.fsf@web.de> <878tynl720.fsf@petton.fr> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1465415609 18796 80.91.229.3 (8 Jun 2016 19:53:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 8 Jun 2016 19:53:29 +0000 (UTC) Cc: emacs-devel@gnu.org To: Nicolas Petton Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Jun 08 21:53:20 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1bAjXS-0001Qn-Sg for ged-emacs-devel@m.gmane.org; Wed, 08 Jun 2016 21:53:19 +0200 Original-Received: from localhost ([::1]:59215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bAjXS-0005Zh-9v for ged-emacs-devel@m.gmane.org; Wed, 08 Jun 2016 15:53:18 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58782) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bAjWn-0005YQ-ND for emacs-devel@gnu.org; Wed, 08 Jun 2016 15:52:38 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bAjWj-0004Tt-I8 for emacs-devel@gnu.org; Wed, 08 Jun 2016 15:52:36 -0400 Original-Received: from mout.web.de ([212.227.15.14]:63150) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bAjWj-0004TK-3F for emacs-devel@gnu.org; Wed, 08 Jun 2016 15:52:33 -0400 Original-Received: from drachen.dragon ([94.217.122.112]) by smtp.web.de (mrweb002) with ESMTPSA (Nemesis) id 0Lud96-1bbAMO1NhY-00zrm0; Wed, 08 Jun 2016 21:52:28 +0200 In-Reply-To: <878tynl720.fsf@petton.fr> (Nicolas Petton's message of "Thu, 02 Jun 2016 21:33:27 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.94 (gnu/linux) X-Provags-ID: V03:K0:Z6D0vFA5r8eBHAf1bA5ZfA2tRUiFg/ZT+j2p4tXhjaErnACsPKM VziNPolurKvoIKRNUVSVTdGxdumW2vyOzqhEMP3Y1+Je4qU0oLP+nwgayoxCWbFc1Wci36M RWDcqfEeVYk4Z3ULasV7DtCWh1QQtAsG0WtUI/JeCrUrm0lgb3Me/ux0FIw44EkOCX1IR2X 0JnAyOoo6lE4zJ7yojDbQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:Ka9MA5238Fo=:CEJfB5iXRPGQuktij3aUYQ z5EJnbtL6GuC9bnSp26OwwFg5NSNW7cl9dTMCJIIId2CAe2l3AY9jKEFTcUrmJNzZ7llshvKh PChiZuVsi7BxrmlL7RJXlSjzh3fGmbUmg7GKne9IFgEkNUW0/G5unw2YjTcVslAI7gB2y2/Uu HZLyA1I3pA1783rQEg4xSh58Bee8J/UsTwyvT7PwIE9Ha0jiXEwSIR+hPvCgM/p0L0GiThQc8 zPDC1BG2h91zDGveHaANeyTJODwgWF8pLYYqMjvTYHUrMSUqEWsB6L8DxwtWz2kELz3ayUK8+ BvM7bf/ZfREvVrk4Y1B5ZnwfO57x2LwxtV9Aq54nw0ZjzDudZlMxzKgbrxRTUvEcXFmsTt6tX 1klWW3TsfQN3bnUPxo7MM24CkdDV175DgydGeBbKFFONJFCsz15AiLBLpv+FtfQFRl7xWI/aM HnzG4Hu3pP7P0AUuwfW7GIOyZxKLAJlCs2rQDWdPANugPWguKRnkn6j7pnIjw0MnZ5Y1bYY9o JknKBnn3Zrq9rOfESOHlNGJzUaC47VUDvbCDYuVEqOQ0U4tbwAICZOw4aCXUSnhRuDTJg3l// OYvWsFYiSF7lxm1/3mD7y7bbrEtBWIAJ359oL1j6cffXFNwhoyve7ixWCefTy/2rDlxKDPvWS PDmMS6E5lS2aOm0ebL1WNWP/utpOv09sc/zA4Q/dLtyp7QhQMvnabg98hwsyLTtXq2nm9mL2F 0sVVgHjzC7SfBGa4rVgaLk4XlDtfWfRUvtMVsbFUuXhT3EiV2UtzvwpMxMKpm2+TPi755LH4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.15.14 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:204240 Archived-At: --=-=-= Content-Type: text/plain Nicolas Petton writes: > > +(defun stream-scan (function init stream) > > + "Return a stream of successive reduced values for STREAM. > > Why not using `seq-reduce'? > > > +(defun stream-mapconcat (function stream separator) > > Would `seq-mapconcat' with a stream-specific implementation make sense? I updated the patch. `stream-scan' is useful and not redundant. `stream-reduce' was indeed redundant, as the generic `seq-reduce' already works for streams, so I removed it. And for now, I excluded `stream-mapconcat' from the patch. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Add-some-more-basic-stream-operations.patch >From 8b3326ff7251ba402002dfcbb9272f59547f09ab Mon Sep 17 00:00:00 2001 From: Michael Heerdegen Date: Thu, 2 Jun 2016 02:42:43 +0200 Subject: [PATCH] Add some more basic stream operations --- packages/stream/stream.el | 45 +++++++++++++++++++++++++++++++++++ packages/stream/tests/stream-tests.el | 23 ++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/packages/stream/stream.el b/packages/stream/stream.el index 22cecac..62eb3b6 100644 --- a/packages/stream/stream.el +++ b/packages/stream/stream.el @@ -333,6 +333,51 @@ calling this function." (cl-defmethod seq-copy ((stream stream)) "Return a shallow copy of STREAM." (stream-delay stream)) + + +;;; More stream operations + +(defun stream-scan (function init stream) + "Return a stream of successive reduced values for STREAM. + +If the elements of a stream s are s_1, s_2, ..., the elements +S_1, S_2, ... of the stream returned by \(stream-scan f init s\) +are defined recursively by + + S_1 = init + S_(n+1) = (funcall f S_n s_n) + +as long as s_n exists. + +Example: + + (stream-scan #'* 1 (stream-range 1)) + +returns a stream of the factorials." + (let ((res init)) + (stream-cons + res + (seq-map (lambda (el) (setq res (funcall function res el))) + stream)))) + +(defun stream-flush (stream) + "Request all elements from STREAM in order for side effects only." + (while (not (stream-empty-p stream)) + (cl-callf stream-rest stream))) + +(defun stream-iterate-function (function value) + "Return a stream of repeated applications of FUNCTION to VALUE. +The returned stream starts with VALUE. Any successive element +will be found by calling FUNCTION on the preceding element." + (stream-cons + value + (stream-iterate-function function (funcall function value)))) + +(defun stream-concatenate (stream-of-streams) + "Concatenate all streams in STREAM-OF-STREAMS and return the result. +All elements in STREAM-OF-STREAMS must be streams. The result is +a stream." + (seq-reduce #'stream-append stream-of-streams (stream-empty))) (defun stream-of-directory-files-1 (directory &optional nosort recurse follow-links) "Helper for `stream-of-directory-files'." diff --git a/packages/stream/tests/stream-tests.el b/packages/stream/tests/stream-tests.el index 23a54b5..16b5756 100644 --- a/packages/stream/tests/stream-tests.el +++ b/packages/stream/tests/stream-tests.el @@ -242,5 +242,28 @@ (should (= 2 (stream-first str))) (should (null (stream-pop stream-empty))))) +(ert-deftest stream-scan-test () + (should (eq (seq-elt (stream-scan #'* 1 (stream-range 1)) 4) 24))) + +(ert-deftest stream-flush-test () + (should (let* ((times 0) + (count (lambda () (cl-incf times)))) + (letrec ((make-test-stream (lambda () (stream-cons (progn (funcall count) nil) + (funcall make-test-stream))))) + (stream-flush (seq-take (funcall make-test-stream) 5)) + (eq times 5))))) + +(ert-deftest stream-iterate-function-test () + (should (equal (list 0 1 2) (seq-into-sequence (seq-take (stream-iterate-function #'1+ 0) 3))))) + +(ert-deftest stream-concatenate-test () + (should (equal (seq-into-sequence + (stream-concatenate + (stream (list (stream (list 1 2 3)) + (stream (list)) + (stream (list 4)) + (stream (list 5 6 7 8 9)))))) + (list 1 2 3 4 5 6 7 8 9)))) + (provide 'stream-tests) ;;; stream-tests.el ends here -- 2.8.1 --=-=-= Content-Type: text/plain Regards, Michael. --=-=-=--