Would this function benefit from functional programming?

Salih Dincer salihdb at hotmail.com
Thu Nov 21 05:20:01 UTC 2024


On Thursday, 21 November 2024 at 00:34:50 UTC, WhatMeWorry` wrote:
> I use this pattern very frequently throughout my code

We can't help you by seeing your pseudocode. Because when we look 
at your code, instead of 3 loops, 2 loops or even a single loop 
using HOFs is enough. I'm posting the following pieces of code in 
case it helps someone:

```d
import std;
enum { rows = 1, columns = 2 }

void main()
{
   // with HOFs
   iota(rows * columns).each!(c => 
repeat("...",6).writefln!"...\n%-(%s\n%)");

   displayX(); // equivalent
}
```

**Equivalent:**
```d
void displayX()
{
   foreach(c; 0..rows * columns)
   {
     writeln("title");
     foreach(p; 0..6)
     {
       writeln("lines");
     }
   }
}
```

SDB at 79


More information about the Digitalmars-d-learn mailing list