Hash join: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: update) |
(Added EchoLisp) |
||
Line 396: | Line 396: | ||
</pre> |
</pre> |
||
=={{header|EchoLisp}}== |
|||
Since this is a real, professional application, we build the hash tables in permanent (local) storage. |
|||
<lang lisp> |
|||
(define ages '((27 "Jonah") (18 "Alan") (28 "Glory") (18 "Popeye") (28 "Alan"))) |
|||
(define nemesis '(("Jonah" "Whales") ("Jonah" "Spiders") ("Alan" "Ghosts") ("Alan" "Zombies") ("Glory" "Buffy"))) |
|||
;; table: table name |
|||
;; source : input list |
|||
;; key-proc : procedure returning the join value ('name' in this task) |
|||
(define (table-hash table source key-proc ) |
|||
(local-make-store table) |
|||
(for ((r source)) |
|||
(local-put-value |
|||
(key-proc r) |
|||
(append (list r) (local-get-value (key-proc r) table)) table))) |
|||
;; build the two tables |
|||
(define-syntax-rule (second record) (cadr record)) |
|||
(define (key-name-age record) (second record)) |
|||
(table-hash 'AGES ages key-name-age) |
|||
(define (key-nemesis-name record) (first record)) |
|||
(table-hash 'NEMESIS nemesis key-nemesis-name) |
|||
;; join |
|||
(for* ((k (local-keys 'AGES)) |
|||
(a (local-get-value k 'AGES)) |
|||
(n (local-get-value k 'NEMESIS))) |
|||
(writeln a n)) |
|||
</lang> |
|||
{{out}} |
|||
<lang lisp> |
|||
(28 "Alan") ("Alan" "Zombies") |
|||
(28 "Alan") ("Alan" "Ghosts") |
|||
(18 "Alan") ("Alan" "Zombies") |
|||
(18 "Alan") ("Alan" "Ghosts") |
|||
(28 "Glory") ("Glory" "Buffy") |
|||
(27 "Jonah") ("Jonah" "Spiders") |
|||
(27 "Jonah") ("Jonah" "Whales") |
|||
</lang> |
|||
=={{header|ECL}}== |
=={{header|ECL}}== |