Jump to content

Hash join: Difference between revisions

Added Elixir
(rearranges in order of the language.)
(Added Elixir)
Line 441:
27 Jonah Spiders
*/
 
</lang>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<lang elixir>defmodule Hash do
def join(table1, index1, table2, index2) do
h = Enum.group_by(table1, fn s -> elem(s, index1) end)
Enum.flat_map(table2, fn r ->
Enum.map(h[elem(r, index2)], fn s -> {s, r} end)
end)
end
end
 
table1 = [{27, "Jonah"},
{18, "Alan"},
{28, "Glory"},
{18, "Popeye"},
{28, "Alan"}]
table2 = [{"Jonah", "Whales"},
{"Jonah", "Spiders"},
{"Alan", "Ghosts"},
{"Alan", "Zombies"},
{"Glory", "Buffy"}]
Hash.join(table1, 1, table2, 0) |> Enum.each(&IO.inspect &1)</lang>
 
{{out}}
<pre>
{{27, "Jonah"}, {"Jonah", "Whales"}}
{{27, "Jonah"}, {"Jonah", "Spiders"}}
{{28, "Alan"}, {"Alan", "Ghosts"}}
{{18, "Alan"}, {"Alan", "Ghosts"}}
{{28, "Alan"}, {"Alan", "Zombies"}}
{{18, "Alan"}, {"Alan", "Zombies"}}
{{28, "Glory"}, {"Glory", "Buffy"}}
</pre>
 
=={{header|Erlang}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.