Arc Forumnew | comments | leaders | submitlogin
Ask Arc: inst or filltbl?
1 point by adm 3851 days ago | 7 comments
Can we have variant of 'inst which will take 'keys and 'vals lists as an argument to generate a hashtable? Or some other routine? like 'filltbl; discussed here:http://arclanguage.org/item?id=10026

I am writing some code to parse a csv file, and convert that into some other format. To represent a csv record, I'll use hashtable, but I couldn't find any convenient way to create a hashtable. Am I missing anything here? thanks.



1 point by CatDancer 3851 days ago | link

Have you tried listtab?

-----

1 point by thaddeus 3851 days ago | link

I think adm wants to create a named table. Correct me if I am wrong adm.

otherwise rntz's function, in the post, already returns a table:

  (def filltbl (tbl keys vals)
      (or= tbl (table))
      (map (fn (k v) (= tbl.k v)) keys vals)
      tbl)

-----

2 points by rntz 3851 days ago | link

Actually, there is already a function 'fill-table - I don't know how I missed it - that does most of what's wanted.

    (def filltbl (tbl keys vals)
      (fill-table (or tbl (table)) (mappend list keys vals))))
However, I honestly don't think this is a useful or general enough function to go in arc.arc, especially as we already have 'fill-table, 'listtab, etc.

As for "creating a named table", er:

    (= foo (table)) ; I'm unclear on what's difficult about creating a table.

-----

1 point by thaddeus 3851 days ago | link

I think the difficult part is passing in an arbitrary name to a function, for which the tables 'name' becomes set to, but I am just guessing that's what he wants...

and I agree I don't see a whole lot of need for the function to be part of the arc release..., since there are probably better ways to accomplish the same end result.... a good example of which is news.arc's storage of story records.

-----

1 point by fallintothis 3851 days ago | link

I think the difficult part is passing in an arbitrary name to a function, for which the tables 'name' becomes set to, but I am just guessing that's what he wants...

I rather doubt that's the issue, as it's easy to do with a macro.

  arc> (mac filltbl (tbl keys vals)
         `(= ,tbl (fill-table (table) (mappend list ,keys ,vals))))
  #3(tagged mac #<procedure: filltbl>)
  arc> (filltbl blah '(a b c) '(1 2 3))
  #hash((c . 3) (a . 1) (b . 2))
  arc> blah
  #hash((c . 3) (a . 1) (b . 2))

I think it's just a request for a convenience function to zip together a list of keys with a list of values into a table, since the existing methods for creating tables center around having alternating key/value pairs.

-----

1 point by thaddeus 3851 days ago | link

Agreed, it was only a wild guess given he was already given a good solution, yet...

   "Am I missing anything here?"
So I don't know what the deal is, but I don't plan to spend any time thinking about it :)

-----

1 point by adm 3840 days ago | link

only difficulty was creating a list of pairs which can be used an an arg for fill-table or listtab.

  (mappend list keys vals)
is precisely what I wanted. Sorry for the caused confusion.

-----