Lazarus

Announcements => Third party => Topic started by: lainz on October 11, 2019, 02:32:09 am

Title: Functional String List
Post by: lainz on October 11, 2019, 02:32:09 am
Hi, yesterday at night when the electric storm calm down  :) I coded the Functional String List.

https://github.com/lainz/FunctionalStringList/blob/master/ufunctionalstringlist.pas

Is a work in progress, but it currently handles:

Code: Pascal  [Select][+][-]
  1.   TFunctionalStringList = class helper for TStringList
  2.   public
  3.     function Filter(fun: TStringListFilterMethod): TStringList;
  4.     function Reduce(fun: TStringListReduceMethod;
  5.       startingValue: string = ''): string;
  6.     function Map(fun: TStringListMapMethod): TStringList;
  7.     procedure ForEach(fun: TStringListForeachMethod);
  8.     function Pop: string;
  9.     function Push(s: string): integer;
  10.     function Shift: string;
  11.     function Unshift(s: string): integer;
  12.     function Reverse: TStringList;
  13.     function Join(separator: string): string;
  14.     function Concat(other: TStringList): TStringList;
  15.     function Slice(fromIndex: integer): TStringList;
  16.     function Slice(fromIndex, toIndex: integer): TStringList;
  17.     function Fill(Value: string; start: integer = 0;
  18.       _end: integer = -1): TStringList;
  19.     function Some(fun: TStringListSomeMethod): boolean;
  20.     function IndexOf(s: string; start: integer = 0): integer;
  21.     function LastIndexOf(s: string; start: integer = -1): integer;
  22.   end;

It is similar to JavaScript array functions.
Title: Re: Functional String List
Post by: lainz on October 11, 2019, 07:14:38 pm
I opened a bug report to see if some or all of these methods can be added to TStringList
https://bugs.freepascal.org/view.php?id=36165
Title: Re: Functional String List
Post by: simone on October 11, 2019, 08:40:42 pm
Thanks for your nice work. This demonstrate that object pascal also allows some sort of functional programming features, as I always suspected, without having time to experience it. You did this job for me. Thanks.
Title: Re: Functional String List
Post by: dsiders on October 12, 2019, 02:27:25 am
I opened a bug report to see if some or all of these methods can be added to TStringList
https://bugs.freepascal.org/view.php?id=36165

I would rather see the FunctionalStringList.pas unit donated to the project. No need to add them directly to TStringList.

Also, should Slice be overloaded? Like:

Code: Pascal  [Select][+][-]
  1. fsl.Slice(FromIndex: Integer);
  2. fsl.Slice(FromIndex, ToIndex: Integer);
  3.  
Title: Re: Functional String List
Post by: AlexTP on October 12, 2019, 07:49:33 am
Adding it to TStringList will make StringList too big over Delphi version (it's ok if FPC adds 2-3 methods, but not ok if it adds 20 new methods).
Title: Re: Functional String List
Post by: wp on October 12, 2019, 11:17:20 am
Adding it to TStringList will make StringList too big over Delphi version (it's ok if FPC adds 2-3 methods, but not ok if it adds 20 new methods).

+1

I normally don't need these functions in a standard StringList, and if I do I can add this functionality easily
Title: Re: Functional String List
Post by: lainz on October 12, 2019, 12:59:03 pm
I commented on the bug report.

In fact there are more methods available but I just coded only some of them.

I'm used to use filter and foreach in JavaScript and Kotlin. Yes they have automatic memory management but also on my Pascal test project there are no leaks.

I wish these methods are added to the generic lists too, not only on stringlist.
Title: Re: Functional String List
Post by: lainz on October 12, 2019, 02:16:07 pm
I opened a bug report to see if some or all of these methods can be added to TStringList
https://bugs.freepascal.org/view.php?id=36165

I would rather see the FunctionalStringList.pas unit donated to the project. No need to add them directly to TStringList.

Also, should Slice be overloaded? Like:

Code: Pascal  [Select][+][-]
  1. fsl.Slice(FromIndex: Integer);
  2. fsl.Slice(FromIndex, ToIndex: Integer);
  3.  

Thanks, I commented in the bug report, if they do not add them I will donate the unit. If the unit is not accepted, I will add as a package into OPM.

I've added the overloaded Slice method, thanks for the suggestion.
Title: Re: Functional String List
Post by: Thaddy on October 12, 2019, 02:17:16 pm
I commented too:
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, so one can choose if that code should be included.

Also note there is some duplicate functionality here, consider:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses sysutils, classes;
  3. var
  4.   l:Tstringlist;
  5.   s:string;
  6. begin
  7.   l:=tstringlist.create;
  8.   try
  9.     l.add('testme');
  10.     l.add('test me 1');
  11.     l.add('test me 2');
  12.     for s in l do; // some of the suggested extensions that are already available for string.
  13.   finally
  14.     l.free;
  15.   end;
  16. end.
Title: Re: Functional String List
Post by: marcov on October 12, 2019, 02:37:34 pm
Adding it to TStringList will make StringList too big over Delphi version (it's ok if FPC adds 2-3 methods, but not ok if it adds 20 new methods).

Too big how? the methods will probably be smartlinked out.

From a software management perspective, tstringlist is already hoplessly overcomplicated IMHO.
Title: Re: Functional String List
Post by: lainz on October 12, 2019, 02:51:25 pm
I commented too:
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, so one can choose if that code should be included.

Also note there is some duplicate functionality here, consider:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses sysutils, classes;
  3. var
  4.   l:Tstringlist;
  5.   s:string;
  6. begin
  7.   l:=tstringlist.create;
  8.   try
  9.     l.add('testme');
  10.     l.add('test me 1');
  11.     l.add('test me 2');
  12.     for s in l do; // some of the suggested extensions that are already available for string.
  13.   finally
  14.     l.free;
  15.   end;
  16. end.

Thanks Thaddy, that could be a propper way to include this.

I've added more tests as requested in the bugtracker.

Edit: Also converted it to a type helper as you suggested  :)
Title: Re: Functional String List
Post by: jamie on October 12, 2019, 10:33:42 pm
Please do not add all of that to the TStringList…

make a unit with that specific code in it and allow those that want to use it included it .

 Most if not all of those methods already exists in the libs..
Title: Re: Functional String List
Post by: Thaddy on October 13, 2019, 06:45:50 am
Yes, limit yourself to list oriented operations, not to string oriented operations on its members: those are already there in the form of the string helpers in sysutils.
Title: Re: Functional String List
Post by: lainz on October 13, 2019, 02:45:23 pm
The unit is already there, you're free to use it or not, so I will not list only the methods that request a function to work. Choosing what will be included or not is not my work, is being discussed in the bugtracker. Maybe nothing is included and it will be fine too.
Title: Re: Functional String List
Post by: lainz on November 03, 2019, 02:56:33 am
Finally these methods were added to TStrings, need to update my trunk to see them.
Title: Re: Functional String List
Post by: mr-highball on November 07, 2019, 04:34:39 pm
Just saw this and it reminded me about this,
https://forum.lazarus.freepascal.org/index.php/topic,45818.75.html

As a demo I only implemented the map method, but it takes advantage of the new generic methods, so maybe it will be useful to someone. I plan to implement the others when I get some time... 
TinyPortal © 2005-2018