On Tuesday 07 May 2002 02:47 am, Donal K. Fellows wrote:
> "hello".each(x) {
> print(x)
> }
>
> Problem with this is probably that the first x looks like an argument
> to each(); I suppose it is, but not in quite the normal sense in that
> it is a sort-of output-argument. If you have local function closures
> you should probably use those, especially if they are introspectable
> in the number of arguments[*]. OTOH, I don't know enough about the
> syntax of your language to know how well that'd work (Java does this
> sort of thing, except with classes not functions, and it produces code
> that doesn't win awards for extreme syntactic elegance.) :^/
>
Hm... originally, I had something that looked like this:
iterator_function() (block_arg,...) { block_body}
That turned out to be rather hard to debug, IMO.
I also considered using brackets instead of braces to differentiate between
normal blocks and the anonymous iterator blocks, but then had the obvious
problem of:
iterator_function()[|x| block]
versus
function_returning_an_array[index]
The parser is relatively simple (and perhaps that is part of my problem). It
simply looks for the closing paren of a method's args, and parses everything
up to a closing 'end' or right brace as an anonymous block.
> If you like 3, go for that. Your tastes are as good a metric here as
> any other I can think of, and better than many.
>
Thanks... I think.
> Donal.
> [* Just one value at a time from the iteratable entity? Nah. But then
> I'd go for iterating over more than one iteratable object at a time too.
> ]
Some iterators return multiple values, if that's what you mean. It would be
rather trivial, for example, to create a String method that returned an
arbitrary number of chars per iteration:
"hello".each(3) { |x| print ("chars=%s", x)
=> hel
=> lo
But the iteration over multiple values would be a big tougher to intergate,
although I did look at a top-level iterator function:
iterate(a,b,c,d).each() { block}
that would call the each method of each object in the chain. However, I
couldn't think of a good use of that to save my life, so removed it.