Forum > FPC development

[SOLVED] No interest in expansion of array (or set)?

(1/2) > >>

egsuh:
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?


PascalDragon:

--- Quote from: egsuh on July 29, 2021, 02:42:33 am ---1) Array.prototype.join()

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

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


--- Quote from: egsuh on July 29, 2021, 02:42:33 am ---2) Spread operator (...)

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

--- End quote ---

And what does the result look like?


--- Quote from: egsuh on July 29, 2021, 02:42:33 am ---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().
--- End quote ---

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.


--- Quote from: egsuh on July 29, 2021, 02:42:33 am ---BTW, can I write some of these functions using type helpers in Free Pascal?

--- End quote ---

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:

--- 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];

--- End quote ---

No, it looks not Pascalish. Pascalish is:

arrWrap = Concat(arr1, arr2, arr3)

or:

arrWrap = arr1 + arr2 + arr3

egsuh:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---interface type    TIntegerArray = array of integer;    { HIntegerArray }    HIntegerArray = type helper for TIntegerArray   private      function getCommaText: string;      procedure setCommaText(AValue:string);   public      property CommaText: string read getCommaText write setCommaText;   end; implementation function HIntegerArray.getCommaText: string;var   i: integer;begin   Result:= '';   for i in Self do       Result += IfThen(Result='', '', ',') + IntToStr(i);end; procedure HIntegerArray.setCommaText(AValue: string);var   sarr: TStringArray;   i : integer;begin   if trim(AValue) = '' then Exit;   sarr:= AValue.Split([',']);   SetLength(Self, Length(sarr));   for i:= Low(sarr) to High(sarr) do       Self[i] := StrToInt(sarr[i]);end; // usage procedure TForm2.Button2Click(Sender: TObject);begin    IArray.Commatext:= '3,4,9,10';    IArray += [56];    ShowMessage(IArray.Commatext);end;
Well, IArray.Commatext looks much better than

   function CommaText(arr: array of integer);

winni:
Hi


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TIntegerDynArray = array of Integer; 
is already defined in the unit types.

Winni

Navigation

[0] Message Index

[#] Next page

Go to full version