From d88b7f8bcf9c509ff5d374fec9cc6054fb656657 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 25 Apr 2019 23:02:54 -0700 Subject: [PATCH] Finishing up part 1. --- pe/a.erl | 16 ++++++++++++++++ pe/b.erl | 4 ++++ pe/chapter8.erl | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 pe/a.erl create mode 100644 pe/b.erl create mode 100644 pe/chapter8.erl diff --git a/pe/a.erl b/pe/a.erl new file mode 100644 index 0000000..3449460 --- /dev/null +++ b/pe/a.erl @@ -0,0 +1,16 @@ +-module(a). +-compile(export_all). + +start(Tag) -> + spawn(fun () -> loop(Tag) end). + +loop(Tag) -> + sleep(), + Val = b:x(), + io:format("Vsn1 (~p) b:x() = ~p~n", [Tag, Val]). + loop(Tag). + +sleep() -> + receive + after 3000 -> true + end. diff --git a/pe/b.erl b/pe/b.erl new file mode 100644 index 0000000..6febccd --- /dev/null +++ b/pe/b.erl @@ -0,0 +1,4 @@ +-module(b). +-export([x/0]). + +x () -> 1. diff --git a/pe/chapter8.erl b/pe/chapter8.erl new file mode 100644 index 0000000..a6ad3ad --- /dev/null +++ b/pe/chapter8.erl @@ -0,0 +1,20 @@ +-module(chapter8). +-export([unique_funs/0]). + +%% The command code:all_loaded() returns a list of {Mod,File} pairs of +%% all modules that have been loaded into the Erlang system. Use the +%% BIF Mod:module_info() to find out about these modules. Write +%% functions to determine which module exports the most functions and +%% which function name is the most common. Write a function to find +%% all unambiguous function names, that is, function names that are +%% used in only one module. +unique_funs() -> + unique_funs(code:all_loaded(), sets:new()). + +unique_funs([], UniqueFuns) -> + {sets:size(UniqueFuns), sets:to_list(UniqueFuns)}; +unique_funs([{Mod, _File}|T], UniqueFuns) -> + Funs = lists:map(fun ({Fun, _Arity}) -> Fun end, Mod:module_info(exports)), + unique_funs(T, sets:union(UniqueFuns, sets:from_list(Funs))). + +