Enumeration adapters in SE 8
Stephen Colebourne
scolebourne at joda.org
Thu Aug 25 00:38:16 PDT 2011
I would note that anytime I design an API method that takes a varargs,
I always consider writing a parallel method taking an Iterable. This
is almost always cut and paste code, and definitely naff.
Stephen
On 25 August 2011 01:50, Andrew Thompson
<lordpixel+core-libs-dev at mac.com> wrote:
> The key advance would be the last one, unfortunately the one Dan says may not make it: allowing the for loop to take an Iterator.
>
> I had some legacy code in the last couple of weeks where I wanted to be able to process arbitrary sequences: arrays, iterables and iterators and I ended up copying and pasting control flow code because there's no unifying interface.
>
> Enumerations are nice to have. Although I can use the for loop with an array, there's no way to write a generic method to handle arrays and Iterables. Arrays that implement Iterable would be more valuable if we're talking convenience.
>
>
>
> On Aug 24, 2011, at 6:13 PM, Collin Fagan <collin.fagan at gmail.com> wrote:
>
>> There was a time where it was considered best practice to return an iterator
>> instead of a collection. There are now many apis and legacy code that are
>> not compatible with the for each loop.
>>
>> I think even if there are only a few places in the jdk where this would
>> apply it would still be convenient in the wild.
>> Collin
>>
>> On Wed, Aug 24, 2011 at 4:58 PM, Colin Decker <cgdecker at gmail.com> wrote:
>>
>>> I've come across various scenarios. One example is
>>> javax.swing.tree.DefaultMutableTreeNode. It has numerous methods that
>>> return
>>> Enumerations, including:
>>> - children
>>> - depthFirstEnumeration
>>> - breadthFirstEnumeration
>>> - preorderEnumeration
>>> - postorderEnumeration
>>> - pathFromAncestorEnumeration
>>>
>>> I've created a utility class with methods to return Iterables for each of
>>> these types of Enumerations for convenience, but it would be much nicer to
>>> just use a method reference.
>>>
>>> Deque's descendingIterator() method is a more modern good candidate for
>>> treating as an Iterable. See this question on StackOverflow:
>>>
>>> http://stackoverflow.com/questions/3883131/idiomatic-way-to-use-for-each-loop-given-an-iterator
>>>
>>> --
>>> Colin
>>>
>>>
>>> On Wed, Aug 24, 2011 at 5:45 PM, Ben Evans <benjamin.john.evans at gmail.com
>>>> wrote:
>>>
>>>> Hi Dan,
>>>>
>>>> On Wed, Aug 24, 2011 at 7:10 PM, Dan Smith <daniel.smith at oracle.com>
>>>> wrote:
>>>>
>>>>> I was pointed to some comments on core-libs about adapting Enumerations
>>>> to
>>>>> for loops in SE 8. (Sorry for the new thread -- I wasn't subscribed.)
>>>> It
>>>>> turns out lambdas + extension methods will make this very easy.
>>>>>
>>>>> In the API:
>>>>>
>>>>> interface Enumeration<E> extends Iterator<E> {
>>>>> boolean hasMoreElements();
>>>>> E nextElement();
>>>>> boolean hasNext() default #hasMoreElements;
>>>>> E next() default #nextElement;
>>>>> void remove() default { throw new UnsupportedOperationException(); }
>>>>> }
>>>>>
>>>>> Note that Iterable is a function type -- a thunk producing an Iterator
>>> --
>>>>> and so we can express Iterables with lambdas and method references.
>>> It's
>>>>> becoming clear that a for loop should provide a target type for these
>>>>> things, so that will probably be part of the SE 8 feature set.
>>>>>
>>>>> With a method reference:
>>>>>
>>>>> Hashtable<String,Object> h = …;
>>>>> for (String s : h#keys) { … }
>>>>>
>>>>
>>>> I'm struggling to see where this might be useful. This example is
>>> obviously
>>>> too trivial and would add nothing, but I can't see to come up with any
>>> more
>>>> extensive example where having a method reference in this position would
>>> be
>>>> actually useful.
>>>>
>>>> Can someone else provide a more compelling example for a for loop taking
>>> a
>>>> method reference?
>>>>
>>>> Thanks,
>>>>
>>>> Ben
>>>>
>>>>
>>>
>>>
>>
>
>
> AndyT (lordpixel - the cat who walks through walls)
> A little bigger on the inside
>
> (see you later space cowboy, you can't take the sky from me)
>
>
>
More information about the lambda-dev
mailing list