Sha256: a8be70273a0622a5698ed2b454744661b1fb5e44fa8a0656b41649f99a900b39

Contents?: true

Size: 1.09 KB

Versions: 120

Compression:

Stored size: 1.09 KB

Contents

-module(example).

-export([add/2, contains/2, difference/2, disjoint/2, empty/1, equal/2, from_list/1, intersection/2, subset/2,
	 union/2, test_version/0]).

-record(set, {s = #{}}).

add(Elem, #set{s = S}) ->
	#set{s = maps:put(Elem, true, S)}.

contains(Elem, #set{s = S}) ->
	maps:is_key(Elem, S).

difference(#set{s = S1}, #set{s = S2}) ->
	#set{s = lists:foldl(
		fun (E, S) -> maps:remove(E, S) end, S1, maps:keys(S2))}.

disjoint(Set1, Set2) ->
	empty(intersection(Set1, Set2)).

empty(#set{s = S}) ->
	maps:size(S) =:= 0.

equal(Set1, Set2) ->
	Set1 == Set2.

from_list(List) ->
	#set{s = from_list(List, #{})}.

from_list([], Acc) -> Acc;
from_list([E|List], Acc) ->
	from_list(List, maps:put(E, true, Acc)).

intersection(#set{s = S1}, #set{s = S2}) ->
	#set{s = lists:foldl(
		fun (E, S) ->
			case maps:is_key(E, S2) of
				true -> maps:put(E, true, S);
				false -> S
			end
		end, #{}, maps:keys(S1))}.

subset(#set{s = S1}, #set{s = S2}) ->
	lists:all(
		fun (E) -> maps:is_key(E, S2) end,
		maps:keys(S1)).

union(#set{s = S1}, #set{s = S2}) ->
	#set{s = maps:merge(S1, S2)}.

test_version() -> 1.

Version data entries

120 entries across 120 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.118 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.117 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.116 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.115 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.114 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.113 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.111 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.110 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.109 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.108 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.107 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.106 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.105 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.104 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.103 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.102 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.101 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.100 tracks/erlang/exercises/custom-set/src/example.erl
trackler-2.2.1.99 tracks/erlang/exercises/custom-set/src/example.erl