« Return to Thread: Groovy syntax /API enhancement

Re: Groovy syntax /API enhancement

by Jochen Theodorou :: Rate this Message:

Reply to Author | View in Thread

Mingfai schrieb:

> thx BarZ and Jochen for your comments.
>
> I made those suggestions completely from the users (API users) point of
> view without considering how (difficult) to implement or how optimize is
> the performance. I didn't make up those suggestions from my mind but
> from my Groovy programming code, I feel if Groovy supports those syntax,
> my code could be cleaner.
>
> for 1..3, they certainly can be done in another way but it's not as
> simple/elegant as my suggested way.
>
>     * It doesn't look like a big issue if it's just "!(x in ['x'])", but
>       when the line is longer and more complex and when use together
>       with other parenthesis, skipping one pair of parenthesis means a
>       lot in code clarity. It's just like in SQL, we can add a NOT to
>       NOT LIKE or NOT IN.

sure, that is true, you have the same for example for instanceof. But
"!in" looks irritating to me, "not in" would be much better, but I am
not too sure we can put that in our grammar.

>     * find(x) vs sublist, they are different. say i have a 10,000 item
>       list, my find return 5,000,  and I want to get the first 100. if i
>       use sublist, the code is longer, as well as I need to construct a
>       list of 5000, and then sublist to a list of 100.

ok, there is a misunderstanding... you don't want to iterate the whole
thing and break after for example 100 matches where found. normally this
is something for "break", which does not work in Groovy 1.x.. hmm

>     * [:] << [k,'v'] , i actually suspect it is possible for the
>       compiler to optimize it than create a new map then join the map.

this kind of optimizations are quite troublesome in a dynamic language.
Especially if an operator like << can change its meaning during runtime

> for 4..5, I did assume the collection is in sequence and is iteratable.
> It seems to me Groovy collections are usually in sequence by default.
> e.g. [] is ArrayList and [:].getClass() is LinkedHashMap, so in the
> majority of case, collection have order and have index. e.g. [] as Set
> is used, then the index will just be unavailable or always return -1.
>
> using it.next() is just a stupid idea. The key point is there should be
> an issue way to get a sibling. Say, if i want to check if a list has
> duplicated value, I may use:
>   boolean isDuplicated = mylist.sort().any{ it ==
> *mylist[mylist.indexOf(it)+1]*   }
>
> When I wrote the above line, I think Groovy is suppose to provide a more
> intelligent way such as :
>   boolean isDuplicated = mylist.sort().any{ it == *nextValue()*  }
> (as Groovy is much more intelligent than Java,  I got an expectation
> that it has to be extremely intelligent! :-) )

Well... there are some problems with your example... first, sort will
modify the list itself, next, you could do:

isDuplicated = mylist.size()==mylist.unique().size()

to realize your idea, Groovy would have to give some kind of Iterator
object and there lies a basic problem, because we use the normal
Iterator provided by the list. There is no peeking. If you compare to
the previous element, then you can use inject:

mylist.inject(null) { value, item -> isDuplicated |= item==value; item }

of course this solution has several downsides.... isDuplicated is set
inside the closure, which means you see the place it is set not very
good. And an additional the initial element given to inject has to be
something that is not in the list, which may require an additional
line... hmm... there might be another way too:

isDuplicated = mylist.inject([false,null]) { value, item ->
  [ value[0] |= item==value[1], item ]
}[0]

well, of course that is not as nice as it could be with some kind of
iterator that allows direct access... and of course this solution is a
bit different too ;)

> My example was not meant to explore how to check duplicated value, but
> there are many cases that we would want to do collection closure
> activity that involves siblings.

can you give more examples?

> I also wonder the compiler could
> optimize the performance as it knows I'm calling nextValue(),

you should forget about the compiler knowing something and improving
performance. This isn't a macro and it is no static language

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

    http://xircles.codehaus.org/manage_email


 « Return to Thread: Groovy syntax /API enhancement