The lifetime of reduce's internal seed

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 26 01:51:12 PDT 2014


On Saturday, 26 April 2014 at 06:24:26 UTC, Ali Çehreli wrote:
> On 04/22/2014 11:45 AM, monarch_dodra wrote:
>
> > Reduce returns "the seed". It's actually doing something more
> like this:
> >
> > int[1] foo()
> > {
> >      int[1] sum
> >      sum = sum[]; //The lambda operates, and the
> >                   //result is assigned back to the seed.
> >      return sum; //Returns the seed.
> > }
>
> My original lambda that returned a slice was correct then. The 
> seed would eventually be copied out. Had the compiler not allow 
> slicing the rvalue then I would be in good shape.

Well... your lambda *was* returning a slice to its local copy of 
sum. So I thin kit is still wrong. "(ref sum, _) => sum[]" would 
have been correct though.

> > BTW, I'm re-implemented reduce recently (not yet pulled), but
> I was
> > *very* thorough about documenting what it does:
> > https://github.com/D-Programming-Language/phobos/pull/2060
> >
> > Could you take a look at it (the documentation I mean), and
> tell me if
> > everything is what you would have expected?
>
> I think it looks great! :)
>
> Two comments/questions which I did not make on github:
>
> 1) Some of the documentation comments that are inside a scope 
> are not formatted as such. For example, this comment does not 
> start with /++ :
>
>
> https://github.com/monarchdodra/phobos/blob/reduceReimpl/std/algorithm.d#L753
>
> I wonder whether they are still included in the produced 
> documentation.

Nope, that was a mistake on my part. Good catch.

> 2) I think even single-line code blocks should have curly 
> brackets but Phobos code does not follow that guideline. :)
>
> Ali

It depends I say. I usually do that, but for certain functions, 
such as reduce, it would *literally* double the amount of lines 
required to write it. I that point, the function becomes long 
enough for it to be a readability problem.


More information about the Digitalmars-d-learn mailing list