11
$\begingroup$

There is new DataStructure in V 12.1. I thought at first it is similar to classical struct or record found in other languages and discussed in

Struct equivalent in Mathematica?

How to use classical data structure in Mathematica

Wikipedia entry for struct is https://en.wikipedia.org/wiki/Struct_(C_programming_language)

To make this question concrete, suppose one wants to make this struct, which in C is

struct personal_info 
{ 
   char name[50]; 
   int  age; 
   char address[50]; 
};

Then make a variable of the above type and access the struct fields using the dot notation in C. This is similar to record in Pascal and Ada also.

How to use the new DataStructure to do this? In C one will do something like the following

struct personal_info my_info;   
my_info.age = 90;
fprintf("my age is %d",my_info.age);

I could not find one basic example in the help pages on how to actually use this new DataStructure. I also looked at Wolfram blog on this, where he says

https://writings.stephenwolfram.com/2020/03/in-less-than-a-year-so-much-new-launching-version-12-1-of-wolfram-language-mathematica/

Think about all those data structures that get mentioned in textbooks, papers, libraries, etc. Our goal is to have all of them seamlessly usable directly in the Wolfram Language, and accessible in compiled code, etc. Of course it’s huge that we already have a universal “data structure”

But following Wolfram's examples on his blog, I still do not know how to make such a simple struct using Datastruct (I am not looking at queues or linked list, or other advanced structs, just the basic and most common struct).

So I tried few random things (when all else fails, I switch to the trial and error method)

ds = CreateDataStructure["DynamicArray"]
ds["Append", {"name" -> "doe joe", "age" -> 90, 
  "Address" -> "POBox 100, main street, California"}]; 

I used DynamicArray above, as I did not know what else to pick for classical struct.

But I do not think this is right at all, as I do not know now how to obtain say the name back.

Ofcourse one can use Association for this now in Mathematica, but I thought the new DataStructure will do this a little easier.

Question is: How to use new DataStructure similar to how it is used in C?

$\endgroup$
10
  • 2
    $\begingroup$ The new DataStructure stuff is really an algorithms-oriented development. They're intend to make your code run faster and struct is already fast enough using Association and some object-oriented programming on top of that. Unless you're working with compiled code, there's no real reason to have a struct that I can see (in that case it would just make it a bit easier to implement things, I figure). $\endgroup$
    – b3m2a1
    Mar 18, 2020 at 23:06
  • 1
    $\begingroup$ @swish Yeah that's a use case I can definitely foresee being important (if not necessary). I'm merely pointing out that if you're not working with FunctionCompile and friends there's gonna be no benefit to having it be a bespoke DataStructure $\endgroup$
    – b3m2a1
    Mar 18, 2020 at 23:18
  • 1
    $\begingroup$ @swish NumericArray is a mostly a transfer format at this point that has special use in compiled code and friends. PackedArray is more relevant for Mathematica programming. $\endgroup$
    – b3m2a1
    Mar 18, 2020 at 23:30
  • 2
    $\begingroup$ struct guarantee that sequencial chunks of memory is used for storage in your RAM for components and thus data sizes need to be known beforehand. Does Association keep the data in sequencial chunks of memory at the time of initialization at the very least? Also the moment you try to replace an in-between item with larger size Association doesn't complain. Probably it stores it somewhere else and keeps track of pointers like a linked list. So Association might not really be a true struct down at memory level. $\endgroup$
    – user13892
    Mar 18, 2020 at 23:32
  • 3
    $\begingroup$ @user13892 No I can promise you Association is not implemented as a struct. It's not memory optimizing because that's not what top-level code does... That's why we have FunctionCompile. Low-level features should enter where low-level features are relevant. The only place where we have an awkward intermediate is PackedArray. $\endgroup$
    – b3m2a1
    Mar 18, 2020 at 23:40

1 Answer 1

5
$\begingroup$

I think that the best match to what you are discussing is an association, e.g.

personalinfo = Association["name" -> "Fred", "age" -> 90]
(* <|"name" -> "Fred", "age" -> 90|> *)

personalinfo["name"]
(* "Fred" *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.