Hi Group!
I'm in the process of learning Common Lisp, and have a quick question.
I have a string, and want its lines to be filled at (say) 80
characters. What is a good way to do that?
I came up with the following solution:
,----
| (defun fill-string (str &optional len d)
| "Return string with lines filled, optionally using LEN to set value
| of *print-right-margin*."
| (let*
| ((*print-right-margin* len)
| (s (princ-to-string (cl-ppcre:split "\\s+" str))))
| (subseq s 1 (1- (length s)))))
`----
This works, but the final (subseq s 1 (1- (length s))) is ugly. How can
I improve it?
Thanks.
--
Henrik Jegbjerg Hansen
Henrik Jegbjerg Hansen <···@freecode.dk> writes:
> Hi Group!
>
> I'm in the process of learning Common Lisp, and have a quick question.
>
> I have a string, and want its lines to be filled at (say) 80
> characters. What is a good way to do that?
>
> I came up with the following solution:
>
> ,----
> | (defun fill-string (str &optional len d)
> | "Return string with lines filled, optionally using LEN to set value
> | of *print-right-margin*."
> | (let*
> | ((*print-right-margin* len)
> | (s (princ-to-string (cl-ppcre:split "\\s+" str))))
> | (subseq s 1 (1- (length s)))))
> `----
Don't bother with these block quotes. Or if you really need, block
quote your english text, like this:
;; Hi Group!
;;
;; I'm in the process of learning Common Lisp, and have a quick question.
;;
;; I have a string, and want its lines to be filled at (say) 80
;; characters. What is a good way to do that?
;;
;; I came up with the following solution:
(defun fill-string (str &optional len d)
"Return string with lines filled, optionally using LEN to set value
of *print-right-margin*."
(let*
((*print-right-margin* len)
(s (princ-to-string (cl-ppcre:split "\\s+" str))))
(subseq s 1 (1- (length s)))))
;; This works, but the final (subseq s 1 (1- (length s))) is ugly. How can
;; I improve it?
;;
;;
;; Thanks.
> This works, but the final (subseq s 1 (1- (length s))) is ugly. How can
> I improve it?
What about:
(defun fill-string (str &optional (len 72))
"Return string with text left-justified on lines LEN wide."
(format nil (format nil "~~{~~<~~%~~1,~A:;~~A~~>~~^ ~~}" len)
(cl-ppcre:split "\\s+" str)))
--
__Pascal Bourguignon__ http://www.informatimago.com/
Pascal Bourguignon <···@informatimago.com> writes:
>> ,----
>> | (defun fill-string (str &optional len d)
>> | "Return string with lines filled, optionally using LEN to set value
>> | of *print-right-margin*."
>> | (let*
>> | ((*print-right-margin* len)
>> | (s (princ-to-string (cl-ppcre:split "\\s+" str))))
>> | (subseq s 1 (1- (length s)))))
>> `----
>
> Don't bother with these block quotes. Or if you really need, block
> quote your english text, like this:
What's wrong with using block quotes?
> ;; Hi Group!
> ;;
> ;; I'm in the process of learning Common Lisp, and have a quick question.
> ;;
> ;; I have a string, and want its lines to be filled at (say) 80
> ;; characters. What is a good way to do that?
> ;;
> ;; I came up with the following solution:
Commenting article text in a newsgroup posting seems artificial to me.
What's your reasoning?
> What about:
>
> (defun fill-string (str &optional (len 72))
> "Return string with text left-justified on lines LEN wide."
> (format nil (format nil "~~{~~<~~%~~1,~A:;~~A~~>~~^ ~~}" len)
> (cl-ppcre:split "\\s+" str)))
This was exactly the thing I was looking for. Thanks! I didn't know
format could do this. Nice.
On another note, format is obviously quite powerful, but my eyes hurt a
little bit looking at those format strings...
--
Henrik Jegbjerg Hansen
Henrik Jegbjerg Hansen <···@freecode.dk> writes:
> Pascal Bourguignon <···@informatimago.com> writes:
>
>>> ,----
>>> | (defun fill-string (str &optional len d)
>>> | "Return string with lines filled, optionally using LEN to set value
>>> | of *print-right-margin*."
>>> | (let*
>>> | ((*print-right-margin* len)
>>> | (s (princ-to-string (cl-ppcre:split "\\s+" str))))
>>> | (subseq s 1 (1- (length s)))))
>>> `----
>>
>> Don't bother with these block quotes. Or if you really need, block
>> quote your english text, like this:
>
> What's wrong with using block quotes?
This:
C/USER1[199]> | (defun fill-string (str &optional len d)
| "Return string with lines filled, optionally using LEN to set value
| of *print-right-margin*."
| (let*
| ((*print-right-margin* len)
| (s (princ-to-string (cl-ppcre:split "\\s+" str))))
| (subseq s 1 (1- (length s)))))
*** - EVAL: variable
| (defun fill-string (str &optional len d)
| has no value
The following restarts are available:
USE-VALUE :R1
You may input a value to be used instead of | (defun fill-string (str &optional len d)
|.
STORE-VALUE :R2
You may input a new value for | (defun fill-string (str &optional len d)
|.
ABORT :R3 ABORT
C/Break 1 USER1[200]>
>> ;; Hi Group!
>> ;;
>> ;; I'm in the process of learning Common Lisp, and have a quick question.
>> ;;
>> ;; I have a string, and want its lines to be filled at (say) 80
>> ;; characters. What is a good way to do that?
>> ;;
>> ;; I came up with the following solution:
>
> Commenting article text in a newsgroup posting seems artificial to me.
> What's your reasoning?
See above.
>> What about:
>>
>> (defun fill-string (str &optional (len 72))
>> "Return string with text left-justified on lines LEN wide."
>> (format nil (format nil "~~{~~<~~%~~1,~A:;~~A~~>~~^ ~~}" len)
>> (cl-ppcre:split "\\s+" str)))
>
> This was exactly the thing I was looking for. Thanks! I didn't know
> format could do this. Nice.
>
> On another note, format is obviously quite powerful, but my eyes hurt a
> little bit looking at those format strings...
Of course, it looks like write-only code. The more so when you write
meta-format like I did.
But there are alternatives, or you could easily write your own set of
functions and macro to generate these strings in a readable way.
http://lemonodor.com/archives/001280.html
--
__Pascal Bourguignon__ http://www.informatimago.com/
The world will now reboot. don't bother saving your artefacts.