foreach(r; requests) { r.concider(); }
Gor Gyolchanyan
gor.f.gyolchanyan at gmail.com
Fri Nov 4 07:38:11 PDT 2011
Actually, i didn't think of it before. Good point.
This is not the most efficient way to iterate over a range in a custom way.
On Fri, Nov 4, 2011 at 6:34 PM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> On Fri, 04 Nov 2011 09:01:11 -0400, Christophe
> <travert at phare.normalesup.org> wrote:
>
>> "Steven Schveighoffer" , dans le message (digitalmars.D:146563), a
>> écrit :
>>
>> The foreach delegate syntax already allow you to have parametrized
>> iteration over a structure !
>>
>> Actually, the delegate do not have to be returned by the & operator, it
>> can be returned by a function. It's a bit awkward first, but then it's
>> easy to use:
>>
>> struct Iterable
>> {
>> int delegate(int delegate(ref int)) inReverse()
>> {
>> return ()
>> {
>> int result = 0;
>> foreach(int i; 0..100)
>> {
>> auto t = 99-i;
>> if((result = dg(t)) != 0) break;
>> }
>> return result;
>> }
>> }
>>
>> int delegate(int delegate(ref int)) byStep(int step)
>> {
>> return()
>> {
>> int result = 0;
>> foreach(int i; iota(0, 100, step))
>> {
>> auto t = i;
>> if((result = dg(t)) != 0) break;
>> }
>> return result;
>> }
>> }
>> }
>>
>>
>> int main()
>> {
>> Iterable it;
>> foreach (i; it.inReverse) writeln(i);
>> foreach (i; it.byStep(2)) writeln(i);
>> }
>>
>>
>> There is no need to add a special syntax to do what you want !
>>
>> Maybe there should be a bit more documentation about this.
>
> I'll point out first that this solution allocates a closure on the heap --
> not necessarily the most efficient solution.
>
> Second of all, this seems like a *lot* of trouble to go through just to call
> a function (yes, that's all you're doing). The delegate returned is called
> *once* and then thrown away.
>
> I'd much rather the language just support the most straightforward solution
> (calling a function with the given parameters).
>
> -Steve
>
More information about the Digitalmars-d
mailing list