So you want to destructure some hash tables, huh?
There are many ways to do that. I show some, which mostly use one of:
- xht's
h-let
- dash's
-let
- s's
s-format
In the examples that follow, flat hash tables are represented with xht's h*, which evaluates to the native format, whereas 2D hash tables are shown as Org tables (which are easier to visualize) — then converted with h<-orgtbl.
All examples return the exact same string: "Alice uses Emacs!"
1D hash tables
Longer: a regular lookup
(let* ((htbl (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano")) (Alice (h-get htbl "Alice"))) (format "%s uses %s!" "Alice" Alice))
Simpler: using XHT's h-let
(let ((htbl (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano"))) (h-let htbl (format "%s uses %s!" "Alice" .Alice)))
Or: using s-format
(let ((htbl (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano"))) (s-format "Alice uses ${Alice}!" 'gethash htbl))
Or: using Dash's -let
Autobinding
(-let* ((htbl (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano")) ((&hash "Alice") htbl)) (format "%s uses %s!" "Alice" Alice))
Binding 'editor
using format
(-let* ((htbl (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano")) ((&hash "Alice" editor) htbl)) (format "%s uses %s!" "Alice" editor))
using s-lex-format
(-let* ((htbl (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano")) ((&hash "Alice" editor) htbl)) (s-lex-format "Alice uses ${editor}!"))
or:
(-let [(&hash "Alice" editor) (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano")] (s-lex-format "Alice uses ${editor}!"))
or:
(--> (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano") (-let [(&hash "Alice" editor) it] (s-lex-format "Alice uses ${editor}!")))
or:
(--> (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano") (-let [editor (h-get it "Alice")] (s-lex-format "Alice uses ${editor}!")))
or:
(--> (h* "Alice" "Emacs" "Bob" "Vim" "Emily" "Nano") (let ((editor (h-get it "Alice"))) (s-lex-format "Alice uses ${editor}!")))
2D hash tables
Longer: a regular lookup
(let* ((orgtbl "| id | name | age | editor | |----+-------+-----+--------| | 01 | Alice | 42 | Emacs | | 02 | Bob | 30 | Vim | | 03 | Emily | 21 | Nano |") (htbl (h<-orgtbl orgtbl)) (name (h-get* htbl "01" "name")) (editor (h-get* htbl "01" "editor"))) (format "%s uses %s!" name editor))
Simpler: using XHT's h-let
(let* ((orgtbl "| id | name | age | editor | |----+-------+-----+--------| | 01 | Alice | 42 | Emacs | | 02 | Bob | 30 | Vim | | 03 | Emily | 21 | Nano |") (htbl (h<-orgtbl orgtbl))) (h-let htbl (format "%s uses %s!" .01.name .01.editor)))
Or: using s-format and h-get
(let* ((orgtbl "| id | name | age | editor | |----+-------+-----+--------| | 01 | Alice | 42 | Emacs | | 02 | Bob | 30 | Vim | | 03 | Emily | 21 | Nano |") (htbl (h<-orgtbl orgtbl))) (s-format "${name} uses ${editor}!" 'gethash (h-get htbl "01")))
Or: using Dash's -let
Binding 'name and 'editor
using format
(-let* ((orgtbl "| id | name | age | editor | |----+-------+-----+--------| | 01 | Alice | 42 | Emacs | | 02 | Bob | 30 | Vim | | 03 | Emily | 21 | Nano |") (htbl (h<-orgtbl orgtbl)) ((&hash "01" (&hash "name" "editor")) htbl)) (format "%s uses %s!" name editor))
using s-lex-format
(--> "| id | name | age | editor | |----+-------+-----+--------| | 01 | Alice | 42 | Emacs | | 02 | Bob | 30 | Vim | | 03 | Emily | 21 | Nano |" h<-orgtbl (-let [(&hash "01" (&hash "name" "editor")) it] (s-lex-format "${name} uses ${editor}!")))