Hash join: Difference between revisions

rearranges in order of the language.
(rearranges in order of the language.)
Line 362:
[ [ 18 "Alan" ] [ "Alan" "Zombies" ] ]
[ [ 28 "Glory" ] [ "Glory" "Buffy" ] ]
</pre>
 
=={{header|Erlang}}==
<lang Erlang>
-module( hash_join ).
 
-export( [task/0] ).
 
task() ->
Table_1 = [{27, "Jonah"}, {18, "Alan"}, {28, "Glory"}, {18, "Popeye"}, {28, "Alan"}],
Table_2 = [{"Jonah", "Whales"}, {"Jonah", "Spiders"}, {"Alan", "Ghosts"}, {"Alan", "Zombies"}, {"Glory", "Buffy"}],
Dict = lists:foldl( fun dict_append/2, dict:new(), Table_1 ),
lists:flatten( [dict_find( X, Dict ) || X <- Table_2] ).
 
 
dict_append( {Key, Value}, Acc ) -> dict:append( Value, {Key, Value}, Acc ).
 
dict_find( {Key, Value}, Dict ) -> dict_find( dict:find(Key, Dict), Key, Value ).
 
dict_find( error, _Key, _Value ) -> [];
dict_find( {ok, Values}, Key, Value ) -> [{X, {Key, Value}} || X <- Values].
</lang>
{{out}}
<pre>
15> hash_join:task().
[{{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>
 
Line 475 ⟶ 443:
 
</lang>
 
=={{header|Erlang}}==
<lang Erlang>
-module( hash_join ).
 
-export( [task/0] ).
 
task() ->
Table_1 = [{27, "Jonah"}, {18, "Alan"}, {28, "Glory"}, {18, "Popeye"}, {28, "Alan"}],
Table_2 = [{"Jonah", "Whales"}, {"Jonah", "Spiders"}, {"Alan", "Ghosts"}, {"Alan", "Zombies"}, {"Glory", "Buffy"}],
Dict = lists:foldl( fun dict_append/2, dict:new(), Table_1 ),
lists:flatten( [dict_find( X, Dict ) || X <- Table_2] ).
 
 
dict_append( {Key, Value}, Acc ) -> dict:append( Value, {Key, Value}, Acc ).
 
dict_find( {Key, Value}, Dict ) -> dict_find( dict:find(Key, Dict), Key, Value ).
 
dict_find( error, _Key, _Value ) -> [];
dict_find( {ok, Values}, Key, Value ) -> [{X, {Key, Value}} || X <- Values].
</lang>
{{out}}
<pre>
15> hash_join:task().
[{{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|Go}}==
Line 981:
28 4 Glory 35 Buffy
</pre>
 
=={{header|Oberon-2}}==
Works with oo2c version 2
Line 1,089 ⟶ 1,090:
28 Glory Buffy
</pre>
 
=={{header|OCaml}}==
This exploits the fact that Hashtbl implements a hash table that can store multiple values for a key, for an especially simple solution:
Line 1,474 ⟶ 1,476:
[[28, "Glory"], ["Glory", "Buffy"]]
</pre>
 
=={{header|Scala}}==
<lang Scala>def join[Type](left: Seq[Seq[Type]], right: Seq[Seq[Type]]) = {
Line 1,502 ⟶ 1,505:
List(28, Alan, Ghosts)
List(28, Alan, Zombies)</pre>
 
 
=={{header|Scheme}}==
Anonymous user