Hash join: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(Add Swift) |
||
Line 2,958: | Line 2,958: | ||
18 Alan Zombies |
18 Alan Zombies |
||
28 Glory Buffy</pre> |
28 Glory Buffy</pre> |
||
=={{header|Swift}}== |
|||
{{trans|Rust}} |
|||
<lang swift>func hashJoin<A, B, K: Hashable>(_ first: [(K, A)], _ second: [(K, B)]) -> [(A, K, B)] { |
|||
var map = [K: [B]]() |
|||
for (key, val) in second { |
|||
map[key, default: []].append(val) |
|||
} |
|||
var res = [(A, K, B)]() |
|||
for (key, val) in first { |
|||
guard let vals = map[key] else { |
|||
continue |
|||
} |
|||
res += vals.map({ (val, key, $0) }) |
|||
} |
|||
return res |
|||
} |
|||
let t1 = [ |
|||
("Jonah", 27), |
|||
("Alan", 18), |
|||
("Glory", 28), |
|||
("Popeye", 18), |
|||
("Alan", 28) |
|||
] |
|||
let t2 = [ |
|||
("Jonah", "Whales"), |
|||
("Jonah", "Spiders"), |
|||
("Alan", "Ghosts"), |
|||
("Alan", "Zombies"), |
|||
("Glory", "Buffy") |
|||
] |
|||
print("Age | Character Name | Nemesis") |
|||
print("----|----------------|--------") |
|||
for (age, name, nemesis) in hashJoin(t1, t2) { |
|||
print("\(age) | \(name) | \(nemesis)") |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Age | Character Name | Nemesis |
|||
----|----------------|-------- |
|||
27 | Jonah | Whales |
|||
27 | Jonah | Spiders |
|||
18 | Alan | Ghosts |
|||
18 | Alan | Zombies |
|||
28 | Glory | Buffy |
|||
28 | Alan | Ghosts |
|||
28 | Alan | Zombies</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |