-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from ajfAfg/memoize-in-transform_
memoize in transform
- Loading branch information
Showing
6 changed files
with
149 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
% NOTE: API resembles the module `maps`. | ||
-module(mutable_map). | ||
|
||
-export([new/0, put/3, find/2]). | ||
|
||
-export_type([t/0, key/0, value/0]). | ||
|
||
-opaque t() :: ets:table(). | ||
|
||
-type key() :: term(). | ||
-type value() :: term(). | ||
|
||
-spec new() -> t(). | ||
new() -> ets:new(?MODULE, [set, private]). | ||
|
||
-spec put(key(), value(), t()) -> true. | ||
put(Key, Value, MutableMap) -> ets:insert(MutableMap, {Key, Value}). | ||
|
||
-spec find(key(), t()) -> {ok, value()} | error. | ||
find(Key, MutableMap) -> | ||
case ets:lookup(MutableMap, Key) of | ||
[{Key, Value}] -> {ok, Value}; | ||
[] -> error | ||
end. |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
-module(dependency_graph_generator). | ||
|
||
-export([dependency_graph/0]). | ||
-export([dependency_graph/1]). | ||
|
||
-import(proper_helper, [limited_atom/0]). | ||
|
||
-include_lib("proper/include/proper.hrl"). | ||
|
||
dependency_graph() -> | ||
dependency_graph(MaxVertexNum) -> | ||
?LET(List, | ||
non_empty(list(limited_atom())), | ||
?SUCHTHAT(L, non_empty(list(limited_atom())), length(L) =< MaxVertexNum), | ||
begin | ||
Vertices = lists:uniq(List), | ||
lists:foldl(fun(From, Acc) -> | ||
Tos = my_lists:sublist_randomly(Vertices), | ||
maps:put(From, Tos, Acc) | ||
MaximumValidEdges = [{V1, V2} || V1 <- Vertices, V2 <- Vertices], | ||
% NOTE: When the number of vertices is the same as the number of edges, | ||
% it often generates a graph that are complex to compute vertex splitters, | ||
% in my experience. | ||
Edges = | ||
lists:nthtail(length(MaximumValidEdges) - length(Vertices), | ||
my_lists:shuffle(MaximumValidEdges)), | ||
lists:foldl(fun({V1, V2}, Acc) -> maps:update_with(V1, fun(Vs) -> [V2 | Vs] end, Acc) | ||
end, | ||
#{}, | ||
Vertices) | ||
maps:from_keys(Vertices, []), | ||
Edges) | ||
end). |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-module(prop_mutable_map). | ||
|
||
-compile(export_all). | ||
|
||
-import(proper_helper, [random_type/0]). | ||
|
||
-include_lib("proper/include/proper.hrl"). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% mutable_map:put/3 and mutable_map:find/2 %%% | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
prop_put_and_find1(doc) -> "The last put term can be found". | ||
|
||
prop_put_and_find1() -> | ||
?FORALL({Key, Value1, Value2}, | ||
tuple([random_type(), random_type(), random_type()]), | ||
begin | ||
MutableMap = mutable_map:new(), | ||
mutable_map:put(Key, Value1, MutableMap), | ||
mutable_map:put(Key, Value2, MutableMap), | ||
{ok, Value2} =:= mutable_map:find(Key, MutableMap) | ||
end). | ||
|
||
prop_put_and_find2(doc) -> "A term that is not put cannot be found". | ||
|
||
prop_put_and_find2() -> | ||
?FORALL(Key, | ||
random_type(), | ||
begin | ||
MutableMap = mutable_map:new(), | ||
error =:= mutable_map:find(Key, MutableMap) | ||
end). |
This file contains 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