Джерело:
Common Lisp Tips
Дата публікації:
14/09/2012 13:29
Постійна адреса новини:
http://www.vsinovyny.com/8323829
14/09/2012 13:29 // Common Lisp Tips
Scanning a string can be done using input functions withwith-input-from-string:
(with-input-from-string (stream "Hello World! How do you do? ")
(let ((c (make-string 4)))
(loop
:for pos = (read-sequence c stream)
:while (= pos (length c))
:do (print c)
:finally (terpri))))
"Hell"
"o Wo"
"rld!"
" Ho"
"w do"
" you"
" do?"
nil
This can also be done using a displaced adjustable array, without copying the data that is read:
(defun make-vector-cursor (vector &key (size 0) (offset 0))
(make-array size
:element-type (array-element-type vector)
:adjustable t
:displaced-to vector
:displaced-index-offset offset))
(defun advance-cursor (cursor &key (size (length cursor)))
(multiple-value-bind (vector offset) (array-displacement cursor)
(adjust-array cursor size
:element-type (array-element-type vector)
:displaced-to vector
:displaced-index-offset (+ offset (length cursor)))))
(let ((c (make-vector-cursor "Hello World! How do you do? " :size 4)))
(loop
:do (print c)
:while (ignore-errors (advance-cursor c))
:finally (terpri)))
"Hell"
"o Wo"
"rld!"
" Ho"
"w do"
" you"
" do?"
NIL
(let ((c (make-vector-cursor "Hello World! How do you do? " :size 1)))
(loop
:for size :from 1
:do (print c)
:while (ignore-errors (advance-cursor c :size size))
:finally (terpri)))
"H"
"e"
"ll"
"o W"
"orld"
"! Ho"
"w do y"
"ou do? "
nil
This post courtesy of Pascal J. Bourguignon
| « |
Наступна новина з архіву Putting the R in REPL |
Попередня новина з архіву Binding keyword arguments |
» | |
|
|
||||