Last active
May 30, 2020 18:14
-
-
Save viniciusd/426b55ea3ff5ad6bdcd48b2e8795411b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
The heat bloomed in December .VB | |
as the carnival season .CV | |
kicked into gear. .LP | |
Nearly helpless with sun and glare, I avoided Rio's brilliant .RP | |
sidewalks | |
and glittering beaches, | |
panting in dark corners | |
and waiting out the inverted southern summer. .JU |
This file contains hidden or 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(text). | |
-export([filling/2, show_file_contents/1]). | |
filling(FileName, LineLength) -> | |
FileContents = get_file_contents(FileName), | |
filling(FileContents, LineLength, []). | |
filling([], _LineLength, ACC) -> lists:reverse(ACC); | |
filling(FileContents, LineLength, ACC) -> | |
{Line, Rest} = take_line(FileContents, LineLength, 0, []), | |
filling(Rest, LineLength, [Line|ACC]). | |
take_line([Word | Contents], _LineLength, _CurrLength, ACC) when Word == ".VB" -> | |
Line = string:join(lists:reverse(ACC), " "), | |
{Line, Contents}; | |
take_line([Word | Contents] , LineLength, _CurrLength, ACC) when Word == ".CV" -> | |
Line = string:join(lists:reverse(ACC), " "), | |
{center(Line, LineLength), Contents}; | |
take_line([Word | Contents], LineLength, _CurrLength, ACC) when Word == ".LP" -> | |
Line = string:join(lists:reverse(ACC), " "), | |
{left_fill(Line, LineLength), Contents}; | |
take_line([Word | Contents], LineLength, _CurrLength, ACC) when Word == ".RP" -> | |
Line = string:join(lists:reverse(ACC), " "), | |
{right_fill(Line, LineLength), Contents}; | |
take_line([Word | Contents], LineLength, CurrLength, ACC) when Word == ".JU" -> | |
RemainingLength = LineLength - CurrLength + 1, | |
Line = string:join(lists:reverse(ACC), | |
lists:duplicate(RemainingLength div (length(ACC)-1), | |
" ")), | |
{Line, Contents}; | |
take_line([Word | Rest], LineLength, CurrLength, ACC) when CurrLength + length(Word) =< LineLength -> | |
take_line(Rest, LineLength, CurrLength+length(Word)+1, [Word|ACC]); | |
take_line(Contents, _LineLength, _CurrLength, ACC) -> | |
{string:join(lists:reverse(ACC), " "), Contents}. | |
center(Line, LineLength) -> | |
left_fill(right_fill(Line, LineLength div 2), | |
LineLength div 2 + LineLength rem 2). | |
left_fill(Line, LineLength) -> | |
RemainingLength = LineLength-length(Line), | |
Line ++ | |
lists:duplicate(RemainingLength, " "). | |
right_fill(Line, LineLength) -> | |
RemainingLength = LineLength-length(Line), | |
lists:duplicate(RemainingLength, " ") ++ | |
Line. | |
% Get the contents of a text file into a list of words. | |
get_file_contents(Name) -> | |
{ok,Contents} = file:read_file(Name), | |
string:lexemes(unicode:characters_to_list(Contents), | |
" \r\n\t"). | |
% Show the contents of a list of strings. | |
% Can be used to check the results of calling get_file_contents. | |
show_file_contents([L|Ls]) -> | |
io:format("~s~n",[L]), | |
show_file_contents(Ls); | |
show_file_contents([]) -> | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment