Why does `filterBidirectional` exist, and why isn't it called `filter`?

Paul Backus snarwin at gmail.com
Thu Mar 9 17:07:40 UTC 2023


On Thursday, 9 March 2023 at 13:16:07 UTC, Steven Schveighoffer 
wrote:
> This has been a constant debate -- should ranges be allowed to 
> lazily initialize on the first call to front (or back)?
>
> I'd say no. back/front shouldn't be modifying operations. I 
> dislike the idea of storing a "was initialized" flag and having 
> to check that on every call.
>
> That being said, there's no formal requirement for it. It's 
> just a convention I think Phobos should stick to for its 
> included ranges.

I think probably you have to go on a case-by-case basis. 
`File.byLine`, for example, does not and should not precompute 
`front`, because doing so is potentially very expensive. OTOH, 
`iota` does and should precompute `front`, because doing so is 
essentially free and makes the implementation much simpler.

In general, I think we should err on the side of laziness, 
because it makes range composition easier. For example, when the 
user writes something like `chain(a(), b(), c())` or 
`choose(cond, a(), b())`, they probably do not want to spend 
unnecessary cycles precomputing `b().front` and `c().front`.


More information about the Digitalmars-d mailing list