Message Boards Message Boards

GROUPS:

[Solved] How to create and return a HashTable from a Function?

Posted 1 year ago
1959 Views
|
6 Replies
|
3 Total Likes
|

In Mathematica 12.2, I'd like to have a user-defined function create a data structure, initialize it, and return it. I'm failing at the 1st step, so instead creating it beforehand with the function performing the initialization:

getCommodityHash[hash_] :=
  hash["Insert", #[[1]] -> #[[2]]] & /@ {{"somekey", "somevalue"}};
...
spamHash = CreateDataStructure["HashTable"];
getCommodityHash[spamHash];

I'd prefer to have this calling sequence though:

spamHash = getCommodityHash[];

How is this done?

POSTED BY: Richard Frost
6 Replies

Richard,

It is not clear from your post what you want. First, its a bad idea to name functions or variables with initial capital letters -- you can inadvertently conflict with Mathematica internals. For example, you name a variable "Hash" but Hash is an internal Mathematica function. Its not clear to me if you are trying to use that function or want to pass some other function??

My guess is that you are asking about "memoization" -- making a function that remembers previous values. See the documentation here. Using this construction you can build a hash table as you call your function.

To be more specific on how to do this, you would need to provide more details of what you want.

Regards,

Neil

POSTED BY: Neil Singer

Hi Neil, Thank you for the heads up about using Hash as a local variable. I've updated my example accordingly. My quest remains:

I'd like to have a user-defined function create a data structure, initialize it, and return it.

POSTED BY: Richard Frost

Did you look at my link? I think that does what you want.

you return and remember the data you compute. your structure would be:

CreateDataStructure[value_] := CreateDataStructure[value] = doSomething[value];

Regards

POSTED BY: Neil Singer

Neil, thank you but I've no need for a function with memory.

I have a suite of notebooks that utilize a mutual package file. At runtime, each notebook needs a set of key-value pairs. Now I could simply return them as a list or association but a hashtable is more suitable. To my knowledge, a hashtable cannot be created and initialized in the same Built-in function. So my question is: how to achieve this in a user-defined function. I attempted it using With[] but the compiler complains about it.

POSTED BY: Richard Frost

To my knowledge, a hashtable cannot be created and initialized in the same Built-in function.

Something like this?

getHashTable[] := Module[
    {ht = CreateDataStructure["HashTable"]},
    ht["Insert", "Bob" -> 1];
    ht["Insert", "Alice" -> 2];
    ht
]

Then you can use it as normal:

In[9]:= hashTable = getHashTable[];

In[10]:= hashTable["Lookup", "Bob"]

Out[10]= 1

In[11]:= hashTable["Lookup", "Carol"]

Out[11]= Missing["KeyAbsent", "Carol"]
POSTED BY: Jason Biggs

Oh that's perfect, thank you.

POSTED BY: Richard Frost
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract