uniq and array of enum members (That are all strings)
H. S. Teoh
hsteoh at quickfur.ath.cx
Wed Jan 16 17:51:33 UTC 2019
On Wed, Jan 16, 2019 at 04:37:21PM +0000, bauss via Digitalmars-d-learn wrote:
> On Wednesday, 16 January 2019 at 16:35:04 UTC, H. S. Teoh wrote:
> > It's not trivial. In order for the computer to know whether or not
> > the i'th element should be excluded, it needs to know what has come
> > before it.
> That's not necessarily true.
> It just has to know whether the element matches an earlier element.
> Your filter example demonstrates exactly why sorting isn't necessary.
Sorting is the simplest approach because then the problem is reduced to
using .uniq. It's not necessarily the most *efficient* approach.
But regardless, the point is that in order to know whether some given
element e matches an earlier element, the computer must somehow keep
track of previously-seen elements. Either you do this implicitly by
sorting it so that the relevant previously-seen element occurs
immediately before the current one, or you have to store it explicitly
somewhere, like in an AA, or you have to compute it on the fly. There
is no way to get around this; you cannot filter an element based on
information you don't have. So either you recompute that information
(search backward until you find a match), or you store it in a structure
like an AA where you can do the lookup easily. The information has to
come from *somewhere*.
Nearly all men can stand adversity, but if you want to test a man's character, give him power. -- Abraham Lincoln
More information about the Digitalmars-d-learn