« Return to Thread: Groovy syntax /API enhancement

Re: Groovy syntax /API enhancement

by Barzilai Spinak-2 :: Rate this Message:

Reply to Author | View in Thread

Mingfai wrote:

> [...]
> support everyWithIndex, anyWithIndex, collectWithIndex, sumWithIndex
> etc. it's even better if the original every, any has an implicit
> variable/syntax for retrieving the index
>
>    1. e.g. [10,20].collectWithIndex{ it,i -> it*(i+1) } = [ 10, 40 ]
>
>       I actually prefer all methods for collections to support the
>       index parameter by default, without a need to use a different
>       WithIndex method.
>
The problem here is that you are assuming that those methods (every,
any, collect, etc) are *iterating* over the collection with some
predetermined order, in sequence. Nothing in the semantics says it
should be so. They could be even be doing it in parallel.

>    1. In any collection closure, e.g. each, every, any etc., support
>       it.next() and it.prev()/previous() and it.sibing(int) to
>       retrieve the next item, return null if not existed (but no
>       nullpointerexception)
>       e.g. [1,2,3,2].sort().find{ it == it.silbling(-1) } == 2
>
>
I have no idea how you think this could be implemented. You are calling
a method (sibling) on the contained element (which could "belong" to
many different collections and have references from many places in the
code). The method should be called on the container, or delegate of the
closure... but I think it's too much trouble.


BarZ

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: Groovy syntax /API enhancement