November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
> I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.
for many of them it is as simple as:
foreach(index, element; array) { }
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:
> On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
>> I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.
>
> for many of them it is as simple as:
>
> foreach(index, element; array) { }
Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way?
bool[] arr = [ture, false, ture, ...];
int count;
foreach(i;arr){
if(!i)
count++;
}
writeln(count);
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote: > On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote: >> On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote: >>> I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx. >> >> for many of them it is as simple as: >> >> foreach(index, element; array) { } > > Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? > > bool[] arr = [ture, false, ture, ...]; > > int count; > foreach(i;arr){ > > if(!i) > count++; > } > writeln(count); writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b); |
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:
> On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
>> On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:
>>> On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
>>>> [...]
>>>
>>> for many of them it is as simple as:
>>>
>>> foreach(index, element; array) { }
>>
>> Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way?
>>
>> bool[] arr = [ture, false, ture, ...];
>>
>> int count;
>> foreach(i;arr){
>>
>> if(!i)
>> count++;
>> }
>> writeln(count);
>
> writefln("Count is: %s", arr
> .filter!(a => a==true)
> .sum);
>
> // Note: std.algorithm.sum is the same as
> // std.algorithm.reduce!((a,b)=a+b);
Oh, I realise now you were counting the number of 'false' values.
I counted the true values - so the filter line is wrong here.
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:
> On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
>> On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:
>>> On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
>>>> I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.
>>>
>>> for many of them it is as simple as:
>>>
>>> foreach(index, element; array) { }
>>
>> Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way?
>>
>> bool[] arr = [ture, false, ture, ...];
>>
>> int count;
>> foreach(i;arr){
>>
>> if(!i)
>> count++;
>> }
>> writeln(count);
>
> writefln("Count is: %s", arr
> .filter!(a => a==true)
> .sum);
>
> // Note: std.algorithm.sum is the same as
> // std.algorithm.reduce!((a,b)=a+b);
How do I save sum as integer or something I try;
arr.writeln;
return arr.filter!(a=>a==false).sum;
but I get
[true, false, true, false, true, true, true, false, true, false]
0
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:
> On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
>> On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:
>>> On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
>>>> I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.
>>>
>>> for many of them it is as simple as:
>>>
>>> foreach(index, element; array) { }
>>
>> Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way?
>>
>> bool[] arr = [ture, false, ture, ...];
>>
>> int count;
>> foreach(i;arr){
>>
>> if(!i)
>> count++;
>> }
>> writeln(count);
>
> writefln("Count is: %s", arr
> .filter!(a => a==true)
> .sum);
>
> // Note: std.algorithm.sum is the same as
> // std.algorithm.reduce!((a,b)=a+b);
well I tried this that way, but my count stays 0, same as if I do it in an int function with a return though I clearly have some false elements in the arr.
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:
>> writefln("Count is: %s", arr
>> .filter!(a => a==true)
>> .sum);
>>
>> // Note: std.algorithm.sum is the same as
>> // std.algorithm.reduce!((a,b)=a+b);
>
Shouldn't you be using walkLength instead of sum, since you are counting the left over values?
import std.range : walkLength;
writefln("Count is: %s", arr
.filter!(a => a==false)
.walkLength);
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen wrote:
> On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:
>>> writefln("Count is: %s", arr
>>> .filter!(a => a==true)
>>> .sum);
>>>
>>> // Note: std.algorithm.sum is the same as
>>> // std.algorithm.reduce!((a,b)=a+b);
>>
>
> Shouldn't you be using walkLength instead of sum, since you are counting the left over values?
>
> import std.range : walkLength;
> writefln("Count is: %s", arr
> .filter!(a => a==false)
> .walkLength);
That would work also yes.
Be interesting to know which is more efficient actually - I suspect they're very similar.
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | On Tuesday, 3 November 2015 at 16:55:44 UTC, wobbles wrote:
> On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen wrote:
>> On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:
>>>> writefln("Count is: %s", arr
>>>> .filter!(a => a==true)
>>>> .sum);
>>>>
>>>> // Note: std.algorithm.sum is the same as
>>>> // std.algorithm.reduce!((a,b)=a+b);
>>>
>>
>> Shouldn't you be using walkLength instead of sum, since you are counting the left over values?
>>
>> import std.range : walkLength;
>> writefln("Count is: %s", arr
>> .filter!(a => a==false)
>> .walkLength);
>
> That would work also yes.
> Be interesting to know which is more efficient actually - I suspect they're very similar.
false converts to zero, so
[false,false,false].sum == 0
Of course true converts to one ->
[true,true,true].sum == 3
|
November 04, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote: > well I tried this that way, but my count stays 0, same as if I do it in an int function with a return though I clearly have some false elements in the arr. You could also use count: http://dlang.org/phobos/std_algorithm_searching.html#count return arr.count!(x => !x); |
Copyright © 1999-2021 by the D Language Foundation