[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations
From: |
Michael Heerdegen |
Subject: |
Re: [PATCH] Gnu Elpa: stream.el: Add some more basic stream operations |
Date: |
Thu, 02 Mar 2017 23:38:24 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) |
Nicolas Petton <address@hidden> writes:
> > And this is how that looks. WDYT?
> >
> > (defun stream-divide-with-get-rest-fun (stream get-rest-fun)
>
> Is this function public on purpose?
>
> > (defun stream-partition-with-get-rest-fun (stream get-rest-fun)
>
> Same question :)
Yes, it's public on purpose. They have ugly names (maybe we find better
ones?), but they are useful per se as the more general tool. I think
they are also quite nice to use, because the creation of streams is
factored out, and you can pass the essence of the calculation as a
function argument.
And `stream-divide' and `stream-partition' no doubt don't cover all use
cases one can think of. Looking at two subsequent elements at the same
time as in `stream-divide' gives you flexibility, but the first part of
`stream-divide' will always have at least one element. This will not
fit in some cases.
Here is another use case where I think `stream-divide-with-get-rest-fun'
suits better: I want to have the last 3 Fibonacci number less than 1000.
This is how I would do it:
#+begin_src emacs-lisp
;; -*- lexical-binding: t -*-
(let ((count 3) fibs)
(setq fibs (stream-make (cons 1 (stream-scan #'+ 1 fibs))))
(seq-into-sequence
(seq-take
(cadr
(stream-divide-with-get-rest-fun
fibs
(lambda (stream)
(let ((rest stream))
(dotimes (_ count) (stream-pop stream))
(while (< (stream-pop stream) 1000)
(stream-pop rest))
rest))))
count)))
#+end_src
You could also do the same with `stream-divide', but it would rather
look more complicated.
Michael.