Created
May 23, 2020 14:13
-
-
Save fjpse/a873bd083a1eb993d024402cc9dd4f82 to your computer and use it in GitHub Desktop.
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
-module(process). | |
-export([ | |
process_file/2, | |
show_lines/1 | |
]). | |
%% | |
%% show_lines(LineList) -> ok | |
%% | |
%% Print out the Lines | |
%% | |
show_lines([]) -> ok; | |
show_lines([Line|LineList]) -> | |
io:format("~s~n", [Line]), | |
show_lines(LineList). | |
%% | |
%% process_file(FileName, LineLength) -> LineList | |
%% | |
%% This function reads the file FileName and returns a list of lines, | |
%% each line with a maximum length of LineLength. | |
%% | |
process_file(FileName, LineLength) -> | |
Queue = read_from_file(FileName), | |
get_lines(Queue, LineLength). | |
%% | |
%% get_lines(Queue, Length) -> LineList | |
%% | |
%% This functions processes the Queue of words and generates | |
%% a list of lines with a maximum length of Length. | |
%% | |
get_lines(Queue, LineLength) -> get_lines(Queue, LineLength, [], queue:new()). | |
%% | |
%% get_lines(Queue, Length, Line, Lines) -> LineList | |
%% | |
%% This function generates a Line of maximum length Length | |
%% extraction words from the Queue and concatenating it with the Line. | |
%% When the maximum length is reached, the Line is inserted in the queue Lines. | |
%% When there is no more words in the Queue, the queue of lines is | |
%% transformed to a list of lines and returned. | |
%% | |
%% NOTE: special case when the current line is empty | |
%% | |
get_lines(Queue, Length, [], Lines) -> | |
case queue:peek(Queue) of | |
{value, Word} when length(Word) >= Length -> | |
get_lines(queue:drop(Queue), Length, [], queue:in(Word, Lines)); | |
{value, Word} -> | |
get_lines(queue:drop(Queue), Length, Word, Lines); | |
empty -> | |
queue:to_list(Lines) | |
end; | |
get_lines(Queue, Length, Line, Lines) -> | |
case queue:peek(Queue) of | |
{value, Word} when length(Line) + length(Word) < Length -> | |
get_lines(queue:drop(Queue), Length, unicode:characters_to_list([Line, " ", Word]), Lines); | |
{value, _Word} -> | |
get_lines | |
(Queue, Length, [], queue:in(Line, Lines)); | |
empty -> | |
queue:to_list(queue:in(Line, Lines)) | |
end. | |
%% | |
%% read_from_line(FileName) -> Queue | |
%% | |
%% This function reads the file FileName | |
%% and returns its content as a queue of words | |
%% | |
read_from_file(FileName) -> | |
{ok, File} = file:open(FileName, [read]), | |
Queue = read_lines(File, queue:new()), | |
file:close(File), | |
Queue. | |
%% | |
%% read_lines(File, Queue) -> Queue | |
%% | |
%% This function reads a new line from File, | |
%% split it into words and inserts the words in the Queue, | |
%% returning it. | |
%% | |
read_lines(File, Queue) -> | |
case io:get_line(File,"") of | |
eof -> | |
Queue; | |
Line -> | |
Words = queue:from_list(string:lexemes(Line, " \r\n")), | |
read_lines(File, queue:join(Queue, Words)) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice usage of
queue
here :)