From: raman@google.com
To: emacs-devel@gnu.org
Subject: Generator Examples
Date: Wed, 21 Sep 2016 17:47:58 -0700 [thread overview]
Message-ID: <22499.10814.549580.139539@gargle.gargle.HOWL> (raw)
[-- 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 --]
--
next reply other threads:[~2016-09-22 0:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-22 0:47 raman [this message]
2016-09-25 0:16 ` Generator Examples 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
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=22499.10814.549580.139539@gargle.gargle.HOWL \
--to=raman@google.com \
--cc=emacs-devel@gnu.org \
/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).