Rust switches to external iteration
Jonathan M Davis
jmdavisProg at gmx.com
Thu Jul 4 12:52:27 PDT 2013
On Thursday, July 04, 2013 21:40:23 w0rp wrote:
> On Thursday, 4 July 2013 at 19:26:43 UTC, Jonathan M Davis wrote:
> > Sometimes, you're stuck, because the nature of the type that
> > you're dealing
> > with forces it to be a pure input range, but you just can't
> > take advantage of
> > many algorithms with pure input ranges, which means that you
> > have to write a
> > lot more code or use std.array.array in order to use the
> > various algorithms,
> > forcing you to allocate when it should be completely
> > unnecessary.
>
> That is exactly why yield is useful, because without yield, you
> have two options.
>
> 1. Write a lot of code to create an InputRange. (Cost in time
> taken to write it.)
> 2. Allocate and lose on performance. (Cost in runtime
> performance.)
>
> Yield gives you option 3.
>
> 3. Write very little code while also saving on performance.
Except that in most cases, you should have been writing a range type with more
capabilites than an input range. IMHO, something like yield would pretty much
only be of value in situations where you just need to iterate over something
(so you're not taking advantage of much in the way of algorithms) and where
you need a range type specific to your program which will never be reused
anywhere. And that implies that you're not writing very reusable code.
Sometimes, that's the way things go, but ranges and algorithms are such that
most of the time, you should be able to write range-based functions which are
reusable. And if you're doing that, you should be writing ranges with the full
set of capabilites that they can have, not just input ranges.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list