Recent

Author Topic: [SOLVED] No interest in expansion of array (or set)?  (Read 4464 times)

egsuh

  • Hero Member
  • *****
  • Posts: 827
[SOLVED] No interest in expansion of array (or set)?
« 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?


« Last Edit: July 31, 2021, 01:28:09 am by egsuh »

PascalDragon

  • Hero Member
  • *****
  • Posts: 3310
  • Compiler Developer
Re: No interest in expansion of array (or set)?
« Reply #1 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).

Alextp

  • Hero Member
  • *****
  • Posts: 1366
    • UVviewsoft
Re: No interest in expansion of array (or set)?
« Reply #2 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

egsuh

  • Hero Member
  • *****
  • Posts: 827
Re: No interest in expansion of array (or set)?
« Reply #3 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);

winni

  • Hero Member
  • *****
  • Posts: 2662
Re: [SOLVED] No interest in expansion of array (or set)?
« Reply #4 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

glorfin

  • Full Member
  • ***
  • Posts: 135
  • LMath supporter
Re: No interest in expansion of array (or set)?
« Reply #5 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.

 

TinyPortal © 2005-2018