[your code here]

WebFreak001 d.forum at webfreak.org
Thu Apr 29 18:09:22 UTC 2021


On Thursday, 29 April 2021 at 17:04:20 UTC, Dennis wrote:
> On Thursday, 29 April 2021 at 15:53:43 UTC, Berni44 wrote:
>> I fear that new users might already be swamped with functional 
>> style and won't understand what's going on here. (But nice 
>> anyway.)
>
> I think the range-based functional style of D is 
> overrepresented.
>
> When I first landed on the homepage, I saw the "Sort lines" 
> example which scared me off. It looked totally unfamiliar, I 
> thought D was more akin to Scala/Haskell than the Java/C-like 
> languages I was most familiar with. Only later did I figure out 
> that D really is a newer C/C++ like the name implies, and the 
> range programming is just Phobos acting on top of it.
>
> In all the examples, there is not a single 
> `if`/`do`/`while`/`for` statement, not a single `class` or 
> `interface` declaration, and only 1 `struct` declaration. I 
> think it wouldn't hurt to have examples showing 
> boring/traditional code in D syntax in addition to the fancy, 
> 'code-golfy' programs.

I personally think it can be used very well with foreach:
```d
// Estimate π using random integers
void main()
{
     import std.stdio, std.range, std.random, std.math, std.conv, 
std.numeric;
     Mt19937_64 randomGen; randomGen.seed(unpredictableSeed!ulong);
     int pairCount = 1_000_000;
     int coprimeCount = 0;
     foreach (randomPair; randomGen.chunks(2).take(pairCount)) {
         if (gcd(randomPair.front, randomPair.dropOne.front) == 1)
             coprimeCount++;
     }
     double prob = coprimeCount.to!double / pairCount.to!double;
     writefln("π ≈ %.15f", sqrt(6.0 / prob));
}
```

Though I would have liked more if the `take` amount could be a 
template parameter instead so it could be tuples where I can 
directly take [0] and [1]


More information about the Digitalmars-d mailing list