Word wrap: Difference between revisions
Content added Content deleted
(→{{header|LFE}}: Tightened up code a bit.) |
(→{{header|LFE}}: Added regex implementation) |
||
Line 1,339: | Line 1,339: | ||
=={{header|LFE}}== |
=={{header|LFE}}== |
||
=== Naive Implementation === |
|||
Paste the following code into the LFE REPL: |
|||
{{trans|Erlang}} |
|||
<lang Lisp> |
<lang Lisp> |
||
Line 1,367: | Line 1,369: | ||
((word `#(,max ,line-len ,line ,acc)) |
((word `#(,max ,line-len ,line ,acc)) |
||
`#(,max ,(+ line-len 1 (length word)) ,(++ line " " word) ,acc))) |
`#(,max ,(+ line-len 1 (length word)) ,(++ line " " word) ,acc))) |
||
</lang> |
|||
=== Regex Implementation === |
|||
<lang lisp> |
|||
(defun make-regex-str (max-len) |
|||
(++ "(.{1," (integer_to_list max-len) "}|\\S{" |
|||
(integer_to_list (+ max-len 1)) ",})(?:\\s[^\\S\\r\\n]*|\\Z)")) |
|||
(defun wrap-text (text max-len) |
|||
(let ((find-patt (make-regex-str max-len)) |
|||
(replace-patt "\\1\\\n")) |
|||
(re:replace text find-patt replace-patt |
|||
'(global #(return list))))) |
|||
</lang> |
</lang> |
||
Line 1,375: | Line 1,391: | ||
"The basic task is to wrap a paragraph of text in a simple way in your language. If there is a way to do this that is built-in, trivial, or " |
"The basic task is to wrap a paragraph of text in a simple way in your language. If there is a way to do this that is built-in, trivial, or " |
||
"provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia.") |
"provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia.") |
||
> (io:format |
> (io:format (wrap-text text 80)) |
||
</lang> |
</lang> |
||
<pre> |
<pre> |
||
Even today, with proportional fonts and complex layouts, there are still cases |
Even today, with proportional fonts and complex layouts, there are still cases |
||
where you need to wrap text at a specified column. The basic task is to wrap a |
where you need to wrap text at a specified column. The basic task is to wrap a |
||
paragraph of text in a simple way in your language. If there is a way to do |
paragraph of text in a simple way in your language. If there is a way to do this |
||
that is built-in, trivial, or provided in a standard library, show that. |
|||
Otherwise implement the minimum length greedy algorithm from Wikipedia. |
Otherwise implement the minimum length greedy algorithm from Wikipedia. |
||
ok |
ok |
||
</pre> |
</pre> |
||
<lang lisp> |
<lang lisp> |
||
> (io:format |
> (io:format (wrap-text text 50)) |
||
</lang> |
</lang> |
||
<pre> |
<pre> |
||
Line 1,392: | Line 1,408: | ||
layouts, there are still cases where you need to |
layouts, there are still cases where you need to |
||
wrap text at a specified column. The basic task is |
wrap text at a specified column. The basic task is |
||
to wrap a paragraph of text in a simple way in |
to wrap a paragraph of text in a simple way in |
||
language. If there is a way to do this that |
your language. If there is a way to do this that |
||
built-in, trivial, or provided in a standard |
is built-in, trivial, or provided in a standard |
||
library, show that. Otherwise implement the |
library, show that. Otherwise implement the |
||
length greedy algorithm from Wikipedia. |
minimum length greedy algorithm from Wikipedia. |
||
ok |
ok |
||
</pre> |
</pre> |