Lazarus

Free Pascal => FPC development => Topic started by: egsuh on July 29, 2021, 02:42:33 am

Title: [SOLVED] No interest in expansion of array (or set)?
Post by: egsuh on July 29, 2021, 02:42:33 am
Basically I'm talking about expansions done in javascript. 

Some examples are:

1) Array.prototype.join()

   Example:
      const elements = ['Fire', 'Air', 'Water'];
      console.log(elements.join());
      // expected output: "Fire,Air,Water"


2) Spread operator (...)

    Example:
      const arr1 = [1, 2, 3];
      const arr2 = [4, 5, 6];
      const arr3 = [7, 8, 9];
      const arrWrap = [...arr1, ...arr2, ...arr3];


3) IndexOf or Set.prototype.has().

I think if expanding set operation is not easy, then it may be added to array.  Like TArray.has().


BTW, can I write some of these functions using type helpers in Free Pascal?


Title: Re: No interest in expansion of array (or set)?
Post by: PascalDragon on July 29, 2021, 09:15:46 am
1) Array.prototype.join()

   Example:
      const elements = ['Fire', 'Air', 'Water'];
      console.log(elements.join());
      // expected output: "Fire,Air,Water"

Not everything is convertible to a string, so this can't be generalized.

2) Spread operator (...)

    Example:
      const arr1 = [1, 2, 3];
      const arr2 = [4, 5, 6];
      const arr3 = [7, 8, 9];
      const arrWrap = [...arr1, ...arr2, ...arr3];

And what does the result look like?

3) IndexOf or Set.prototype.has().

I think if expanding set operation is not easy, then it may be added to array.  Like TArray.has().

This is one of the main features of sets, the in-operator, no need to extend anything there.

And arrays are not sets.

You can achieve this functionality by declaring a generic function and with the upcoming implicit specialization feature (not yet part of trunk) you even won't have to type out the specialization.

BTW, can I write some of these functions using type helpers in Free Pascal?

You need to declare a type helper for each array type (generic type helpers are not yet supported). Also there might be problems if you try to invoke a helper on an anonymous array of X if it was declared on a named type TXArray = array of X and vice versa (have not tested that part in a while).
Title: Re: No interest in expansion of array (or set)?
Post by: AlexTP on July 29, 2021, 12:52:28 pm
Quote
2) Spread operator (...)

    Example:
      const arr1 = [1, 2, 3];
      const arr2 = [4, 5, 6];
      const arr3 = [7, 8, 9];
      const arrWrap = [...arr1, ...arr2, ...arr3];

No, it looks not Pascalish. Pascalish is:

arrWrap = Concat(arr1, arr2, arr3)

or:

arrWrap = arr1 + arr2 + arr3
Title: Re: No interest in expansion of array (or set)?
Post by: egsuh on July 31, 2021, 01:27:39 am
I have tested following, which is good enough. By declaring new types, e.g. TIntegerArray=array of integer, the codes look much like OOP.

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. type
  4.    TIntegerArray = array of integer;
  5.  
  6.    { HIntegerArray }
  7.  
  8.    HIntegerArray = type helper for TIntegerArray
  9.    private
  10.       function getCommaText: string;
  11.       procedure setCommaText(AValue:string);
  12.    public
  13.       property CommaText: string read getCommaText write setCommaText;
  14.    end;
  15.  
  16. implementation
  17.  
  18. function HIntegerArray.getCommaText: string;
  19. var
  20.    i: integer;
  21. begin
  22.    Result:= '';
  23.    for i in Self do
  24.        Result += IfThen(Result='', '', ',') + IntToStr(i);
  25. end;
  26.  
  27. procedure HIntegerArray.setCommaText(AValue: string);
  28. var
  29.    sarr: TStringArray;
  30.    i : integer;
  31. begin
  32.    if trim(AValue) = '' then Exit;
  33.    sarr:= AValue.Split([',']);
  34.    SetLength(Self, Length(sarr));
  35.    for i:= Low(sarr) to High(sarr) do
  36.        Self[i] := StrToInt(sarr[i]);
  37. end;
  38.  
  39. // usage
  40.  
  41. procedure TForm2.Button2Click(Sender: TObject);
  42. begin
  43.     IArray.Commatext:= '3,4,9,10';
  44.     IArray += [56];
  45.     ShowMessage(IArray.Commatext);
  46. end;

Well, IArray.Commatext looks much better than

   function CommaText(arr: array of integer);
Title: Re: [SOLVED] No interest in expansion of array (or set)?
Post by: winni on July 31, 2021, 10:45:50 am
Hi

Code: Pascal  [Select][+][-]
  1. TIntegerDynArray = array of Integer;
  2.  

is already defined in the unit types.

Winni
Title: Re: No interest in expansion of array (or set)?
Post by: glorfin on September 16, 2021, 05:27:15 pm
Well, IArray.Commatext looks much better than

   function CommaText(arr: array of integer);

Why? I honestly do not understand why everywhere OOP must be used. And one potential problem with this approach is that only one helper at a time may be active. It means, that if several type helpers exist for TIntegerArray, you may end up with not knowing, which one works in a program using many third party units and what behaviour to expect. Therefore, I am not very enthusiastic about type helpers, and to that end about syntax expansion in general.
Title: Re: No interest in expansion of array (or set)?
Post by: PascalDragon on September 20, 2021, 09:43:31 am
I honestly do not understand why everywhere OOP must be used.

While only a small effect it allows an IDE to show the methods currently available while for the procedural approach you need to search through a larger list.

And one potential problem with this approach is that only one helper at a time may be active.

FPC 3.3.1 has the MultiHelpers modeswitch. If multiple helpers provide the same method the order of the units determines which one is used just like for any scoping rules.
TinyPortal © 2005-2018