# Lazarus

## Free Pascal => General => Topic started by: giahung1997 on June 07, 2019, 10:25:16 am

Title: Arbitrary complex data structure?
Post by: giahung1997 on June 07, 2019, 10:25:16 am
Title: Re: Arbitrary complex data structure?
Post by: Thaddy on June 07, 2019, 03:09:17 pm
Dynamic arrays can be ragged arrays. I gave an example on this forum some time ago.
Simplest example:
Code: Pascal  [Select][+][-]
1. {\$mode delphi}
2. var
3.    a:array of array of integer;
4. begin
5.    a:= [[1,2,3,4],[1,2,3]]; // now 4 X 3, rectangular
6.    a[1] := [4,5]; // change 1 element to 2 instead of three: ragged, no longer rectangular
7. end.
Needs FPC 3.2.0 or higher.
You can build infinitely complex structures using this technique. Does that help?
Title: Re: Arbitrary complex data structure?
Post by: Zoran on June 19, 2019, 07:34:42 pm
Dynamic arrays can be ragged arrays. I gave an example on this forum some time ago.
Simplest example:
Code: Pascal  [Select][+][-]
1. {\$mode delphi}
2. var
3.    a:array of array of integer;
4. begin
5.    a:= [[1,2,3,4],[1,2,3]]; // now 4 X 3, rectangular
6.    a[1] := [4,5]; // change 1 element to 2 instead of three: ragged, no longer rectangular
7. end.
Needs FPC 3.2.0 or higher.
You can build infinitely complex structures using this technique. Does that help?

In this example, I don't understand how is a rectangular in the first place?!

Title: Re: Arbitrary complex data structure?
Post by: lucamar on June 19, 2019, 08:31:50 pm
In this example, I don't understand how is a rectangular in the first place?!

The line:

Code: [Select]
`a:= [[1,2,3,4],[1,2,3]];`
creates an array of 4 rows, 3 columns per row (or viceversa ;)); i.e. a rectangular array, as if it had been declared as:
a: array[0..3, 0..2] of Integer;
Title: Re: Arbitrary complex data structure?
Post by: Thaddy on June 19, 2019, 08:43:42 pm

In this example, I don't understand how is a rectangular in the first place?!
Well , that's disappointing  because it shows an arbitrary array that  I understood what OP meant.
Title: Re: Arbitrary complex data structure?
Post by: Zoran on June 19, 2019, 09:12:40 pm
In this example, I don't understand how is a rectangular in the first place?!

The line:

Code: [Select]
`a:= [[1,2,3,4],[1,2,3]];`
creates an array of 4 rows, 3 columns per row (or viceversa ;)); i.e. a rectangular array, as if it had been declared as:
a: array[0..3, 0..2] of Integer;

Strange syntax it is. %)

I would expect that initializer to allocate and populate array a to something like this:
a[0] = [1, 2, 3, 4] -- four elements
a[1] = [1, 2, 3] -- three elements

but, if I understand you well, it is actually:
a[0] ... allocate three integers (will they be initialized? to what? zeros?)
a[1] ... ditto
a[2] ... ditto
a[3] ... ditto

?

Title: Re: Arbitrary complex data structure?
Post by: lucamar on June 19, 2019, 09:49:15 pm
Yes, that's it. It's kind of an array constructor; something like BASIC's Dim() sentence.

