Recent

Author Topic: Functional String List  (Read 5241 times)

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Functional String List
« 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.
« Last Edit: October 13, 2019, 03:13:19 pm by lainz »

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Functional String List
« Reply #1 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

simone

  • Hero Member
  • *****
  • Posts: 573
Re: Functional String List
« Reply #2 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.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Functional String List
« Reply #3 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.  
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

AlexTP

  • Hero Member
  • *****
  • Posts: 2384
    • UVviewsoft
Re: Functional String List
« Reply #4 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).

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Functional String List
« Reply #5 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

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Functional String List
« Reply #6 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.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Functional String List
« Reply #7 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Functional String List
« Reply #8 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.
« Last Edit: October 12, 2019, 02:18:56 pm by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Functional String List
« Reply #9 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.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Functional String List
« Reply #10 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  :)
« Last Edit: October 12, 2019, 03:17:47 pm by lainz »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Functional String List
« Reply #11 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..
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Functional String List
« Reply #12 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.
Specialize a type, not a var.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Functional String List
« Reply #13 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.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Functional String List
« Reply #14 on: November 03, 2019, 02:56:33 am »
Finally these methods were added to TStrings, need to update my trunk to see them.

 

TinyPortal © 2005-2018