Hash join: Difference between revisions

1,468 bytes added ,  10 years ago
J
(→‎{{header|Tcl}}: Incorrect: Missing rows)
(J)
Line 441:
((3,"Glory"),("Glory","Buffy"))
</pre>
 
=={{header|J}}==
 
Data:
 
<lang J>table1=: ;:;._2(0 :0)
27 Jonah
18 Alan
28 Glory
18 Popeye
28 Alan
)
 
table2=: ;:;._2(0 :0)
Jonah Whales
Jonah Spiders
Alan Ghosts
Alan Zombies
Glory Buffy
)</lang>
 
The task does not specify the hash function to use, so we'll use an identity function. But [[SHA-1]] could be used instead, with a little more work (you'd need to convert the name into the bit vector needed by the SHA-1 interface). Practically speaking, though, the only benefit of SHA-1 in this context would be to slow down the join.
 
Implementation:
 
<lang J>hash=: ]
clean=: #~ 0 -.@e."1 #&>
dojoin=:3 :0
c1=. {.{.y
c2=. (1 {"1 y) -. a:
c3=. (2 {"1 y) -. a:
c1,"1 >{c2,&<c3
)
 
JOIN=: clean ;,/each(hash@{."1 <@dojoin/. ]) (1 1 0&#inv@|."1 table1), 1 0 1#inv"1 table2</lang>
 
Result:
 
<lang J> JOIN
┌─────┬──┬───────┐
│Jonah│27│Whales │
├─────┼──┼───────┤
│Jonah│27│Spiders│
├─────┼──┼───────┤
│Alan │18│Ghosts │
├─────┼──┼───────┤
│Alan │18│Zombies│
├─────┼──┼───────┤
│Alan │28│Ghosts │
├─────┼──┼───────┤
│Alan │28│Zombies│
├─────┼──┼───────┤
│Glory│28│Buffy │
└─────┴──┴───────┘</lang>
 
 
=={{header|OCaml}}==
6,962

edits