Hash join: Difference between revisions

Go solution
(clean up verbiage a bit)
(Go solution)
Line 22:
 
Task: implement the Hash Join algorithm and show the result of joining two tables with it.
 
=={{header|Go}}==
<lang go>package main
 
import "fmt"
 
func main() {
tableA := []struct {
value int
key string
}{
{27, "Jonah"}, {18, "Alan"}, {28, "Glory"}, {18, "Popeye"},
{28, "Alan"},
}
tableB := []struct {
key string
value string
}{
{"Jonah", "Whales"}, {"Jonah", "Spiders"},
{"Alan", "Ghosts"}, {"Alan", "Zombies"}, {"Glory", "Buffy"},
}
// hash phase
h := map[string][]int{}
for i, r := range tableA {
h[r.key] = append(h[r.key], i)
}
// join phase
for b := range tableB {
for _, a := range h[tableB[b].key] {
fmt.Println(tableA[a], tableB[b])
}
}
}</lang>
{{out}}
<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>
 
=={{header|Haskell}}==
Line 94 ⟶ 137:
((3,"Glory"),("Glory","Buffy"))
</pre>
 
=={{header|Perl 6}}==
<lang perl6>my @A = [1, "Jonah"],
1,707

edits