View Issue Details
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0036165||FPC||FCL||public||2019-10-11 19:13||2019-11-02 16:11|
|Reporter||lainz||Assigned To||Michael Van Canneyt|
|Priority||normal||Severity||minor||Reproducibility||unable to reproduce|
|Fixed in Version||3.3.1|
|Summary||0036165: Add functional methods to TStringList|
|Description||Hi, is possible to add functional methods like: filter, map, reduce and foreach for TStringList?|
Here I wrote an unit that includes all these methods, plus others that can be handy to have too
Here is the test program
|Tags||No tags attached.|
|Fixed in Revision||43365|
I guess the reduce isn’t exactly that. It needs an extra parameter for the starting value (could be by default the empty string) and value are added from that.
Also I suggest Join function to handle the case of an empty list.
In the Push function, i would rather not do AddText but Add.
In the Reverse function, it would be more efficient to swap items in place. Though from a functional point of view you would rather avoid changing the source list but only create a new one.
For the Concat function you can use AddStrings to add a range of value instead of going through Text property.
I would rather not add a synonym Length as Count exists already.
I am not sure what is the point of ValueOf.
I hope that these methods are not added. I don't need them normally, and if I do I can create them easily by myself.
In particular I don't like that some functions create stringlists on their own in a silent way. It is very easy to forget to destroy them. This kind of programming is good for languages with automatic garbage collection, but not for Pascal.
Thanks circular for the suggestions. I will try to add them.
About the possibility of memory issues instead of returning a new list it can be passed as a const parameter, like is done in some other places.
If there is no way to adding these functions, I can donate the unit as was suggested in the forum.
If not added at all I can create a package and share it in OPM.
||And reduce will crash on a zero element tstringlist. I think this code could some testing and usage first before permanently including|
||I didn't plan on adding all methods (some are duplicates of existing ones) and proper testing was on my todo list.|
I don't think it is necessary to add these methods to TStringlist, but there is maybe a case to create a type helper for TStringlist instead of a class.
That can be done in a separate unit. Also note there is duplicate functionality here, consider:
uses sysutils, classes;
l.add('test me 1');
l.add('test me 2');
for s in l do; // some of the suggested extensions that are already available for string.
Thanks I fixed reduce and applied @circular suggestions as well.
Yes, it needs more testing. I will add more tests.
||If FPC already added 2-3 new methods to Delphi StringList- it's ok, but if FPC adds 20 new methods- it's overkill.|
I must agree with Alexey Tor, that's a bit over kill.
altering a Basic component used in thousands of locations in code with that much rewrite will take the rest of my coding life to work around the bugs that will appear.
This is totally insane. I don't need all that extra bloat sitting around not doing anything other than slowing down the process, we have already enough of that going on at work now!
Can't a StringList Helper be made in a separate unit for those that think they need that pile of bloat and slow down?
I mean, isn't that what building classes derived from for example a TStringList is what this is all about?
Let's not get carried away or hysterical.
Adding extra methods does not harm you in any way as long as existing methods are not touched.
You don't need to use these methods if you don't want them.
They will also not slow things down unless existing methods would be altered to accomodate for them.
In the best case your binary will get a bit bigger.
Added most of them to TStrings.
The ones that return a TStrings instance have been created in 2 forms:
one where the target strings must be specified, one where a list of the same class as the stringlist is returned.
Foreach has been added in 3 flavours: one with string, one with string, index, one with string, index and object.
Methods that have an existing equivalent with another name, have not been added.
|2019-10-11 19:13||lainz||New Issue|
|2019-10-11 20:55||Michael Van Canneyt||Assigned To||=> Michael Van Canneyt|
|2019-10-11 20:55||Michael Van Canneyt||Status||new => assigned|
|2019-10-12 09:26||circular||Note Added: 0118504|
|2019-10-12 11:14||wp||Note Added: 0118507|
|2019-10-12 12:51||lainz||Note Added: 0118510|
|2019-10-12 14:04||Marco van de Voort||Note Added: 0118513|
|2019-10-12 14:07||Michael Van Canneyt||Note Added: 0118514|
|2019-10-12 14:12||Thaddy de Koning||Note Added: 0118515|
|2019-10-12 14:13||Thaddy de Koning||Note Edited: 0118515||View Revisions|
|2019-10-12 14:20||lainz||Note Added: 0118516|
|2019-10-12 18:39||CudaText man||Note Added: 0118521|
|2019-10-12 21:32||jamie philbrook||Note Added: 0118527|
|2019-10-12 21:43||Michael Van Canneyt||Note Added: 0118530|
|2019-11-02 16:11||Michael Van Canneyt||Status||assigned => resolved|
|2019-11-02 16:11||Michael Van Canneyt||Resolution||open => fixed|
|2019-11-02 16:11||Michael Van Canneyt||Fixed in Version||=> 3.3.1|
|2019-11-02 16:11||Michael Van Canneyt||Fixed in Revision||=> 43365|
|2019-11-02 16:11||Michael Van Canneyt||FPCTarget||=> 4.0.0|
|2019-11-02 16:11||Michael Van Canneyt||Note Added: 0118987|