Why any! with map! is not working here

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jun 6 21:32:11 UTC 2019


On Thursday, June 6, 2019 5:50:36 AM MDT rnd via Digitalmars-d-learn wrote:
> On Thursday, 6 June 2019 at 09:49:28 UTC, Jonathan M Davis wrote:
> > So, to start, the any portion should be something more like
> >
> > any!pred(ss);
> >
> > or
> >
> > ss.any!pred();
> >
> > or
> >
> > ss.any!pred;
> >
> > where pred is whatever the predicate is.
>
> Apparently, following also works:
>
>
> any(ss.map!(a => a > 127))   // as written by Marco de Wild
> or
> any(map!(a => a > 127)(ss))
>
>
> Thanks for detailed explanations.
> Philosophically, I personally think, there should be only one way
> to do such things since that will add to simplicity, as in C.
> Apparently, smallness and simplicity of C contributed greatly to
> its success.
> Only drawbacks of C, like its unsafe parts, should be removed and
> clearly advantages newer concepts should be added in a clearly
> defined manner.
> Just thinking loudly!

If any is not given a predicate, it defaults to just checking whether the
element itself is true (requiring that the element be bool), which is why
Marco's suggestion works, but it's a rather odd way to write the code and
will be less efficient unless the optimizer manages to optimize away the
extra work involved with having map.

However, in general, with D, you're not going to find that there is only one
way to do things. There are going to tend to be many different approaches to
solve the same problem. D code is often simple because of the powerful
language constructs and standard library, but it makes no attempt to make it
so that there's only one way to do things - especially when you start
combining stuff to do whatever it is you're trying to do.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list