%% --- %% Excerpted from "Programming Erlang", %% published by The Pragmatic Bookshelf. %% Copyrights apply to this code. It may not be used to create training material, %% courses, books, articles, and the like. Contact us if you are in doubt. %% We make no guarantees that this code is fit for any purpose. %% Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information. %%--- -module(test_mapreduce). -compile(export_all). -import(lists, [reverse/1, sort/1]). tests([]) -> test(). test() -> wc_dir("."). wc_dir(Dir) -> F1 = fun generate_words/2, F2 = fun count_words/3, Files = lib_find:files(Dir, "*.erl", false), L1 = phofs:mapreduce(F1, F2, [], Files), reverse(sort(L1)). generate_words(Pid, File) -> F = fun(Word) -> Pid ! {Word, 1} end, foreachWordInFile(File, F). count_words(Key, Vals, A) -> [{length(Vals), Key}|A]. foreachWordInFile(File, F) -> case file:read_file(File) of {ok, Bin} -> foreachWordInString(binary_to_list(Bin), F); _ -> void end. foreachWordInString(Str, F) -> case get_word(Str) of no -> void; {Word, Str1} -> F(Word), foreachWordInString(Str1, F) end. isWordChar(X) when $A=< X, X=<$Z -> true; isWordChar(X) when $0=< X, X=<$9 -> true; isWordChar(X) when $a=< X, X=<$z -> true; isWordChar(_) -> false. get_word([H|T]) -> case isWordChar(H) of true -> collect_word(T, [H]); false -> get_word(T) end; get_word([]) -> no. collect_word([H|T]=All, L) -> case isWordChar(H) of true -> collect_word(T, [H|L]); false -> {reverse(L), All} end; collect_word([], L) -> {reverse(L), []}.