Skip to content

Latest commit

 

History

History
98 lines (78 loc) · 2.78 KB

0068-cl-utilities.org

File metadata and controls

98 lines (78 loc) · 2.78 KB

cl-utilities

This is a yet another utilities library. It is used by only a single poftheday’s dependency - xml-emitter, reviewed on 10 May.

Amount of tools in this toolbox is not very large and mostly covered by more popular libs.

For example, it has a macro for item collection. For example, this macro can be useful to collect hash key value pairs into an alist. But this facility is also available from UIOP:

POFTHEDAY> (let ((the-hash #h(:foo "bar"
                              :blah "minor")))
             (cl-utilities:collecting
               (maphash
                (lambda (key value)
                  (cl-utilities:collect
                      (cons key value)))
                the-hash)))
((:FOO . "bar") (:BLAH . "minor"))

;; Here is the similar code using UIOP
POFTHEDAY> (let ((the-hash #h(:foo "bar"
                              :blah "minor")))
             (uiop:while-collecting (collect)
               (maphash
                (lambda (key value)
                  (collect (cons key value)))
                the-hash)))
((:FOO . "bar") (:BLAH . "minor"))

But UIOP is more powerful allows you to collect many types of items simultaneously. For example, we might want to collect keys and values into the separate lists:

POFTHEDAY> (let ((the-hash #h(:foo "bar"
                              :blah "minor")))
             (uiop:while-collecting (collect-key collect-value)
               (maphash
                (lambda (key value)
                  (collect-key key)
                  (collect-value value))
                the-hash)))
(:FOO :BLAH)
("bar" "minor")

An interesting feature is a function read-delimited. It can be useful to read chars from the stream into a limited buffer:

POFTHEDAY> (with-input-from-string (stream "The string
with multiple
lines.")
             (let ((buffer (str:repeat 10 " ")))
               (loop for num-chars = (cl-utilities:read-delimited
                                      buffer
                                      stream)
                     while (not (zerop num-chars))
                     do (format t "~A chars were read~%"
                                num-chars)
                        (format t "Buffer: ~A~2%"
                                (str:substring 0 num-chars
                                               buffer)))))
10 chars were read
Buffer: The string

10 chars were read
Buffer: with multi

2 chars were read
Buffer: le

6 chars were read
Buffer: lines.

Probably this facility is also covered by more popular utility library?