Would this function benefit from functional programming?

Inkrementator invalid at invalid.org
Thu Nov 21 21:43:50 UTC 2024


On Thursday, 21 November 2024 at 00:34:50 UTC, WhatMeWorry` wrote:
> Just making it more compact.

Python itertools has `product`, I use it from time to time and it 
can definitely make code more compact.

```
auto product(R1, R2)(R1 r1, R2 r2)
{
     return r1.map!((i) => zip(repeat(i), r2.save())).joiner()
}

product(iota(rows), iota(columns).each!(bind!( (row, col){
   // Do stuff with the values
}

foreach(row, col; product(iota(rows), iota(columns)){...}
```

It's debatable whether this is nicer, especially when D still has 
suboptimal tuple support and boilerplate like 'bind' is needed, 
but it might be worth thinking about when you can chain more than 
a simple `each` call behind it.


More information about the Digitalmars-d-learn mailing list