Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2955 > unrolled thread

Question about function construction

Started byZiYuan Lin <ziyuang@gmail.com>
First post2011-06-05 11:04 +0000
Last post2011-06-06 10:23 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Question about function construction ZiYuan Lin <ziyuang@gmail.com> - 2011-06-05 11:04 +0000
    Re: Question about function construction Albert Retey <awnl@gmx-topmail.de> - 2011-06-06 10:23 +0000

#2955 — Question about function construction

FromZiYuan Lin <ziyuang@gmail.com>
Date2011-06-05 11:04 +0000
SubjectQuestion about function construction
Message-ID<isfnrv$btm$1@smc.vnet.net>
Hi, there.
  I want to construct a function serving as a hash table. This is how I make it:

  ConstructHashtable[] := 
   Module[{pairs = {{"key1", "value1"}, {"key2", "value2"}, {"key3", 
        "value3"}, {"key4", "value4"}}}, Clear[Hashtable]; 
    Hashtable[key_?StringQ] := (Hashtable[#[[1]]] = #[[2]]) & /@ pairs;]

And then I run the code above together with a test:

  ConstructHashtable[]; Hashtable["key1"]

Yet it returns all values:

  {"value1", "value2", "value3", "value4"}

But if I run it again:

  Hashtable["key1"]

It will get normal:

  "value1"

What is the reason for this? Thank you~

[toc] | [next] | [standalone]


#2968

FromAlbert Retey <awnl@gmx-topmail.de>
Date2011-06-06 10:23 +0000
Message-ID<isi9ql$lrv$1@smc.vnet.net>
In reply to#2955
Am 05.06.2011 13:04, schrieb ZiYuan Lin:
> Hi, there.
>    I want to construct a function serving as a hash table. This is how I make it:
>
>    ConstructHashtable[] :=
>     Module[{pairs = {{"key1", "value1"}, {"key2", "value2"}, {"key3",
>          "value3"}, {"key4", "value4"}}}, Clear[Hashtable];
>      Hashtable[key_?StringQ] := (Hashtable[#[[1]]] = #[[2]])&  /@ pairs;]
>
> And then I run the code above together with a test:
>
>    ConstructHashtable[]; Hashtable["key1"]
>
> Yet it returns all values:
>
>    {"value1", "value2", "value3", "value4"}
>
> But if I run it again:
>
>    Hashtable["key1"]
>
> It will get normal:
>
>    "value1"
>
> What is the reason for this? Thank you~

the reason is that the first call to Hashtable will call the code that 
fills the hashtable and that returns the list with the values. Only 
after that for the defined keys the corresponding values are returned. 
Your implementation has another "feature" that I hardly can believe is 
intended: for every key that is not in the hashtable yet, it will 
recreate the entries and again return the list of values. Actually I 
think you want something like this:

ConstructHashtable[] :=
  Module[{pairs = {{"key1", "value1"}, {"key2", "value2"}, {"key3",
       "value3"}, {"key4", "value4"}}}, Clear[Hashtable];
   (Hashtable[#[[1]]] = #[[2]]) & /@ pairs;
   ]

I think you also probably want to add a rule for those keys that are not 
available, and I don't see an advantage to define pairs in the first 
place. This is what I probably would do:

ConstructHashtable[] := (
   Clear[Hashtable];
   Hashtable[___] = Missing["NotAvailable"];
   Scan[(Hashtable[#[[1]]] = #[[2]]) &,
    {{"key1", "value1"}, {"key2", "value2"},
     {"key3", "value3"}, {"key4", "value4"}}
    ]
   )


hth,

albert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web