Hash join: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|TXR}}: Drop @(do ...))
Line 1,737: Line 1,737:
Generic hash join. Arguments <code>left-key</code> and <code>right-key</code> are functions applied to the elements of the <code>left</code> and <code>right</code> sequences to retrieve the join key.
Generic hash join. Arguments <code>left-key</code> and <code>right-key</code> are functions applied to the elements of the <code>left</code> and <code>right</code> sequences to retrieve the join key.


<lang txrlisp>(defvar age-name '((27 Jonah)
<lang txr>@(do
(18 Alan)
(defvar age-name '((27 Jonah)
(18 Alan)
(28 Glory)
(28 Glory)
(18 Popeye)
(18 Popeye)
(28 Alan)))
(28 Alan)))


(defvar nemesis-name '((Jonah Whales)
(defvar nemesis-name '((Jonah Whales)
(Jonah Spiders)
(Jonah Spiders)
(Alan Ghosts)
(Alan Ghosts)
(Alan Zombies)
(Alan Zombies)
(Glory Buffy)))
(Glory Buffy)))


(defun hash-join (left left-key right right-key)
(defun hash-join (left left-key right right-key)
(let ((join-hash [group-by left-key left])) ;; hash phase
(let ((join-hash [group-by left-key left])) ;; hash phase
(append-each ((r-entry right)) ;; join phase
(append-each ((r-entry right)) ;; join phase
(collect-each ((l-entry [join-hash [right-key r-entry]]))
(collect-each ((l-entry [join-hash [right-key r-entry]]))
^(,l-entry ,r-entry)))))
^(,l-entry ,r-entry)))))


(format t "~s\n" [hash-join age-name second nemesis-name first]))
(format t "~s\n" [hash-join age-name second nemesis-name first])</lang>
</lang>


{{out}}
{{out}}


<pre>$ txr hash-join.txr
<pre>$ txr hash-join.tl
(((27 Jonah) (Jonah Whales)) ((27 Jonah) (Jonah Spiders)) ((18 Alan) (Alan Ghosts)) ((28 Alan) (Alan Ghosts)) ((18 Alan) (Alan Zombies)) ((28 Alan) (Alan Zombies)) ((28 Glory) (Glory Buffy)))</pre>
(((27 Jonah) (Jonah Whales)) ((27 Jonah) (Jonah Spiders)) ((18 Alan) (Alan Ghosts)) ((28 Alan) (Alan Ghosts)) ((18 Alan) (Alan Zombies)) ((28 Alan) (Alan Zombies)) ((28 Glory) (Glory Buffy)))</pre>