Properties: .sort and .reverse
bearophile
bearophileHUGS at lycos.com
Wed Jul 29 05:02:02 PDT 2009
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).
Bye,
bearophile
More information about the Digitalmars-d
mailing list