On Dec 27, 6:24 am, "Pascal J. Bourguignon" <
p...@informatimago.com>
wrote:
> Are you dumb?
Maybe. ;-)
> How crippling is writing:
>
> (-> hash-of-hash :key1 :key2)
>
> ???
>
> (let ((hash-of-hash (make-hash-table)))
> (setf (-> hash-of-hash :key1) (make-hash-table))
> (setf (-> hash-of-hash :key3) (make-array 33 :initial-element 33))
> (setf (-> hash-of-hash :key1 :key2) 42)
> (list (-> hash-of-hash :key1 :key2)
> (-> hash-of-hash :key3 3)))
> --> (42 33)
Okay, let's play this game. Suppose you have a data file that has
thousands of records and dozens of columns, but for the sake of
brevity, let's say your data file looks like this:
1,George,Washington,Virginia,1788,2 terms
2,John,Adams,Massachusetts,1796,1 term
3,Thomas,Jefferson,Virginia,1800,2 terms
...
44,Barack,Obama,Illinois,2008,2 terms
With Perl, you would do something like this to create the hash:
my %nested_hash_of_presidents;
while (<IN>)
{
my ($key,$first,$last,$home,$year,$terms) = split $_;
$nested_hash_of_presidents{$key} = {
first => $first,
last => $last,
home => $home,
year => $year,
terms => $terms,
};
}
To write out the data, you would do something like this
(ABBREVIATE nested_hash_of_presidents as pres)
foreach my $key (keys %pres)
{
print OUT qq($key,$pres{$key}{first},...,$pres{$key}{terms}\n);
}
What I'd expect Common Lisp to have is something like this:
(defparameter *nested-hash-of-presidents*)
(do ((line (read-line STREAM)))
(('eol nil))
(multiple-value-bind (key first last home year terms) (parse-csv
line))
(setf *nested-hash-of-presidents* key
(:first first :last last :home home :year year :terms terms))))
Yeah, I know my syntax is off -- please consider this as some kind of
lispy pseudo-code.
And yes, I've decided to roll my own, with grateful thanks to some
others who have posted suggestions. This will be my first real macro,
and I'm sure that I'll have many false starts before I get it right.
But ... I'm still surprised that something like this isn't already
available. This is a common task for me and I suspect for many others,
and I'm baffled at the prospect of writing my own in 2012, fifty plus
years after the birth of Lisp.
CC.