View Issue Details

IDProjectCategoryView StatusLast Update
0036165FPCFCLpublic2019-11-02 16:11
ReporterlainzAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityunable to reproduce
Status resolvedResolutionfixed 
Platformx64OSWindowsOS Version10
Product Version3.3.1Product Build 
Target VersionFixed in Version3.3.1 
Summary0036165: Add functional methods to TStringList
DescriptionHi, 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
https://github.com/lainz/FunctionalStringList/blob/master/ufunctionalstringlist.pas

Here is the test program
https://github.com/lainz/FunctionalStringList/blob/master/umain.pas
TagsNo tags attached.
Fixed in Revision43365
FPCOldBugId
FPCTarget4.0.0
Attached Files

Activities

circular

2019-10-12 09:26

reporter   ~0118504

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.

Regards

wp

2019-10-12 11:14

reporter   ~0118507

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.

lainz

2019-10-12 12:51

reporter   ~0118510

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.

Marco van de Voort

2019-10-12 14:04

manager   ~0118513

And reduce will crash on a zero element tstringlist. I think this code could some testing and usage first before permanently including

Michael Van Canneyt

2019-10-12 14:07

administrator   ~0118514

I didn't plan on adding all methods (some are duplicates of existing ones) and proper testing was on my todo list.

Thaddy de Koning

2019-10-12 14:12

reporter   ~0118515

Last edited: 2019-10-12 14:13

View 2 revisions

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:

{$mode delphi}{$H+}
uses sysutils, classes;
var
  l:Tstringlist;
  s:string;
begin
  l:=tstringlist.create;
  try
    l.add('testme');
    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.
  finally
    l.free;
  end;
end.

lainz

2019-10-12 14:20

reporter   ~0118516

Thanks I fixed reduce and applied @circular suggestions as well.

Yes, it needs more testing. I will add more tests.

Alexey Tor.

2019-10-12 18:39

reporter   ~0118521

If FPC already added 2-3 new methods to Delphi StringList- it's ok, but if FPC adds 20 new methods- it's overkill.

jamie philbrook

2019-10-12 21:32

reporter   ~0118527

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?

Michael Van Canneyt

2019-10-12 21:43

administrator   ~0118530

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.

Michael Van Canneyt

2019-11-02 16:11

administrator   ~0118987

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.

Issue History

Date Modified Username Field Change
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 Alexey Tor. 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