Would this function benefit from functional programming?

monkyyy crazymonkyyy at gmail.com
Thu Nov 21 01:56:34 UTC 2024


On Thursday, 21 November 2024 at 00:34:50 UTC, WhatMeWorry` wrote:
> I use this pattern very frequently throughout my code
>
> ```
> void displayHex()
> {
>     foreach(r; 0..rows)
>     {
>         foreach(c; 0..columns)
>         {
>             writeln("...");
>             foreach(p; 0..6)
>             {
>                 writeln("...");
>             }
>         }
>     }
> }
> ```
> And I was wondering if it would be worthwhile to convert it to 
> functional programming. I saw a talk by Mr. Bright who did 
> something like, a.c.d(3).e.f
>
> But his example was understandably terse.  I believe the map 
> function can take the place of the foreaches?  Also I've been 
> unable to find examples of writeln being used.  I'm not 
> interested in speeding up the code or making it more 
> understandable. Just making it more compact.  Maybe what I'm 
> really asking is if functional programming is not appropriate 
> in all scenarios.

```
row.iota.each!(
    cols.iota.each!((c){
      writeln("...");
      iota(6).each!(a=>writeln("..."));
})});
```
iota is a bad name for counter, and map has an unnecessary 
restriction on void functions; but this is a travail conversion


More information about the Digitalmars-d-learn mailing list