Two chunks but No allocation

rkompass rkompass at gmx.de
Thu Mar 28 23:08:54 UTC 2024


On Thursday, 28 March 2024 at 03:54:05 UTC, Salih Dincer wrote:
> On Wednesday, 27 March 2024 at 20:50:05 UTC, rkompass wrote:
>> This works:
>
> I decided to give the full code. Maybe then it will be better 
> understood what I mean. I actually pointed out the indirect 
> solution above but it's a bit ugly and I'm sure there must be a 
> better way?
>
I didn't look exactly at you code but at the ranges problem.


Perhaps this is of help:

```d
import std.stdio;
import std.range;
import std.algorithm;

void main() {
   auto fib = (real a, real b) => recurrence!"a[n-1] + a[n-2]"(a, 
b);
   auto golden3 = fib(1,1).chunks(2).map!(r => r.fold!((a, e) => 
a/e)).take(10);
   writeln(golden3);
}
```
I thought what you wanted (and what I found to be an interesting 
problem) was to convert the subranges delivered by `chunks(2)` to 
values that still are generated lazily, without saving them in an 
array (which converts the range type to a higher one), according 
to original range.

You can drop and take from the folded values range.

I got `[1, 0.666667, 0.625, 0.619048, 0.618182, 0.618056, 
0.618037, 0.618034, 0.618034, 0.618034]` from the above code.


More information about the Digitalmars-d-learn mailing list