Without lambdas, you're a bit limited here. But with them, I've found
scala's approach to work well.
for(x <- xs) { println(x) }
is just syntactic sugar for
xs foreach { x => println(x) }
and
for(x <- xs) yield { x.toUpperCase }
is
xs map { x => x.toUpperCase }
*anything* with the appropriate map/flatMap/filter/foreach method(s) on can
be used in a for-comprehension.
(which is why scala doesn't call it a "for loop", because it really isn't)
On 31 July 2012 13:31, Dale Wijnand <[email protected]> wrote:
> I would say you could create delegating iterables/iterators for those
> types. What would be an alternative would you have preferred?
>
> Dale
>
> On 31 July 2012 14:17, Kevin Wright <[email protected]> wrote:
>
>> Yes/No. You're still forced to only use it with things that can be
>> Iterables, yet there's a whole category of stuff where foreach makes sense,
>> but can't be represented in this manner.
>>
>> One of the more obvious examples here is something like a stream of lines
>> coming over a network socket, in which you want the body of the foreach
>> expression to be executed asynchronously for each incoming line (perhaps by
>> dispatching to a thread pool), and for the expression as a whole to be
>> non-blocking.
>>
>>
>> On 31 July 2012 08:15, Roland Tepp <[email protected]> wrote:
>>
>>> Sorry, couldn't resist, but let your class implement Iterable and voila
>>> - the foreach is extended!
>>>
>>> esmaspäev, 30. juuli 2012 15:55.30 UTC+3 kirjutas Ricky Clarkson:
>>>
>>>> 6. foreach is not open for extension, i.e., it only works with
>>>> Iterables and arrays.
>>>>
>>>
>>
>>
--
You received this message because you are subscribed to the Google Groups "Java
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.