Getting a total from a user defined variable

Ferhat Kurtulmuş aferust at gmail.com
Thu Apr 20 21:41:37 UTC 2023


On Thursday, 20 April 2023 at 19:41:21 UTC, Joel wrote:
> ```d
> import std;
>
> struct Person {
>     string name;
>     ulong age;
> }
>
> void main() {
>     auto p=[Person("Joel", 43), Person("Timothy", 40)];
>     writeln("Total: ", p.reduce!((a,b) => a.age+b.age)(0UL)); 
> // how do I get the total of ages added together?
> }
> ```

```d
import std;

struct Person {
     string name;
     ulong age;
}

void main() {
     auto p=[Person("Joel", 43), Person("Timothy", 40)];

     writeln("Total: ", p.map!(a => a.age).reduce!"a + b");
}
```


More information about the Digitalmars-d-learn mailing list