Created
March 2, 2012 03:00
-
-
Save built/1955231 to your computer and use it in GitHub Desktop.
Later PDXErlang intersection implementation w/ Tim. Much more performant.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(larray). | |
| -include_lib("eunit/include/eunit.hrl"). | |
| -export([intersection/2]). | |
| intersection(ListA, ListB) -> | |
| Numbers = lists:foldl( fun(N, Ranking) -> dict:update_counter(N, 1, Ranking) end , dict:new(), ListA ++ ListB), | |
| lists:usort([Unique || Unique <- dict:fetch_keys(Numbers), dict:fetch(Unique, Numbers) > 1]). | |
| %% helper functions to create big lists of random integers | |
| large_random_array(N, MaxValue) -> | |
| large_random_array(N, MaxValue, []). | |
| large_random_array(0, _, L) -> | |
| L; | |
| large_random_array(N, MaxValue, L) -> | |
| large_random_array(N - 1, MaxValue, [random:uniform(MaxValue) | L]). | |
| intersection_test_() -> | |
| ListA = lists:usort(large_random_array(100000, 1000000)), | |
| ListB = lists:usort(large_random_array(100000, 1000000)), | |
| Result = lists:usort(sets:to_list(sets:intersection(sets:from_list(ListA), sets:from_list(ListB)))), | |
| ?_assertEqual(Result, intersection(ListA,ListB)). |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More performant but much less Erlangy. Needs some pattern matching.