Advertisement
Guest User

Untitled

a guest
Aug 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.72 KB | None | 0 0
  1. -module(format).
  2. -export([fixed_line_length/2]).
  3.  
  4. %
  5. % In word processing systems it is customary for lines to be filled
  6. % and broken automatically, to enhance the appearance of the text.
  7. % Input of the form
  8. %
  9. % The heat bloomed                  in December
  10. %  as the      carnival  season
  11. %                  kicked into gear.
  12. % Nearly helpless with sun and glare, I avoided Rio's brilliant
  13. % sidewalks
  14. %  and glittering beaches,
  15. % panting in dark     corners
  16. %   and waiting out the inverted southern summer.
  17. %
  18. % would be transformed by filling to
  19. %
  20. % The heat bloomed in December as the
  21. % carnival season kicked into gear.
  22. % Nearly helpless with sun and glare,
  23. % I avoided Rio's brilliant sidewalks
  24. % and glittering beaches, panting in
  25. % dark corners and waiting out the
  26. % inverted southern summer.
  27. %
  28.  
  29. % Loads a file and returns each of its
  30. % lines (list of strings) with a constant maximum length
  31. fixed_line_length(FileName, Length) ->
  32.   Words = words_from_file(FileName),
  33.   ExistWordsLongerThanLength = lists:any(fun(WordI) -> length(WordI) + 1 > Length end, Words),
  34.   if
  35.     ExistWordsLongerThanLength ->
  36.       throw("There are some words longer than maximum line length");
  37.   true ->
  38.     format_words_in_fixed_line_length(Words, Length)
  39.   end.
  40.  
  41. % Split lines in fixed length
  42. format_words_in_fixed_line_length([WordI|Words], Length) ->
  43.     format_words_in_fixed_line_length(Words, Length, "", WordI).
  44.  
  45. format_words_in_fixed_line_length([], _, Acc, CurrLine) -> Acc ++ [CurrLine];
  46. format_words_in_fixed_line_length([WordI|Words], Length, Acc, CurrLine) ->
  47.   CurrLineLength = length(CurrLine),
  48.   WordILength = length(WordI),
  49.   if
  50.     CurrLineLength + WordILength + 1 > Length ->
  51.       format_words_in_fixed_line_length([WordI|Words], Length, Acc ++ [CurrLine], "");
  52.     true ->
  53.       format_words_in_fixed_line_length(Words, Length, Acc, string:trim(CurrLine ++ " " ++ WordI))
  54.   end.
  55.  
  56.  
  57. % Return a list with a words from the file named FileName.
  58. words_from_file(FileName) ->
  59.     FileLines = get_file_contents(FileName),
  60.     Words = lists:concat(
  61.       lists:map(
  62.         fun(FileLine) -> string:tokens(FileLine, " ") end,
  63.         FileLines
  64.       )
  65.     ),
  66.     Words.
  67.  
  68.  
  69. % Get the contents of a text file into a list of lines.
  70. % Each line has its trailing newline removed.
  71. get_file_contents(Name) ->
  72.     {ok,File} = file:open(Name,[read]),
  73.     Rev = get_all_lines(File,[]),
  74.     lists:reverse(Rev).
  75.  
  76.  
  77. % Auxiliary function for get_file_contents.
  78. % Not exported.
  79. get_all_lines(File,Partial) ->
  80.     case io:get_line(File,"") of
  81.         eof -> file:close(File),
  82.                Partial;
  83.         Line -> {Strip,_} = lists:split(length(Line)-1,Line),
  84.                 get_all_lines(File,[Strip|Partial])
  85.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement