Jump to content

Word wrap: Difference between revisions

→‎{{header|LFE}}: Added regex implementation
(→‎{{header|LFE}}: Tightened up code a bit.)
(→‎{{header|LFE}}: Added regex implementation)
Line 1,339:
=={{header|LFE}}==
 
=== Naive Implementation ===
Paste the following code into the LFE REPL:
 
{{trans|Erlang}}
 
<lang Lisp>
Line 1,367 ⟶ 1,369:
((word `#(,max ,line-len ,line ,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>
 
Line 1,375 ⟶ 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 "
"provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia.")
> (io:format "~s~n" (list (wrap-text test-text) 80))
</lang>
<pre>
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
paragraph of text in a simple way in your language. If there is a way to do this
this that is built-in, trivial, or provided in a standard library, show that.
Otherwise implement the minimum length greedy algorithm from Wikipedia.
ok
</pre>
<lang lisp>
> (io:format "~s~n" (list (wrap-text test-text 50)))
</lang>
<pre>
Line 1,392 ⟶ 1,408:
layouts, there are still cases 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
your language. If there is a way to do this that is
is built-in, trivial, or provided in a standard
library, show that. Otherwise implement the minimum
minimum length greedy algorithm from Wikipedia.
ok
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.