Properties: .sort and .reverse

Bill Baxter wbaxter at gmail.com
Wed Jul 29 09:35:20 PDT 2009


On Wed, Jul 29, 2009 at 5:02 AM, bearophile<bearophileHUGS at lycos.com> wrote:
> Moritz Warning:
>> They should go into a library.
>> I wonder why they ended up in the language anyway..
>
> Because they are (supposed to be) handy. You don't want to add imports and remember where to import from, if you want to write a 10-lines long program. Such kind of programs are common enough in dynamic languages.
> Another small advantage is that you avoid template bloat that comes from using such functions tanen from a std lib made of function templates over different kind of arrays.
>
> But after seeing how slow the built-in sort is, and seeing how it doesn't accept a "key" mapping function, I think moving sort in the std-lib may be better.
>
> - I'd like to keep reverse as a property, because it's a common operation and because there's only one way to implement it well.
> - Beside the reverse, I'd like to add few more standard methods to arrays:
> - mul, to multiply a char/array n times.
> - find (find position of an item)
> - opIn_r (tell if a given item is present)
> - count (count how many copies of a given item are present)
> - remove (remove first instance of an item)
> - delete (remove i-th item)
> - insert (insert item before index)
> - Maybe "pop()" too may be useful, it's like delete, but it also returns it. Default is the last item.
>
> I presume Andrei will not like such suggestions (that I have written here in one of my first lists of things, lot of time ago).

Even if these things exist, they should still be written in D.  It
doesn't seem right to burden compiler writers with the task of writing
such library-ish functions.  If the importing thing is a problem (and
I tend to agree that it is) then they should be imported automatically
in std.object.

--bb



More information about the Digitalmars-d mailing list