unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Generator Examples
@ 2016-09-22  0:47 raman
  2016-09-25  0:16 ` Michael Heerdegen
  0 siblings, 1 reply; 7+ messages in thread
From: raman @ 2016-09-22  0:47 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: message body text --]
[-- Type: text/plain, Size: 380 bytes --]

I'm attaching a short texinfo file containing a couple of examples of
using generators in Emacs Lisp.

The texinfo manual contains one example which I found confusing in
parts:

1. The example has  a nested call to iter-yield -- not sure why
2. I still dont quite understand the purpose of the optional argument
to iter-next, an example that leverages  it would be good to have.


[-- Attachment #2: Generator Examples in texinfo --]
[-- Type: application/octet-stream, Size: 1554 bytes --]

\input texinfo    @c -*- texinfo -*-
@c %**start of header
@setfilename ./generators.info
@settitle Generators In Emacs Lisp
@documentencoding UTF-8
@documentlanguage en
@c %**end of header

@finalout
@titlepage
@title Generators In Emacs Lisp
@author raman
@end titlepage

@ifnottex
@node Top
@top Generators In Emacs Lisp
@end ifnottex

@menu
* Define a Fibonacci generator::
* Iterate Over A List:: Iterate Over A List. 
@end menu

@node Define a Fibonacci generator
@chapter Define a Fibonacci generator

@lisp
(iter-defun fibonacci-iter ()
  "Return a Fibonacci sequence generator."
  (let ((a 1)
        (b 1))
    (while t
      (iter-yield a)
      (cl-psetq a b b (+ a b )))))
@end lisp

Instantiate a Fibonacci generator and use it to collect the first 10
numbers in the sequence.

@lisp
(setq f (fibonacci-iter))
;;; The first 10 Fibonacci numbers:
(cl-loop for i from 1 to 10 collect (iter-next f))
@end lisp

@node Iterate Over A List
@chapter Iterate Over A List:

A generator that takes a list and returns an iterator over that list:

@lisp
(iter-defun list-iter (l)
  "Return an iterator that iterates over list `l'."
  (let ((local(copy-sequence l)))
    (while local (iter-yield (pop local)))))
@end lisp


Test the above by iterating over a list. This example is mostly for
illustration  demonstrates the use of @code{cl-loop} clause @code{iter-by}.

@lisp
;;; Create a list iterator:

(setq l-iter (list-iter '(a b c d e)))
;;; Loop  through the iterator, collecting values:
(cl-loop for e iter-by   l-iter collect e)
@end lisp

@bye

[-- Attachment #3: .signature --]
[-- Type: text/plain, Size: 5 bytes --]


-- 

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-09-25  2:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-22  0:47 Generator Examples raman
2016-09-25  0:16 ` Michael Heerdegen
2016-09-25  0:29   ` raman
2016-09-25  1:03     ` Michael Heerdegen
2016-09-25  2:26       ` T.V Raman
2016-09-25  0:32   ` raman
2016-09-25  1:06     ` Michael Heerdegen

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).