That is,  if I understood correctly the new sintax  :-[

ETA: Oh, re. initialization, IIRC they are initialized to the Default for the items' type. in that same constructor? I'm not sure.

It was announced in May, 2018 in (where else? :)) the fpc-anounce mailing list (https://lists.freepascal.org/pipermail/fpc-announce/2018-May/000606.html) and extensively discused in fpc-pascal (https://lists.freepascal.org/pipermail/fpc-pascal/2018-May/053892.html)
Title: Re: Arbitrary complex data structure?
Post by: Zoran on June 19, 2019, 11:28:30 pm

It was announced in May, 2018 in (where else? :)) the fpc-anounce mailing list (https://lists.freepascal.org/pipermail/fpc-announce/2018-May/000606.html) and extensively discused in fpc-pascal (https://lists.freepascal.org/pipermail/fpc-pascal/2018-May/053892.html)

But no, according to this announcement, it will be what I thought in the first place -- it will allocate 4+3 = 7 integers (and initialize them to given values). It will not allocate 4 * 3 = 12 integers.

So, the array was not rectangular in the first place.

Title: Re: Arbitrary complex data structure?
Post by: lucamar on June 20, 2019, 09:34:52 am
But no, according to this announcement, it will be what I thought in the first place -- it will allocate 4+3 = 7 integers (and initialize them to given values). It will not allocate 4 * 3 = 12 integers.

So, the array was not rectangular in the first place.

Hmmm ... You're right. It's not understandable otherwise.

Seems I'll have to re-learn quite some things after the 3.2 release  :)
Title: Re: Arbitrary complex data structure?
Post by: Zoran on June 20, 2019, 10:38:48 am
Ragged array like this is not what I means and I myself think it ugly. When people from JVM based language background said data structure he means Classes.

There are different container classes:
The simplest: TFPList (https://www.freepascal.org/docs-html/current/rtl/classes/tfplist.html)

Then, TList (https://www.freepascal.org/docs-html/current/rtl/classes/tlist.html)
TColection (https://www.freepascal.org/docs-html/current/rtl/classes/tcollection.html)

Then, generic containers (unit fgl) (https://www.freepascal.org/docs-html/current/rtl/fgl/index-4.html)
Apart from these generic containers from fgl unit, there is more efficient generics.collections library, but not yet available in current FPC 3.0.

Some information in wiki: https://wiki.freepascal.org/Data_Structures,_Containers,_Collections (https://wiki.freepascal.org/Data_Structures,_Containers,_Collections)

Title: Re: Arbitrary complex data structure?
Post by: marcov on June 20, 2019, 10:56:06 am
I think he means something like Tuples in some languages. It is not an original java or C# feature, but one that was added later because their VMs got it added for other functional and/or scripting languages. (LINQ)

And while you can emulate it with arrays of variants to some agree (and TDataset etc does), the scripting like language level syntax is not there. And hopefully never comes.

If you want a scripting language, may I suggest Python?
Title: Re: Arbitrary complex data structure?
Post by: Thaddy on June 20, 2019, 11:32:09 am
Yes, and has excellent FPC bindings...
Title: Re: Arbitrary complex data structure?
Post by: giahung1997 on June 20, 2019, 03:33:47 pm
I think he means something like Tuples in some languages. It is not an original java or C# feature, but one that was added later because their VMs got it added for other functional and/or scripting languages.

And while you can emulate it with arrays of variants to some agree (and TDataset etc does), the scripting like language level syntax is not there. And hopefully never comes.

If you want a scripting language, may I suggest Python?
Please close this thread. Everyone misunderstood me and I confused everyone  :(
Title: Re: Arbitrary complex data structure?
Post by: PascalDragon on June 21, 2019, 10:09:27 am
Please close this thread. Everyone misunderstood me and I confused everyone  :(
But please don't edit your initial post to remove the question, cause now nobody knows what everyone answered to.
Title: Re: Arbitrary complex data structure?
Post by: giahung1997 on June 22, 2019, 07:36:44 am
Please close this thread. Everyone misunderstood me and I confused everyone  :(
But please don't edit your initial post to remove the question, cause now nobody knows what everyone answered to.
Then delete this thread altogether. It's a useless offtopic bulshit  8-)
Title: Re: Arbitrary complex data structure?
Post by: engkin on June 22, 2019, 12:42:45 pm
Please close this thread. Everyone misunderstood me and I confused everyone  :(
But please don't edit your initial post to remove the question, cause now nobody knows what everyone answered to.

I'm a fantom language hobbyist fan and I have used it to do many small homework kindof exercises. My algorithms sucks and my App all very slow and memory hundry but at least worksand give the correct answer. I used to use listof a list of a list and return a list kind of thing. Fantom allowed me to do such thing. I knowit's bad practice but I wonder if I could do the same in Pascal? Is there any limitations?The JVM which Fantom utilize allows me to do incredible things

Ex: lst1 = [1, 2, 3]
lst2 = [4, 5,6]
lst3 = [[lst1], [lst2]]
Function ftake lst3 as input and return lst4 also a list as output.

It's a simple case. There's manymore insane things  :D