[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFC: Generators v2
From: |
Stephen J. Turnbull |
Subject: |
Re: RFC: Generators v2 |
Date: |
Sun, 25 Aug 2013 22:56:24 +0900 |
Nic Ferrier writes:
> I'm personally uncomfortable about claiming `next'. At least use a
> better name, like `gen-next'?
Another possible name is `pop', I think. `pop' being a macro anyway
it should be able to handle Yet Another Type of argument. `pop',
because when applied to arguments, a generator returns an iterator
whose API is like a list restricted to being consumed by iteration. I
like `next', though. `next' is used in Python, at least, and it seems
like the natural name to use. `gen-next'? No, thank you.
> Perhaps it would be possible to avoid grabbing next by making it an
> argument to the object returned by yield:
>
> (defgenerator y (x) [...])
>
> (let ((g (y 10)))
> (funcall g :next)
> (funcall g :next)
> (funcall g :next)) => 7
I think the package is misusing the name "generator". If `g' is a
function (which it seems it indeed is in this package), it's already
possible to create closures in various ways, so that a function
carries its state with it. So this is just syntactic sugar for
closures.
Indeed this syntactic sugar is *very* useful for coroutines and the
like, but it's not really useful for generators. The point of a
generator is that it returns an iterable, ie, an object which looks
like a sequence in an iteration context. In particular, code that can
iterator over the value of a generator should be able to use an
ordinary list in the same place, *without* knowing which it's going to
get in advance. I don't see how you make that work generically if
generators return functions. You have to alter *every* iteration
construct to recognize generators. And then what happens if you hand
it an ordinary function? Is it possible to distinguish between a
function created with defun, and the value of a generator created with
defgenerator? I don't see how (without introspecting the code for
yields).
> The only thing nicer than that would be to have generators be real
> lisp-1 functions:
Too bad that Emacs Lisp is a Lisp-2, I guess.
You actually could do that Lisp-2-fully in a specific syntactic
context, though:
; pass the syntactic sugar, plz
; oh, yeah, the gencinnamon, too! then you can omit the fmakunbound
(prog2
(define-function 'g (y (10)))
(g)
(fmakunbound 'g))
=> 9
It should be easy enough to do that with a macro, though I don't see
how to make it very general.
Steve