Python, Rust, Swift, C++, ObjC and more all support the equivalent of a foreach loop today. That perfectly shows intent
Python, Rust, Swift, C++, ObjC and more all support the equivalent of a foreach loop today. That perfectly shows intent
In XQuery, the root type is a flattened sequence. Flattened means not just that [1, [2, 3]] is flattened to [1, 2, 3], but also the a single object 42 is equivalent to a sequence of one object [42].
This means that if I write:
declare function local:prefixName($name)
{
fn:concat("prefix-", $name)
}
I can invoke it with a single name or a sequence of names without any explicit iteration.It has drawbacks but using XQuery certainly highlights how much code we normally waste dealing with "things" and "sequences of things" separately.
(Than again working with XQuery also highlights how much code one can waste dealing with XML namespaces :)
As a PS, I think the parent comment was referring to a (corresponding) "for apple in apples: peel(apple)" syntax though. The main point was that you don't have to bother with the manual index iteration as in my worst case example.
let mut mixer = Mixer::empty();
apples.for_each(|apple| {
if apple.is_fresh() {
mixer.add(apple.peel());
}
});
let mixer = apples
.filter(Apple::is_fresh)
.map(Apple::peel)
.fold(Mixer::empty(), Mixer::add);Even with loop-over-iterable semantics, explicit loops obscure intent compared to comprehensions and map/reduce/filter/etc.; they are better than C-style loops which in turn are better than conditionals + gotos, but that doesn't make them good at expressing intent in many common cases.